venkysmarty
venkysmarty

Reputation: 11431

about posix ftime in C++

I am looking in to the code about getting utc time.

static const long long SECSEPOCHS = 11644473600ll;
static const long long SECSto100nanosecs = 10000000ll; 
static const long long MSECSto100nanosecs = 10000ll;
longlong mytime;
struct timeb now;

ftime(&now);
mytime = now.time;
mytime += SECSEPOCHS 
mytime *= SECSto100nanosecs;
mytime += now.millitm * MSECSto100nanosecs;

I have following questions about above code. What is the magic number "11644473600ll", how we got that number, and why we are adding this number to now.time that is returned by ftime. What is epcoh mean here?

Thanks

Upvotes: 1

Views: 1064

Answers (3)

Tayyab
Tayyab

Reputation: 10631

Epoch refers to the reference time that is used to calculate further events. For example if I decide that 1st Jan 2000 is the reference time (EPOCH) so whenever we I say "1 year past". It will mean that it is 1st Jan 2001 as our reference was year 2000.

Windows NT time is specified as the number of 100 nanosecond intervals since January 1, 1601. UNIX time is specified as the number of seconds since January 1, 1970. There are 134,774 days (or 11,644,473,600 seconds) between these dates.

Upvotes: 2

Erik
Erik

Reputation: 91270

The epoch is Midnight UTC, Jan 1 1970, the base for unixtime. The magic number is used to convert in between unixtime and microsoft's filetime which starts at Midnight UTC, Jan 1 1601.

Your code does the following:

ftime(&now); 
mytime = now.time;  // Get unixtime right now
mytime += SECSEPOCHS; // Adjust to seconds since microsoft's 1601 epoch
mytime *= SECSto100nanosecs; // Convert to 100-nanosecond units
mytime += now.millitm * MSECSto100nanosecs; // Add the millisecond part of the unixtime

The result is a value which is suitable for MS's FILETIME structure.

Upvotes: 2

gpvos
gpvos

Reputation: 2802

ftime returns Unix time, which is counted in seconds since 1 January 1970 00:00:00 UTC. The rest of the code converts it to a Windows NT file time, which is counted in units of 100 nanoseconds since 1 January 1601 00:00:00 UTC. The "epoch" is the "start of time", and 11644473600 is the Unix epoch expressed in seconds since the NT filetime epoch.

Upvotes: 1

Related Questions