SYSTEMTIME equivalent for linux - c++

I am doing cross-platform compilation of a huge solution written in C++ from VS2010 to linux environment.
My question is: what is the closest equivalent of struct SYSTEMTIME in linux ?
typedef struct _SYSTEMTIME
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
Thanks in advance

Have a look at ctime(3) and related functions. The struct tm appears to be a close match to the _SYSTEMTIME structure.
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};

Related

C++ custom time date struct to utc epoch

I use a library that uses the following struct to define a start timestamp as follows.
struct SYSTEMTIME {
/** year */
WORD year;
/** month */
WORD month;
/** day of week */
WORD dayOfWeek;
/** day */
WORD day;
/** hour */
WORD hour;
/** minute */
WORD minute;
/** second */
WORD second;
/** milliseconds */
WORD milliseconds;
};
For every log entry after this time is specified in nanoseconds difference from this first timestamp.
Lets say its UTC 2017-12-19 14:44:00
And the first following log entries are 397000ns after this.
How do I create a chronos, time_t or unix time from epoch from the first SYSTEMTIME struct and then add the nanoseconds to it.
Printout should be for this first entry
2017-12-19 14:44:00.000397
Best regards
Updated
I've slightly modified the code below to convert between SYSTEMTIME and date::sys_time<std::chrono::milliseconds>, instead of date::sys_time<std::chrono::nanoseconds>.
Rationale: So that there is no implicit precision loss in to_SYSTEMTIME. Clients of to_SYSTEMTIME can explicitly truncate precision in any way that they desire (floor, round, ceil, etc.). And failure to truncate precision (if needed) won't be a silent run time error.
The client code (in main) is not impacted by this change.
You could use Howard Hinnant's free, open-source, header-only date/time library for this:
#include "date/date.h"
#include <iostream>
using WORD = int;
struct SYSTEMTIME {
/** year */
WORD year;
/** month */
WORD month;
/** day of week */
WORD dayOfWeek;
/** day */
WORD day;
/** hour */
WORD hour;
/** minute */
WORD minute;
/** second */
WORD second;
/** milliseconds */
WORD milliseconds;
};
date::sys_time<std::chrono::milliseconds>
to_sys_time(SYSTEMTIME const& t)
{
using namespace std::chrono;
using namespace date;
return sys_days{year{t.year}/t.month/t.day} + hours{t.hour} +
minutes{t.minute} + seconds{t.second} + milliseconds{t.milliseconds};
}
int
main()
{
using namespace std::chrono;
using namespace date;
SYSTEMTIME st{2017, 12, 2, 19, 14, 44, 0, 0};
auto t = to_sys_time(st) + 397000ns;
std::cout << floor<microseconds>(t) << '\n';
}
Output:
2017-12-19 14:44:00.000397
This converts a SYSTEMTIME to a std::chrono::time_point<system_clock, milliseconds> (which has a type-alias named date::sys_time<milliseconds>) by collecting the different parts out of the SYSTEMTIME. It then simply adds nanoseconds to that time_point, truncates it to the desired precision of microseconds, and streams it out.
If it would be helpful, here is how you could use the same library to do the opposite conversion:
SYSTEMTIME
to_SYSTEMTIME(date::sys_time<std::chrono::milliseconds> const& t)
{
using namespace std::chrono;
using namespace date;
auto sd = floor<days>(t);
year_month_day ymd = sd;
auto tod = make_time(t - sd);
SYSTEMTIME x;
x.year = int{ymd.year()};
x.month = unsigned{ymd.month()};
x.dayOfWeek = weekday{sd}.c_encoding();
x.day = unsigned{ymd.day()};
x.hour = tod.hours().count();
x.minute = tod.minutes().count();
x.second = tod.seconds().count();
x.milliseconds = tod.subseconds().count();
return x;
}
You can use mktime() from <ctime> to convert tm, to time_t, which is an integer type.
tm is very simmilar to your SYSTEMTIME struct. Therefore you sould be able to translate them easily back and forth.
translate your struct into tm and then use gmtime() like this.
#include <ctime>
struct tm time = fromSystemtime(...);
time_t timestamp;
timestamp = mktime(&time);
For more details see the following links:
struct tm
time_t
mktime

math with ctime and time_t

