0

I want to construct a struct tm with Australia/Sydney timezone, so I first use:

setenv("TZ","Australia/Sydney",1);
tzset()

then I set the struct tm as:

struct tm _tm;
_tm.tm_sec = 0;
_tm.tm_min = 45;
_tm.tm_hour = 7;
_tm.tm_mday = 18;
_tm.tm_mon = 8;
_tm.tm_year = 114;

This should set to Australia time 2014/09/18 7:45:00 then I call:

time_t other_tm = mktime(&_tm);

After this call both other_tm and _tm pointed to 6:45am of Australia time! The reason is other_tm has value 1410986700 which you can verify from Epoc converter it indeeds pointed to 6:45am, anybody has an idea why?

user2426361
  • 387
  • 1
  • 7
  • 13

1 Answers1

0

Using this cross-platform, free, open-source C++11/14 <chrono>-based library, here is a modern way to get the desired result:

#include "tz.h"
#include <iostream>

int
main()
{
    using namespace std::chrono_literals;
    using namespace date;
    auto zt = make_zoned("Australia/Sydney", local_days{2014_y/9/18} + 7h + 45min);
    std::cout << zt.get_sys_time().time_since_epoch().count() << '\n';
}

This outputs:

1410990300

(which is 1410986700 + 3600)

This library is much easier and safer to use than the antiquated C API for time handling.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577