Time convert on c++

Background

UTC: Coordinated Universal Time.

GMT: Greenwich Mean Time.

FILETIME:

LOCALTIME:

SYSTEMTIME:

STRUCTURE/DEFINITION

For windows

SYSTEMTIME structure. The time is either in coordinated universal time (UTC) or local time, depending on the function that is being called.

typedef struct _SYSTEMTIME {   WORD wYear;   WORD wMonth;          // based on 1(Jan)  WORD wDayOfWeek;      // 0 for sunday, 1 for monday, … 6 for saturday  WORD wDay;            // based on 1  WORD wHour;           // from 0 to 23  WORD wMinute;         // from 0 to 59  WORD wSecond;         // from 0 to 59  WORD wMilliseconds;   // from 0 to 999} SYSTEMTIME;

FILETIME structure. This value represents the number of 100-nanosecond units since the beginning of January 1, 1601.

typedef struct _FILETIME {   DWORD dwLowDateTime;   DWORD dwHighDateTime; } FILETIME, FAR *LPFILETIME;

ULARGE_INTEGER Union

typedef union _ULARGE_INTEGER {      struct {            DWORD LowPart;            DWORD HighPart;      };      struct {            DWORD LowPart;            DWORD HighPart;      } u;      ULONGLONG QuadPart;} ULARGE_INTEGER,   *PULARGE_INTEGER;

Note: As the structure shown above.FILETIME –> ULARGE_INTEGER: Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER or __int64 value because it can cause alignment faults on 64-bit Windows. You should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member.ULARGE_INTEGER –> FILETIME: Cast pointer to ULARGE_INTEGER to FILETIME or copy the LowPart and HighPart members into the FILETIME** structure.

time_t (__int64 or long integer)
struct tm {  int  tm_sec;        // from 0 to 59  int  tm_min;        // from 0 to 59  int  tm_hour;       // from 0 to 23  int  tm_mday;       // from 1 to 31  int  tm_mon;        // from 0 to 11  int  tm_year;       // Indicates the number of years since 1900.   int  tm_wday;       // 0 for sunday, 6 for saturday  int  tm_yday;       // from 0 to 365   int  tm_isdst;};

Retrieve Function

From total seconds (clocks) to tm structure for local time, lval is the variable for total seconds.

time_t t(lval);struct tm  * pltm = localtime((const time_t*)&t);

From total seconds (clocks) to tm structure for gmt time, lval is the variable for total seconds.

time_t t(lval);struct tm * pgtm = gmtime((const time_t*)&t);cout<<asctime((const tm*)pgtm);

From tm structure to calendar value

time_t _t = mktime(pltm);




原文链接: https://www.cnblogs.com/rogerroddick/archive/2013/02/05/2892912.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/77642

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午6:04
下一篇 2023年2月9日 下午6:04

相关推荐