Problems with conversion between mktime and gmtime_r - c++

If I define a broken-down time using struct tm, then convert it to time_t using mktime and the result try to get back again as an struct tm I thought I was going to get the same result.
OK, maybe the first time something change because I define tm_isdst = -1 so mktime maybe affect that value, but not the second time.
The following code does that:
void print(const tm& t)
{
std::cout << t.tm_mday << '/'
<< t.tm_mon << '/'
<< t.tm_year << ' '
<< t.tm_hour << ':'
<< t.tm_min << ':'
<< t.tm_sec << "; "
<< t.tm_wday << '|'
<< t.tm_isdst << '\n';
}
int main(){
tm t;
t.tm_sec = 32;
t.tm_min = 22;
t.tm_hour = 0;
t.tm_mday = 6;
t.tm_mon = 0;
t.tm_year = 2023 - 1900;
t.tm_wday = 5;
t.tm_isdst = -1;
print(t);
time_t timet0 = mktime(&t);
std::cout << "time_t0 = " << timet0 << '\n';
gmtime_r(&timet0, &t);
print(t);
time_t timet1 = mktime(&t);
std::cout << "time_t1 - time_t0 = " << (timet1 - timet0) << '\n';
std::cout << "time_t1 = " << timet1 << '\n';
gmtime_r(&timet1, &t);
print(t);
timet1 = mktime(&t);
std::cout << "time_t = " << timet1 << '\n';
gmtime_r(&timet1, &t);
print(t);
}
And the result of executing it is
6/0/123 0:22:32; 5|-1
time_t0 = 1672960952
5/0/123 23:22:32; 4|0
time_t1 - time_t0 = -3600
time_t1 = 1672957352
5/0/123 22:22:32; 4|0
time_t = 1672953752
5/0/123 21:22:32; 4|0
Why am I losing an hour every time I call the pair mktime/gmtime_r?

Related

Month auto increment using mktime and timegm functions in C++

I'd like to convert a string date (UTC) to a timestamp using C++.
It works fine except for the month, which is auto incremented by one.
If the string is 20221222074648, for 2022-12-22 07:46:48, the timestamp will be 1674373608, which is the timestamp of 2023-01-22 07:46:48.
I don't understand the reason of this auto-increment, because there's no changes of the month's variable in the code below.
cout << "date : " << receivedDate << endl;
struct tm t;
time_t timestamp;
size_t year_size = 4;
size_t other_size = 2;
t.tm_year = stoi(receivedDate.substr(0, 4), &year_size, 10) - 1900;
t.tm_mon = stoi(receivedDate.substr(4, 2), &other_size, 10);
t.tm_mday = stoi(receivedDate.substr(6, 2), &other_size, 10);
t.tm_hour = stoi(receivedDate.substr(8, 2), &other_size, 10);
t.tm_min = stoi(receivedDate.substr(10, 2), &other_size, 10);
t.tm_sec = stoi(receivedDate.substr(12, 2), &other_size, 10);
t.tm_isdst = -1;
cout << "year : " << t.tm_year + 1900 << "\nmonth : " << t.tm_mon << "\nday : " << t.tm_mday << "\nhour : " << t.tm_hour << "\nminutes : " << t.tm_min << "\nseconds : " << t.tm_sec << endl;
timestamp = timegm(&t);
cout << "timestamp : " << timestamp << endl;
cout << "asctime timestamp : " << asctime(&t);
Your problem is pretty simple !
the tm.tm_mom goes from 0 to 11, You need to add -1 on your month value
t.tm_mon = stoi(receivedDate.substr(4, 2), &other_size, 10) - 1;

How to get the value of chrono c++?

