-2

I want to convert UTC timestamp in epoch time seconds considering DST in C language. I am doing development on VS 2005 as this is legacy product. I gone through various websites but it is not useful.

We can do this by boost library but it is not recommended.

I am passing input as "2017-03-12T01:00:00Z" i.e. UTC timestamp, and I want epoch time of it i.e. in seconds. When I convert this epoch time back to UTC time, it should give me exact result as input timestamp.

Lennart
  • 9,657
  • 16
  • 68
  • 84
Prashant
  • 11
  • 4

1 Answers1

0

You can make use of mktime().

According to the man page, you have to parse the input and fill up the structure tm, defined as

struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

and then, make a call to mktime() as

time_t result;
result = mktime(&t);

Point to note: Please read the linked manual and check for the applicable values of the structure members, especially

tm_mon

The number of months since January, in the range 0 to 11.

and

tm_year

The number of years since 1900.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. – Prashant Nov 16 '18 at 08:41
  • @Prashant Please show the code that is not working. – Sourav Ghosh Nov 16 '18 at 08:48
  • When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST" – Prashant Nov 16 '18 at 08:50
  • @Prashant I dont see that, check [this](https://onlinegdb.com/HyzIP-h6Q). – Sourav Ghosh Nov 16 '18 at 09:01
  • Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference. – Prashant Nov 16 '18 at 09:37
  • @Prashant If you already know the offset, just adjust it. However, `gmtime()` returns UTC - so not sure if the system timezone setting should affect that. – Sourav Ghosh Nov 16 '18 at 09:48
  • @SouravGhosh : [`mktime`](https://en.cppreference.com/w/c/chrono/mktime) interpretes the `struct tm` as local time, so that will depend on your system's timezone. – Sander De Dycker Nov 16 '18 at 10:01