Boost logo

Boost :

Subject: Re: [boost] milliseconds UTC from discrete year, month, day, hour, minute, second
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2013-11-22 13:37:12

On Nov 22, 2013, at 5:59 AM, Bjorn Reese <breese_at_[hidden]> wrote:

> On 11/21/2013 08:30 PM, Victor Yankee wrote:
>> Can someone should me how to calculate the milliseconds since jan 1, 1970
>> in UTC, given separate
>> variables holding the year, month,day,hour,minute and seconds?
> std::mktime(your_time) * 1000

The precision of time_t (the return of mktime) is implementation defined. Additionally it should be noted that the input to mktime is in the local time zone, not UTC. And traditionally this solution won't take leap seconds into account (though it certainly might).

The same question was asked on SO, and I've provided an answer there:

There's two versions:

1. A really simple solution that neglects leap seconds.

2. A slightly more complicated solution that isn't fully coded in the answer that will take leap seconds into account.

The answers don't depend on implementation-defined properties, and can use either boost::chrono, or std::chrono to simplify the duration arithmetic. No explicit conversion factors are used, except to specify that there are 24 hours in a day.


Boost list run by bdawes at, gregod at, cpdaniel at, john at