I am trying to make a text game where there is a timer and once the game was finished before or in 60 seconds, there is a bonus points. However, I have no idea how can I get the value or the time from using the chrono without cout-ing it. I want to use the value for calculating the bonus point. i can cout the value through the .count() but I cannot get that value to use for the condition part.
here's my code for the scoring part:
void Game::score(auto start, auto end) {
int bonus = 0;
int total = 0;
string name;
box();
gotoxy(10,8); cout << "C O N G R A T U L A T I O N S";
gotoxy(15,10); cout << "You have successfully accomplished all the levels!";
gotoxy(15,11); cout << "You are now a certified C-O-N-N-E-C-T-o-r-I-s-T" << char(002) << char(001);
gotoxy(20,13); cout << "= = = = = = = = = = GAME STATS = = = = = = = = = =";
gotoxy(25,15); cout << "Time Taken: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds";
gotoxy(25,16); cout << "Points: " << pts << " points";
if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 60) {
bonus == 5000;
} else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 90) {
bonus == 3000;
} else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 120) {
bonus == 1000;
}
gotoxy(30,17); cout << "Bonus Points (Time Elapsed): " << bonus;
total = pts + bonus;
gotoxy(25,18); cout << "Total Points: " << total << " points";
gotoxy(20,20); cout << "Enter your name: ";
cin >> name;
scoreB.open("scoreboard.txt",ios::app);
scoreB << name << "\t" << total << "\n";
scoreB.close();
}
You should really use the chrono literals for comparing durations. See example here:
#include <chrono>
#include <iostream>
#include <thread>
using Clock = std::chrono::system_clock;
void compareTimes(std::chrono::time_point<Clock> startTime,
std::chrono::time_point<Clock> finishTime) {
using namespace std::chrono_literals;
std::chrono::duration<float> elapsed = finishTime - startTime;
std::cout << "elapsed = " << elapsed.count() << "\n";
if (elapsed > 10ms) {
std::cout << "over 10ms\n";
}
if (elapsed < 60s) {
std::cout << "under 60s\n";
}
}
int main() {
using namespace std::chrono_literals;
auto startTime = Clock::now();
std::this_thread::sleep_for(20ms);
auto finishTime = Clock::now();
compareTimes(startTime, finishTime);
return 0;
}
Demo: https://godbolt.org/z/hqv58acoY

How to automatically generate new CSV file according to current time using C++

