Windows日期时间与相互转化

标签: , , ,

在Windows中有各种不同的日期和时间格式,不幸的是你经常需要在它们之间转换以实现某些有用的功能,这意味着知道每种格式的属性是很重要的。

下面是一张关于标准日期和时间格式的简单表格:

Container

Domain

Format / Notes

Min Date

Resolution

SYSTEMTIME

Win32

Struct (16 bytes)

January 1, 1601

1 millisecond

FILETIME

Win32

Offset value (64bit unsigned int)

January 1, 1601

100 nanoseconds

VT_DATE

Win32 / OLE Automation

Offset value (64bit signed float), 0.0 is December 30, 1899.

January 1, 100 (1)

500 milliseconds (2)

FAT/MS-DOS

Win32

Struct (values packed into two 16bit ints)

January 1, 1980

2 seconds

time_t

CRT

Offset value (unsigned int)

January 1, 1970

1 second

tm

CRT

Struct (36 bytes (3))

January 1, 1900

1 second

相互转化

Windows时间:

 

SYSTEMTIME

FILETIME

VT_DATE

FAT/MS-DOS

SYSTEMTIME

 

SystemTimeToFileTime

SystemTimeToVariantTime

 

FILETIME

FileTimeToSystemTime

 

 

FileTimeToDosDateTime

VT_DATE

VariantTimeToSystemTime

 

 

VariantTimeToDosDateTime

FAT/MS-DOS

 

DosDateTimeToFileTime

DosDateTimeToVariantTime

 

CRT时间:

 

time_t

tm

time_t

 

gmtime

tm

mktime

 

SYSTEMTIME与time_t的转化:

void SystemTimeToTime_t(SYSTEMTIME *systemTime, time_t *dosTime)
{
    LARGE_INTEGER jan1970FT = {0};
    jan1970FT.QuadPart = 116444736000000000I64; // january 1st 1970
    LARGE_INTEGER utcFT = {0};
    SystemTimeToFileTime(systemTime, (FILETIME*)&utcFT);
    unsigned __int64 utcDosTime = (utcFT.QuadPart - jan1970FT.QuadPart)/10000000;
    *dosTime = (time_t)utcDosTime;
}

void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
{
    LARGE_INTEGER jan1970FT = {0};
    jan1970FT.QuadPart = 116444736000000000I64; // january 1st 1970
    LARGE_INTEGER utcFT = {0};
    utcFT.QuadPart = ((unsigned __int64)dosTime)*10000000 + jan1970FT.QuadPart;
    FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
}

参考链接:http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx

随机文章:

  1. VBS转EXE工具:ExeScript
  2. 三星官方Android 5.0 Lollipop系统Deodex化及刷Xposed
  3. VBS调用WMI遍历搜索硬盘文件并计数
  4. _get_osfhandle函数
  5. BAT批处理编辑器BatProject

留下回复