Does anyone know how todo math with ctime? I need to be able to get the time in sec in "time_t" (like it normally does) and then subtract a set number of seconds from it before inputting time_t into ctime to get the time and date.
so basically it would calculating the date of so many sec ago.
time_t
The most basic representation of a date and time is the type time_t. The value of a time_t variable is the number of seconds since January 1, 1970, sometimes call the Unix epoch. This is the best way to internally represent the start and end times for an event because it is easy to compare these values.
struct tm
While time_t represents a date and time as a single number, struct tm represents it as a struct with a lot of numbers:
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
};
Conversion
You can convert a time_t value to a struct tm value using the localtime function:
struct tm startTM;
time_t start;
/* ... */
startTM = *localtime(&start);
So,you can subtract subtract a set number of seconds like this
startTm.tm_sec -= somesecond;
add convert to time_t like this
struct tm startTM;
time_t start;
/* ... */
start = mktime(&startTM);
and use ctime fun to convert date
ctime(&start)
hope it can helpful!
You can try:
time_t now = time( NULL);
struct tm now_tm = *localtime( &now);
now_tm.tm_sec -= 50; // subtract 50 seconds to the time
now_tm.tm_sec +=1000; // add 1000 sec to the time
printf( "%s\n", asctime( &now_tm));
time_t is an integral type. It always represents a number of seconds, so you can freely add/subtract integers from it.
Example:
time_t now = time(nullptr);
time_t one_minute_ago = now - 60;
std::cout << ctime(&one_minute_ago) << std::endl;

Wrong time with localtime()

