I wrote some datetime conversion functions:
/ / =========================================================================================================================================================================================== ==============================
// File Name: TimeUtil.hpp
// Content Description:
//
// Writer: iYFT
// e-mail: ≤ft@163.com
//
// Create a date:
2004
Year 07
Month 01
day
10:52:54
// Modify date:
2004
Year 09
Month 13
day
15:08:35
/ / -------------------------------------------------------------------------------------------- -----------------------------
//
// Modify the record:
//
//
//
/ / =========================================================================================================================================================================================== =============================
/ / =========================================================================================================================================================================================== ==============================
/ / Guaranteed that the source file will only be compiled once
/ / -------------------------------------------------------------------------------------------- -----------------------------
#iF _MSC_VER> = 1000
/ / The source file will only be compiled once
#pragma overce
#ENDIF / / _MSC_VER> = 1000
#ifndef __HPP_timeUtil_HPP__
#define __hpp_timeutil_HPP __ // ============================================= =======================================================================================================================================================
#define DTM_DATE_LENGTH 10
#define dtm_datetime_length 20
/ / Returns the current local time zone (in seconds, because some time zones are decimal)
// Beijing Time Zone 08: 00 = 8 * 60 * 60 = 28800
// Tehran time zone 03: 30 = 3 * 60 * 60 30 * 60 = 12600
// Hawaii time zone is -10: 00 = -10 * 60 * 60 = -36000
Static long dtm_gettimezone (out time_zone_information ** ppzt = NULL)
{
Static time_zone_information tz;
Static DWORD NRESULT = 0;
Static bool binited = false;
IF (binited == false)
{
Switch (: getTimezoneInformation (& tz))
{
Case Time_ZONE_ID_INVALID:
NRESULT = 0;
Break;
DEFAULT:
Nresult = ((tz.bias tz.standardbias) * -60);
}
BINITED = TRUE;
}
IF (PPZT! = NULL)
{
* ppzt = & tz;
}
Return Nresult;
}
// Return to the current local time
Static DWORD DTM_GETLOCALECURRENTDATE ()
{
SystemTime T; :: getLocalTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
Return NDATE;
}
// Return to the current local time
Static DWORD DTM_GETLOCALECURRENTDATE (OUT LPTSTR SDATE)
{
SystemTime T; :: getLocalTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
LPTSTR SPOS = SDATE;
INT2T (SPOS, 4, T. Wyear, 4); SPOS = 4;
* SPOS = __t ('-');
INT2T (SPOS, 2, T.Wmonth, 2); SPOS = 2; * SPOS = __t ('-');
INT2T (SPOS, 2, T. Wday, 2); SPOS = 2;
* SPOS = __t ('/ 0');
Return NDATE;
}
// Return to the current local time
Static void DTM_GETLOCALECURRENTSTRING (OUT LPTSTR SDATE)
{
SystemTime T; :: getLocalTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
LPTSTR SPOS = SDATE;
INT2T (SPOS, 4, T. Wyear, 4); SPOS = 4;
* SPOS = __t ('-');
INT2T (SPOS, 2, T.Wmonth, 2); SPOS = 2;
* SPOS = __t ('-');
INT2T (SPOS, 2, T. Wday, 2); SPOS = 2;
* SPOS = __t ('');
INT2T (SPOS, 2, T.WHOUR, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.Wminute, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.WSecond, 2); SPOS = 2;
* SPOS = __t ('/ 0');
}
/ / Return to the current standard time
// Note: Greenwich standard time and local time are sometimes poor, Beijing is 8 hours
Static DWORD DTM_GETCURRENTDATE ()
{
SystemTime T; :: getSystemTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
Return NDATE;
}
/ / Return to the current standard time
// Note: Greenwich standard time and local time are sometimes poor, Beijing is 8 hours
Static DWORD DTM_GETCURRENTDATE (OUT LPTSTR SDATE)
{
SystemTime T; :: getSystemTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
LPTSTR SPOS = SDATE;
INT2T (SPOS, 4, T. Wyear, 4); SPOS = 4;
* SPOS = __t ('-');
INT2T (SPOS, 2, T.Wmonth, 2); SPOS = 2;
* SPOS = __t ('-');
INT2T (SPOS, 2, T. Wday, 2); SPOS = 2;
* SPOS = __t ('/ 0');
Return ndate;}
/ / Return to the current standard time
// Note: Greenwich standard time and local time are sometimes poor, Beijing is 8 hours
Static void DTM_GetcurrentString (Out LPTSTR SDATE)
{
SystemTime T; :: getSystemTime (& T);
DWord ndate = t.wyear * 10000 t.wmonth * 100 t.wday;
LPTSTR SPOS = SDATE;
INT2T (SPOS, 4, T. Wyear, 4); SPOS = 4;
* SPOS = __t ('-');
INT2T (SPOS, 2, T.Wmonth, 2); SPOS = 2;
* SPOS = __t ('-');
INT2T (SPOS, 2, T. Wday, 2); SPOS = 2;
* SPOS = __t ('');
INT2T (SPOS, 2, T.WHOUR, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.Wminute, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.WSecond, 2); SPOS = 2;
* SPOS = __t ('/ 0');
}
// Return the datetime string; format is [YYYY-MM-DD HH: MM: SS]
Static void DTM_GETSTRING (in SystemTime T, Out LPTSTR STIME)
{
LPTSTR SPOS = STIME;
INT2T (SPOS, 4, T. Wyear, 4); SPOS = 4;
* SPOS = __t ('-');
INT2T (SPOS, 2, T.Wmonth, 2); SPOS = 2;
* SPOS = __t ('-');
INT2T (SPOS, 2, T. Wday, 2); SPOS = 2;
* SPOS = __t ('');
INT2T (SPOS, 2, T.WHOUR, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.Wminute, 2); SPOS = 2;
* SPOS = __t (':');
INT2T (SPOS, 2, T.WSecond, 2); SPOS = 2;
* SPOS = __t ('/ 0');
}
// Return the time string; format is [x points x second]
Static void DTM_GetTimeText (in dword ntime, out lptstr station)
{
IF (NTIME <1000 * 60)
{
DWORD NINTEGRAL = NTIME / 1000;
DWORD NDECIMAL = NTIME% 1000;
NDECIMAL = (NDECIMAL * 100) / 1000;
INT2T (STIME, 16, NINTEGRAL);
Uint ncount = lptstrlen (station);
IF (nDecimal> 0)
{
Stime [ncount ] = __t ('.');
IF (nDecimal> = 10)
INT2T (Stime Ncount, 2, (NTIME% 1000 5) / 10, 2);
Else
INT2T (Stime NCOUNT, 2, NDECIMAL, 2);
Ncount = LPTSTRLEN (STIME NCOUNT);
}
Stime [ncount ] = __t ('');
#ifdef unicode
Stime [ncount ] = __w ('second');
#ELSE
Stime [ncount ] = (acha) 0xc3; // second = 0xc3eb
Stime [ncount ] = (achar) 0xeb; // second = 0xc3eb
#ENDIF
Stime [ncount] = TCHR_NULL;
}
Else
{
INT2T (STIME, 16, NTIME / 1000/60);
Uint ncount = lptstrlen (station);
Stime [ncount ] = __t ('');
#ifdef unicode
Stime [ncount ] = __w ('points');
#ELSE
Stime [ncount ] = (acha) 0xB7; // 分 = 0xB7D6
Stime [ncount ] = (acha) 0xD6; // second = 0xB7D6
#ENDIF
NTIME = NTIME% (1000 * 60);
DWORD NINTEGRAL = NTIME / 1000;
DWORD NDECIMAL = NTIME% 1000;
IF (Nintegral> 0 || NDECIMAL> 0)
{
Stime [ncount ] = __t ('');
NDECIMAL = (NDECIMAL * 100) / 1000;
INT2T (STIME NCOUNT, 16, NINTEGRAL);
Ncount = LPTSTRLEN (STIME NCOUNT);
IF (nDecimal> 0)
{
Stime [ncount ] = __t ('.');
IF (nDecimal> = 10)
INT2T (Stime Ncount, 2, (NTIME% 1000 5) / 10, 2);
Else
INT2T (Stime NCOUNT, 2, NDECIMAL, 2);
Ncount = LPTSTRLEN (STIME NCOUNT);
}
Stime [ncount ] = __t ('');
#ifdef unicode
Stime [ncount ] = __w ('second');
#ELSE
Stime [ncount ] = (acha) 0xc3; // second = 0xc3ebstime [ncount ] = (acha) 0xeb; // second = 0xc3eb
#ENDIF
}
Stime [ncount] = TCHR_NULL;
}
}
// Convert UNIX TIME TIME_T to SystemTIME
Static void DTM_CONVERTTIME (in Time_t T, Out LpsystemTime PST)
{
Filetime ft;
Longlong LL;
LL = INT32X32TO64 (T, 10000000) 116444736000000000;
ft.dwlowdateTime = (DWORD) LL;
ft.dwhighdateTime = (DWORD) (LL >> 32);
:: FileTimetosystemTime (& ft, pst);
}
// Convert UNIX TIME TIME_T to FILETIME
Static void DTM_CONVERTTIME (in time_t t, out lpfiletime PFT)
{
Longlong LL;
LL = INT32X32TO64 (T, 10000000) 116444736000000000;
PFT-> DWLOWDATETIME = (DWORD) LL;
PFT-> dwhighdatetime = (dword) (LL >> 32);
}
// convert FileTime to Unix Time Time_T
Static Time_t DTM_CONVERTTIME (in Const LpsystemTime PST)
{
Filetime ft;
:: SystemTimetOfiletime (PST, & ft);
Longlong ll = ft.dwhighdatetime;
LL << = 32;
LL | = ft.dwlowdatetime;
LL - = 116444736000000000;
Return (TIME_T) (LL / 10000000);
}
// convert FileTime to Unix Time Time_T
Static Time_t DTM_CONVERTTIME (in Const LPFILetime PFT)
{
Longlong LL = PFT-> DWHIGHDATETIME;
LL << = 32;
LL | = PFT-> DWLOWDATETIME;
LL - = 116444736000000000;
Return (TIME_T) (LL / 10000000);
}
#ENDIF / / #1ndef __HPP_TIMEUTIL_HPP__