The reciprocal transformation between time_t and date

Source: Internet
Author: User
Tags assert

The difference between the two:

time_t is an integral type that stores the time difference (seconds) (in UTC) to 1970-1-1 00:00:00

Data is a floating-point type, and the integer portion stores the time difference (days) 00:00:00 to 1899-12-30, and the fractional portion is stored minutes and seconds. (Btw:data can represent the time before the base point, time_t can not)

This algorithm just want to explain the conversion between the two is very easy, do not need MFC support, do not need to "faint", so the following two points do not consider:

A. Do not consider the time difference between local and UTC (This thing doesn't look like it, or it's time_t good, a time_t is enough, date has to know where it is used, otherwise there is no fart value)-------Since someone asked me what to do, Well, I'll take that into consideration.

B. Regardless of the error value (such as time_t (-1) is an error value), do not consider the failure of the conversion situation (because the scope of the two representations are different).

#include <ctime>

Inline double timet_to_lotus (time_t t)
{
Return t/(24*60*60) + 25569//t/(24*60*60) leaves only the number of days. time_t 1970/1/1 as the starting point, lotus in 1899/12/30 as the basis for 25,569 days
+ t% (24*60*60)/(60*60)/(24.0)//t% (24*60*60) leave only minutes and seconds, then/(60*60) when left, except 24 when converted to Lotus
+ t% (60*60)/(a)/(24.0*60.0)//t% (60*60) left only minutes, then/(60) left points, in addition to the 24*60 will be converted to Lotus points
+ t%60/(24.0*60.0*60.0); t% (60) will only leave a second, except 24*60*60 into lotus seconds

Return (T+timezone)/(24.0*60.0*60.0) + 25569; TimeZone is the time zone difference
}

Inline time_t Lotus_to_timet (double dt)
{
Return (time_t) ((dt-25569) * (24*60*60)-timezone + 0.5); +0.5 is what reason everybody must know. I wondered which SN made the format, and if there was no long long, it would rather be struct rather than double.
}

The following is the test code
#include <ATLComTime.h>
#include <ctime>
#include <cassert>

int main ()
{
time_t a = time (0);
COleDateTime B (Timet_to_lotus (a));
printf ("Current UTC%04d-%02d-%02d%02d:%02d:%02d\n", B.getyear (), B.getmonth (), B.getday (), B.gethour (), B.getm Inute (), B.getsecond ());

time_t C = Lotus_to_timet (B.M_DT);
ASSERT (c = = a);

return 0;
}

//////////////////////////////////////////////////////////////////

Mutual transformation between time_t and SYSTEMTIME (answer [Feveny] q)

#include <ctime>
#include <windows.h>

Inline SYSTEMTIME timet_to_systime (time_t t)
{
TM GM = *localtime (&t);

SYSTEMTIME st = {1900+gm.tm_year, 1+gm.tm_mon, Gm.tm_wday, Gm.tm_mday, Gm.tm_hour, Gm.tm_min, gm.tm_sec, 0};
Return St;
}

Inline time_t systime_to_timet (const systemtime& ST)
{
struct TM gm = {st.wsecond, St.wminute, St.whour, St.wday, St.wmonth-1, st.wyear-1900, St.wdayofweek, 0, 0};

Return Mktime (&AMP;GM);
}

The following is the test code
#include <ATLComTime.h>
#include <cassert>

int main ()
{
time_t a = time (0);
SYSTEMTIME B = Timet_to_systime (a);
time_t C = Systime_to_timet (b);

assert (A = = c);

COleDateTime d (a);
COleDateTime e (b);

assert (d = = e);

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.