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. 中兴F460 V3.0版本获取超级用户密码
  2. 效率低下的Split函数,用VBS的人你伤不起
  3. C语言strlcpy函数的实现
  4. 用VBS监视进程创建和删除
  5. 人人网状态及评论导出工具

留下回复