I have finished the task of reading sensor data and store into CSV file successfully. But everytime I want to stop reading & storing sensor data, I must close the exe file. Due to the factory operation, they want to continuously run the program during a month without closing the exe file. I was required to modify the code to generate a new CSV file after a period (e.g. 1 hour). So the file name maybe: 20190516_10h0m0s, 20190516_11h0m0s, 20190516_12h0m0s...
I tried to use (struct tm) and for(;timeinfo->tm_min < 60;) to make a new CSV produced after 60'. But every loop, the item Data, time, Channel are written to the CSV again. It looks weird. But when I create the file dest and put the dest.open outside for loop, it only store correct data format without creating a new CSV as I want. Please suggest me how to make it work as expectation.
void EX_GetMultiValue(LONG i_lDriverHandle,
WORD i_wSlotID,
struct SlotInfo &i_SlotInfo)
{
char filename[20], filename2[20];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_mon++; // Because the range of tm_mon is 0~11, so we need to increment it by 1
timeinfo->tm_year = timeinfo->tm_year + 1900; // Because year counted since 1900
clock_t start = clock();
sprintf(filename,
"%04d%02d%02d_%02dh%02dm%02ds.csv",
timeinfo->tm_year,
timeinfo->tm_mon,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
printf("\nFilename: %s", filename);
ofstream dest2;
dest2.open(filename, ios_base::app | ios_base::out);
dest2 << "Date" << "," << "Time" << "," << "milisecond" << ","
<< "Channel 0" << "," << "Channel 1" << "," << "Channel 2" << ","
<< "Channel 3" << "," << "Channel 4" << "," << "Channel 5" << ","
<< "Channel 6" << "," << "Channel 7" << "," << "Channel 8" << ","
<< "Channel 9" << "," << "Channel 10" << "," << "Channel 11" << ","
<< endl;
for (; timeinfo->tm_min < 60;)
{
ofstream dest;
dest.open(filename, ios_base::app | ios_base::out);
LONG lGetMultiValueResult = AIO_GetValues(i_lDriverHandle,
i_wSlotID,
wRawValue); //get raw value
if (ERR_SUCCESS == lGetMultiValueResult)
{
clock_t timeElapsed = clock() - start;
unsigned secElapsed = timeElapsed / CLOCKS_PER_SEC;
unsigned msElapsed = timeElapsed / CLOCKS_PER_MS;
while (msElapsed >= 1000)
msElapsed -= 1000;
while ((timeinfo->tm_sec + secElapsed) > 59)
{
timeinfo->tm_sec -= 60;
timeinfo->tm_min++;
}
while (timeinfo->tm_min > 59)
{
timeinfo->tm_min -= 60;
timeinfo->tm_hour++;
}
while (timeinfo->tm_hour > 23)
{
timeinfo->tm_hour -= 24;
timeinfo->tm_mday++;
}
dest << timeinfo->tm_year << "-" << timeinfo->tm_mon << "-"
<< timeinfo->tm_mday << "," << timeinfo->tm_hour << "h"
<< timeinfo->tm_min << "m" << timeinfo->tm_sec + secElapsed
<< "s" << ",";
dest << msElapsed << "ms" << ",";
for (iCnt = 0; iCnt < g_ChannelNum; iCnt++)
{
wRangeType = *(i_SlotInfo.wChRange + iCnt); //get range type
EX_ScaleRawValue(wRangeType,
wRawValue[iCnt],
&dScaledValue,
cUnit); //get scale value
if (strcmp(cUnit, "UN") != 0)
{
printf("Channel %d raw data is 0x%04X, scaled value is %.4f %s.\n",
iCnt,
wRawValue[iCnt],
dScaledValue,
cUnit);
dest << dScaledValue << ",";
Sleep(1);
}
else
printf("Channel %d range is unknown.\n", iCnt);
}
dest << endl;
}
else
printf("Fail to get value, error code = %d\n",
lGetMultiValueResult);
dest.close();
dest2.close();
if (dest == NULL)
{
perror("Error creating file: ");
return;
}
}
Using standard C++-facilities and Howard Hinnants date.h:
#include <chrono>
#include <string>
#include <iostream>
#include "date.h"
int main()
{
auto now{ std::chrono::system_clock::now() };
std::string filename{ date::format("%Y%m%e_%Hh%Mm%Ss.csv", now) };
std::cout << filename << '\n';
}
I don't know what you're trying to do with the rest of your code, so ...

Making POD classes movable

I have a POD class and I want to make it movable for efficiency. I keep all the data in a std::array member object, and I make my public member variables references to parts of this std::array object. By doing this, now I am able to move the entire data by moving the std::array instance in the move constructor (I know that it is not literally a POD class anymore after writing constructors.).
Is this a good method of doing this? Does it actually move the data? See the code output below: After moving the std::array, I observe that both objects have the same values. It looks like it doesn't move, but it copies the data. What is the problem here?
#include <array>
class MyPodClass
{
private:
typedef double TYPE_x;
typedef double TYPE_y;
typedef double TYPE_z;
typedef int TYPE_p;
typedef int TYPE_r;
typedef int TYPE_s;
typedef char TYPE_k;
typedef char TYPE_l;
typedef char TYPE_m;
typedef float TYPE_a;
typedef float TYPE_b;
typedef float TYPE_c;
enum TypeSizes
{
STARTING_POSITION_x = 0,
STARTING_POSITION_y = STARTING_POSITION_x + sizeof(TYPE_x),
STARTING_POSITION_z = STARTING_POSITION_y + sizeof(TYPE_y),
STARTING_POSITION_p = STARTING_POSITION_z + sizeof(TYPE_z),
STARTING_POSITION_r = STARTING_POSITION_p + sizeof(TYPE_p),
STARTING_POSITION_s = STARTING_POSITION_r + sizeof(TYPE_r),
STARTING_POSITION_k = STARTING_POSITION_s + sizeof(TYPE_s),
STARTING_POSITION_l = STARTING_POSITION_k + sizeof(TYPE_k),
STARTING_POSITION_m = STARTING_POSITION_l + sizeof(TYPE_l),
STARTING_POSITION_a = STARTING_POSITION_m + sizeof(TYPE_m),
STARTING_POSITION_b = STARTING_POSITION_a + sizeof(TYPE_a),
STARTING_POSITION_c = STARTING_POSITION_b + sizeof(TYPE_b),
END_POSITION = STARTING_POSITION_c + sizeof(TYPE_c),
};
std::array<unsigned char, END_POSITION> MovableBulkData;
public:
MyPodClass()
: //x(*static_cast<TYPE_x*>(&MovableBulkData[STARTING_POSITION_x])), // ERROR: Invalid type conversion. Why?
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
MyPodClass(MyPodClass && RValue)
: MovableBulkData(std::move(RValue.MovableBulkData)),
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
const MyPodClass & operator=(MyPodClass && RValue)
{
MovableBulkData = std::move(RValue.MovableBulkData);
return *this;
}
TYPE_x & x;
TYPE_y & y;
TYPE_z & z;
TYPE_p & p;
TYPE_r & r;
TYPE_s & s;
TYPE_k & k;
TYPE_l & l;
TYPE_m & m;
TYPE_a & a;
TYPE_b & b;
TYPE_c & c;
};
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
MyPodClass PodObject1, PodObject2;
PodObject1.y = 3.4;
PodObject1.s = 4;
PodObject1.m = 'm';
PodObject1.a = 2.3f;
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Moving PodObject1 to PodObject2..." << std::endl << std::endl;
PodObject2 = std::move(PodObject1);
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Modifying PodObject1 and PodObject2..." << std::endl << std::endl;
PodObject1.s = 5;
PodObject2.m = 'n';
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << std::endl;
_wsystem(L"timeout /t 60 /nobreak");
return 0;
}
Output:
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = -9.25596e+61
PodObject2.s = -858993460
PodObject2.m = ╠
PodObject2.a = -1.07374e+08
Moving PodObject1 to PodObject2...
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = m
PodObject2.a = 2.3
Modifying PodObject1 and PodObject2...
PodObject1.y = 3.4
PodObject1.s = 5
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = n
PodObject2.a = 2.3
This is a misuse of move semantics. Since your class contains a number of simple data members like int and float, there is really nothing to move. You'd be better off with memcpy(), which is probably close to what your compiler gives you for free if you just write the class the normal, naive way, with no std::array and no pointer gymnastics.
Move semantics would have been useful here if your class contained e.g. a std::string, because std::string uses dynamically allocated memory which can be "moved" (read: adopted) into the target of a move.
The above of course means that you could "fix" your problem by dynamically allocating the array, which would allow you to move it. But in the end this would be a baroque way to achieve the effect of using a trivial POD class with no gymnastics and storing it in a std::unique_ptr, which of course enables move semantics.

What is an intelligent way to determine max size of a strftime char array?

How can I size the char array for strftime without trial and error? Using mktime, the timestamp size N in the example has to be greater 86, otherwise I get arbitrary dates back.
e.g.
N = 86 : 2013-07-13 02:41
N = 82 : 1979-05-18 13:23
How do I efficiently scale N without prior knowledge of the date? The check >0 does not help.
#include <iostream>
#include <cstring>
#include <ctime>
#define N 86
using namespace std;
int main(void)
{
time_t t;
struct tm ts;
char timestamp[N] ;
ts.tm_min = 41;
ts.tm_hour = 2;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
t = mktime(&ts);
if (strftime(timestamp, sizeof(timestamp)-1, "%Y-%m-%d %H:%M", &ts) > 0)
cout << timestamp;
else {
cerr << "strftime failed." <<endl;
return 1;
}
return 0;
}
From the documentation for strftime:
If the length of the resulting C string, including the terminating null-character, doesn't exceed maxsize, the function returns the total number of characters copied to ptr (not including the terminating null-character). Otherwise, it returns zero, and the contents of the array pointed by ptr are indeterminate.
That means if you don't know the size and can dynamically allocate a string you can do something along the lines of:
int size = N; // Some starting size
char *timestamp = malloc(size);
// Your time stuff
int result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
// While there isn't enough room to store the result
while (result == 0)
{
free(timestamp); // Free old data
size *= 2; // Double the size (should be more than enough)
timestamp = malloc(size); // Allocate the new size. You can check for failed allocations here as well.
// Retry
result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
}
std::cout << timestamp;
Because you tagged this as C++, perhaps you might consider the following.
--> Note that there is no struggle with the string size here.
// function to create a timestamp style string
std::string yyDmmDdd_hhCmmGet(time_t tt)
{
std::stringstream ss;
// goal - something like: "%Y-%m-%d %H:%M"
{
struct tm mybdtod; // linux api: my broken down time of day
// the following is a relatively slow function
::localtime_r (&tt, &mybdtod);
// linux api - convert time_t to tm as local time
ss << std::setw(4) << (mybdtod.tm_year+1900)
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mon+1
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mday
<< " ";
ss << std::dec << std::setfill('0') << std::setw(2)
<< mybdtod.tm_hour
<< ":"
<< std::setfill('0') << std::setw(2)
<< mybdtod.tm_min;
}
return(ss.str());
}
int t186(void)
{
struct tm ts; // linux api: time struct
::memset(&ts, 0, sizeof(tm));
ts.tm_min = 41;
ts.tm_hour = 3-1;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
time_t tt = mktime(&ts); // linux api: Convert tm struct to time_t
// format time_t to string
std::string s = yyDmmDdd_hhCmmGet(tt); // timestamp style
std::cout << "\n" << s
<< "\n s.size(): "
<< s.size() << " chars" << std::endl;
// now we know how many chars timestamp needs
// add 1 to size because ?strftime seems to need it?
char timestamp[s.size()+1];
(void)strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M", &ts);
// linux api: format time_t to string
std::cout << "\n" << timestamp << std::endl;
std::cout << " sizeof(timestamp): "
<< sizeof(timestamp) << " chars" << std::endl;
return(0);
}