ZTimeConv

Downloads

Description

ZTimeConv is a class for converting from/to different time format that can be found in C/C++ on Windows.
  The problem with this formats is that they have different resolution and ranges so it is not always possible to convert. But as long as you work with times that are not to long in the future or the past this should not be a problem.



 

Time Formats

 

time_t

This is the most common format, and is the format that are most suitable if you write code for multiple platforms since time_t is supported in standard c library.
Is is a normally a 32bit signed long value that have existed since ANSI C.
It is also called unixtime or unix-epoch time. time_t is the number of seconds since 1-Jan-1970 00:00:00. The problem with 32bit time_t is that it will rollover in 19-Jan-2038. Because at 19-Jan-2038 03:14:07 the value of a time_t will be "2 147 483 647" and the after another second the value will roll over and we get the value "-2 147 483 648" ( that represent the lowest value we can store and that date is 13-Dec-1901 20:45:52 ). So to avoid this "soon" to come Y2k38-Bug a 64bit time_t have been started to be used instead. Expending time_t to 64bit will make the time_t last until the year 292 000 000 000. Which should be enough since it is longer then the estimated time the universe will survive.

On windows you can use __time64_t to use the 64bit time_t value. And in Visual Studio 2005 (and newer) time_t is change to use __time64_t by default. You can force the compile to use the 32bit variation instead by defining _USE_32BIT_TIME_T.

You can also use __time32_t or __time64_t types to specify what type of time_t variant you want.

So to summerrise.
time_t (32bit) have a time range from 13-Dec-1901 20:45:52 to 19-Jan-2038 03:14:07.
time_t (64bit) have a time range from -292 Billion to 292 Billion year in the future.

 

DOS DateTime

This is a format that was used back in the MSDOS days. It is using a 16bit value for the year and a 16bit value for the time. The bit in the 16bit value are divided up so for year bit 0-4 is day of month , 5-8 month and so on. This two 16bit values are often put together in a 32bit unsigned value.

DOS DateTime has a range of 1-Jan-1980 to 31-Dec-2099 but the seconds resolution is divided by 2.
  And this might have an impact on how correct an value can be converted when converting to/from DosTime, the seconds part of the DOSTime might be off by 1 second. This format is rarely used anymore, and is only used for backward compatibility reason.

 

FileTime

a FileTime is a 64bit values that represents the number of 100-nonosecond intervals that have elapsed since 00:00:00.000 1-Jan-1601 (UTC).

This value is stored in the FILETIME structure that containing two 32bit DWORD, one for low and one for the high part of the datetime.

When you get a FILETIME from the system API it will be in system time (UTC). Since the filetime on  NTFS filesytem time is recorded in UTC.
  FAT record time in local time so when getting time from an fileitem that time is converted into system time

 

SystemTime

The SYSTEMTIME is a structure that represents a date and time using individual member variables for the month, day, year, weekday, hour, minute, second, and millisecond.

 

The ZTimeConv class

All functions in the CZTimeConv class are static and the entire class are in the header file, so you only need to add a include statement for it.

This is all the conversion functions that are accessible from the CZTimeConv.

// To SystemTime
BOOL ToSystemTime( SYSTEMTIME* pSystemTime , const FILETIME* pFileTime )
BOOL ToSystemTime( SYSTEMTIME* pSystemTime , const __time32_t dwTime_t )
BOOL ToSystemTime( SYSTEMTIME* pSystemTime , const __time64_t dwTime_t )
BOOL ToSystemTime( SYSTEMTIME* pSystemTime , const WORD dosDate , const WORD dosTime )

// To FileTime
BOOL ToLocalFileTime( FILETIME* pLocalFileTime , const FILETIME* pFileTime )
BOOL ToLocalFileTime( FILETIME* pLocalFileTime , const SYSTEMTIME* st )
BOOL ToLocalRelFileTime( FILETIME* pLocalFileTime , const FILETIME* pFileTime )

BOOL FromFileTimeLocal( FILETIME* pFileTime  ,const FILETIME* pLocalFileTime  )
BOOL FromFileTimeLocalRel( FILETIME* pFileTime  ,const FILETIME* pLocalFileTime  )

BOOL ToFileTime( FILETIME* pFileTime , const SYSTEMTIME* pSystemTime )
BOOL ToFileTime( FILETIME* pFileTime , const __time32_t Time_t )
BOOL ToFileTime( FILETIME* pFileTime , const __time64_t Time_t )
BOOL ToFileTime( FILETIME* pFileTime , const double variantTime )
BOOL ToFileTime( FILETIME* pFileTime , const WORD dosDate , const WORD dosTime )

int64  FileTimeToINT64(FILETIME* ft)
void INT64ToFileTime(FILETIME* ft , __int64 nValue)

// To DosTime
BOOL ToDosDateTime( DWORD* pDateTime , const __time32_t dwTime_t )
BOOL ToDosDateTime( WORD* pDate , WORD* pTime , const FILETIME* pFileTime )
BOOL ToDosDateTime( DWORD* pDosDateTime , const SYSTEMTIME* st )
BOOL ToDosDateTime( WORD* pDate , WORD* pTime , const SYSTEMTIME* st )

void DosTimeDWORD2WORD( WORD* pDate , WORD* pTime , DWORD dosDateTime )
void DosTimeWORD2DWORD( DWORD* pDosDateTime , WORD Date , WORD Time )
BOOL ToDosDateTime( DWORD* pDateTime , const FILETIME* pFileTime )

// To Time_t
BOOL ToTime_t( __time32_t* pdwTime_t , const FILETIME* pFileTime )
BOOL ToTime_t( __time64_t* pdwTime_t , const FILETIME* pFileTime )
BOOL ToTime_t( __time32_t* pdwTime_t , const SYSTEMTIME* pSystemTime )
BOOL ToTime_t( __time64_t* pdwTime_t , const SYSTEMTIME* pSystemTime )
BOOL ToTime_t( __time32_t* pdwTime_t , const DWORD dosDateTime )
BOOL ToTime_t( __time32_t* pdwTime_t , const WORD dosDate , const WORD dosTime )
BOOL ToTime_t( __time64_t* pdwTime_t , const DWORD dosDateTime )
BOOL ToTime_t( __time64_t* pdwTime_t , const WORD dosDate , const WORD dosTime )

// UTC time_t to Local time_t
 __time32_t Time_tToLocalTime_t( const __time32_t Time_t )
 __time64_t Time_tToLocalTime_t( const __time64_t Time_t )
 __time32_t Time_tToLocalRelTime_t( const __time32_t Time_t )
 __time64_t Time_tToLocalRelTime_t( const __time64_t Time_t )
 __time32_t LocalTime_tToUTCTime_t( const __time32_t Time_t )
 __time64_t LocalTime_tToUTCTime_t( const __time64_t Time_t )

 

Example

Convert a time_t time into a filetime

 time_t ttTime = 1213288048;
 FILETIME ft;
 CZTimeConv::ToFileTime( &ft , ttTime );
 

For more example on how to use this class, download the source for the development tool TimeConverter.

Tagged:  

( Try Multi Commander, A file manager for professionals )

Copyright (c) 2015 - Mathias Svensson