Running dateon my server results in the correct time. But using localtime() in C(++) I'm getting the wrong time.
Running date: Fr 30. Nov 12:15:36 CET 2012
Using localtime(): Fr 30 Nov 2012 11:15:36 CET
What's wrong here?
OS: Debian 5.0.10
Some code:
struct tm* today;
today = localtime(...);
strftime(timeBuffer,50,myConnection.getMetaData().getDateFormat().c_str(),today);
disclaimer : This answer was written before any mention of strftime was added, and was a gut reaction to the 1 hour difference in the timestamps. Looking back at it now, that 1 hour difference couldn't have been due to DST (because the dates are not in summer), but is likely showing a UTC timestamp (1 hour difference between UTC and CET).
Unfortunately, the answer was accepted, and so I can't delete it. Even more unfortunate, is that the question as it stands is not answerable without additional information.
Leaving the original answer here for full transparency, but know that it does not address the question as asked :
The struct tm returned by localtime has a tm_isdst field that indicates whether daylight saving time (DST) is in effect. You need to take that field into account when formatting the time.
Try using asctime to format the time eg. :
puts(asctime(today));
I have experienced the same problem while writing a date adjustment routine. Adding 86400 seconds (= 1 day) to any given datetime value should result in incrementing the datetime value by one day. However in testing, the output value invariably added exactly one hour to the expected output. For instance, '2019-03-20 00:00:00' incremented by 86400 seconds resulted in '2019-03-21 01:00:00'. The reverse also occurred: '2019-03-21 00:00:00' decremented by -86400 resulted in '2019-03-20 01:00:00'.
The solution (inexplicably) was to subtract 3600 seconds (one hour) from the final interval before applying it to the input datetime.
The solution (thanks to helpful comments from #Lightness-Races-in-Orbit) was to set tm_isdst to -1 before calling mktime(). This tells mktime() that the DST status for the input datetime value is unknown, and that mktime() should use the system timezone databases to determine the correct timezone for the input datetime value.
The function (as corrected below) allows for any integer adjustment of days and now produces consistently correct results:
#include <stdio.h>
#include <string.h>
#include <time.h>
/*******************************************************************************
* \fn adjust_date()
*******************************************************************************/
int adjust_date(
char *original_date,
char *adjusted_date,
char *pattern_in,
char *pattern_out,
int adjustment,
size_t out_size)
{
/*
struct tm {
int tm_sec; // seconds 0-59
int tm_min; // minutes 0-59
int tm_hour; // hours 0-23
int tm_mday; // day of the month 1-31
int tm_mon; // month 0-11
int tm_year; // year minus 1900
int tm_wday; // day of the week 0-6
int tm_yday; // day in the year 0-365
int tm_isdst; // daylight saving time
};
*/
struct tm day;
time_t one_day = 86400;
// time_t interval = (one_day * adjustment) - 3600;
time_t interval = (one_day * adjustment);
strptime(original_date, pattern_in, &day);
day.tm_isdst = -1;
time_t t1 = mktime(&day);
if (t1 == -1) {
printf("The mktime() function failed");
return -1;
}
time_t t2 = t1 + interval;
struct tm *ptm = localtime(&t2);
if (ptm == NULL) {
printf("The localtime() function failed");
return -1;
}
strftime(adjusted_date, out_size, pattern_out, ptm);
return 0;
}
/*******************************************************************************
* \fn main()
*******************************************************************************/
int main()
{
char in_date[64] = "20190321000000" ,
out_date[64],
pattern_in[64] = "%Y%m%d%H%M%S",
pattern_out[64] = "%Y-%m-%d %H:%M:%S";
int day_diff = -1,
ret = 0;
size_t out_size = 64;
memset(out_date, 0, sizeof(out_date));
ret = adjust_date(in_date, out_date, pattern_in, pattern_out, day_diff, out_size);
if (ret == 0)
{
printf("Adjusted date: '%s'\n", out_date);
}
return ret;
}
Hopefully, this will be of some help to somebody. Your constructive comments are greatly appreciated.
handling date time is very error prone and usually badly tested. i always recommend using boost::date_time http://www.boost.org/doc/libs/1_52_0/doc/html/date_time.html
here are nice examples http://en.highscore.de/cpp/boost/datetime.html
Did you try this ? :
time_t rawtime;
struct tm * today;
time ( &rawtime );
today= localtime ( &rawtime );
puts(asctime (today));

Converting a unix time to a human readable format

I'm building my own unix time to human readable conversion, and I'm stuck.
I can extract the year just fine, but the day is proving too tricky.
/*
Converts a 32-bit number of seconds after 01-01-1970 to a _SYSTEMTIME structure
*/
static _SYSTEMTIME Convert(unsigned long a_UnixTime)
{
newtime.wMilliseconds = 0;
newtime.wYear = (unsigned long)((float)a_UnixTime / (364.24f * 24.f * 60.f * 60.f));
newtime.wDay = (a_UnixTime - (unsigned long)((float)newtime.wYear * 364.24f * 24.f * 60.f * 60.f)); // returns 65177
return newtime;
}
Or is there a built-in function that I've overlooked?
Thanks in advance.
UPDATE:
Hmm... it seems Windows Mobile doesn't support strftime, time or localtime, so I'll still have to roll my own. :(
Are you looking for gmtime?
struct tm * gmtime(const time_t *clock);
External declarations, as well as the tm structure definition, are contained in the <time.h> include file. The tm structure includes at least the following fields:
int tm_sec; /* seconds (0 - 60) */
int tm_min; /* minutes (0 - 59) */
int tm_hour; /* hours (0 - 23) */
int tm_mday; /* day of month (1 - 31) */
int tm_mon; /* month of year (0 - 11) */
int tm_year; /* year - 1900 */
int tm_wday; /* day of week (Sunday = 0) */
int tm_yday; /* day of year (0 - 365) */
int tm_isdst; /* is summer time in effect? */
char *tm_zone; /* abbreviation of timezone name */
long tm_gmtoff; /* offset from UTC in seconds */
If you want to format to print, you need strftime(), it's the standard solution, used together with e.g. localtime() to convert the raw timestamp to a more human-friendly format.

C++ function that returns system date

I need function in c++ that allows me to retrieve and store the system date. I have a class for storing dates.
Dealing with dates and time is difficult, thats why people use libraries. I prefer boost::date_time.
boost::posix_time::ptime local_time = boost::posix_time::second_clock::local_time();
boost::gregorian::date d = local_time.date();
d is a current date in local time, which use computer timezone settings. To get UTC time you can use boost::posix_time::second_clock::universal_time().
From time.h:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
time_t time(time_t * timer);
struct tm* gmtime(const time_t *timer);
struct tm* localtime(const time_t * timer);
For C++ on Windows look at the Windows time functions, in particular GetSystemTime.
Just to add, GetSystemTime gives you UTC time, while to get the TimeZone adjusted time you need to use GetLocalTime.
One more difference between WinBase time function (through windows.h) compared to time.h functions is windows time functions are reliable all the way back to 1601, while time.h is only till 1900 onwards. I am not sure if that is something which you need to consider.
time()
But also see localtime and asctime for display
Here's what I ended up using (nowtm is populated with current system time):
time_t rawtime=time(NULL);
tm* nowtm = gmtime(&rawtime);
where tm is defined:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
I wrote quite similar answer a few minutes ago. You can use chrono available in C++11.
http://en.cppreference.com/w/cpp/chrono/time_point