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;
Related
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?
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
I'm trying to parse date time from a PNG file but can't quite get it with QRegExp
this_png_20211208_1916.png
QDateTime Product::GetObstime()
{
QDateTime obstime;
QString filename = FLAGS_file_name.c_str();
QString year, month, day, hour, minute, second;
QRegExp regexp = QRegExp("^.*\\w+_(\\d{4}\\d{2}\\d{2})_(\\d{2}\\d{2})\\.png$");
VLOG(3) << " filename: " << filename.toStdString();
if(regexp.indexIn(filename) !=-1)
{
VLOG(3) << " filename: " << filename.toStdString();
QStringList dt_bits = regexp.capturedTexts();
if(dt_bits.size() >=2)
{
year = dt_bits.at(1).mid(0, 4);
month = dt_bits.at(1).mid(5, 2);
day = dt_bits.at(1).mid(8, 2);
hour = dt_bits.at(2).mid(0, 2);
minute = dt_bits.at(2).mid(3, 2);
second = dt_bits.at(2).mid(3, 2);
VLOG(3) << " Year: " << year.toStdString()
<< " Month: " << month.toStdString()
<< " Day: " << day.toStdString()
<< " Hour: " << hour.toStdString()
<< " Min: " << minute.toStdString()
<< " Sec: " << second.toStdString();
QString datetime_str = year + "-" + month + "-" + day +
"T" + hour + ":" + minute + second + "00Z";
obstime = QDateTime::fromString(datetime_str, Qt::ISODate);
if (obstime.isValid())
{
VLOG(3)<<"Date iS VALID: "<<obstime.toString(Qt::ISODate).toStdString();
}
else
{
LOG(ERROR)<<" Error! Date Time bits did not match format.";
}
}
}
return obstime;
}
been using tools like https://regex101.com/
but to no avail. am I missing something?
You have the following errors in your code:
month = dt_bits.at(1).mid(5, 2); should be month = dt_bits.at(1).mid(4, 2); because the index is 0-based, not 1-based
day = dt_bits.at(1).mid(8, 2); should be day = dt_bits.at(1).mid(6, 2);
minute = dt_bits.at(2).mid(3, 2); should be minute = dt_bits.at(2).mid(2, 2);
second = dt_bits.at(2).mid(3, 2); should be second = "00"; because your filenames do not contain seconds
Generally I would recommend doing all of the work in the regex instead of doing some fancy splitting using QString::mid():
QRegExp regexp = QRegExp("^.*\\w+_(\\d{4})(\\d{2})(\\d{2})_(\\d{2})(\\d{2})\\.png$");
This gives you all the fields in separate groupings, no need for QString::mid() at all.
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 ...
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);
}