Applications of the coledatetime type

Source: Internet
Author: User
Tags time and date

Use the coledatetime class
1) obtain the current time.
Ctime time;
Time = ctime: getcurrenttime ();
2) obtain the time element.
Int year = time. getyear ();
Int month = time. getmonth ();
Int day = time. getday ();
Int hour = time. gethour ();
Int minute = time. getminute ();
Int second = time. getsecond ();
Int dayofweek = time. getdayofweek ();
3) obtain the time interval.
Ctimespan timespan (0, 0, 1, 0); // days, hours, minutes, seconds
Timespan = ctime: getcurrenttime ()-time;
4) converts the time to a string.
Cstring sdate, stime, selapsed time;
Sdate = time. Format ("% m/% d/% Y"); // ex: 12/10/98
Stime = time. Format ("% H: % m: % s"); // ex: 9:12:02
Selapsed time = timespan. Format ("% d: % H: % m: % s"); // % d is total elapsed days
5) convert the string to time.
Cstring sdatetime;
Int nyear, nmonth, ndate, nhour, Nmin, nsec;
Sscanf (sdatetime, "% d-% d: % d", & nyear, & nmonth, & ndate, & nhour, & Nmin, & nsec );
Ctime stime (nyear, nmonth, ndate, nhour, Nmin, nsec );
For more information about the time format, see strftime In the MFC documentation.

Use the coledatetime class
1) Get a day of the year.
Coledatetime datetime;
Datetime = coledatetime: getcurrenttime ();
Int dayofyear = datetime. getdayofyear ();
2) read time from text strings.
Coledatetime datetime;
Datetime. parsedatetime ("12:12:23 27 January 93 ");
3) obtain the time interval.
// For example, calculate the date difference
Coledatetime begin_date (1970, 1, 1, 0, 0 );
Coledatetime end_date (1990, 1, 1, 0, 0, 0 );
Coledatetimespan timespan; // calculate the time difference
Timespan = end_date-begin_date;
Long expi_date = timespan. getdays ();

Description
■ Ctime and coledatetime have almost the same functions. However, coledatetime allows users to get a day of the year (a good way to create a Julian Date) and analyze a time text string.
■ Compared with ctime, coledatetime has the advantage that it supports DWORD variables. The number of digits used by coledatetime is twice that of the Double Floating Point. Since ctime is only a simple calculation of the number of seconds passed since January 1, January 1, 1970, it will reach 2037 by January 1, 4294967295, and thus cannot be used any more. Instead, coledatetime is
Floating Point number, which indicates the number of days since January 1, December 30, 1900 (the hour is the decimal part of the day.

Supplement:

1. coledatetime
. Net has the datetime class used to process the date and time, which is very convenient. VC can use the coledatetime class of the ATL library. It is also good to use:
First, include the relevant header file:
# Include <atlcomtime. h>

Initialize the coledatetime instance:
Coledatetime date_time;
Date_time.setdate (year, month, day );
Date_time.settime (hour, minute, second );

Coledatetime is overloaded with operators such as +,-, + =,-=, and so on, so that the time and date can be easily regressed. The operation object is coledatetimespan, for example:
Date_time + = coledatetimespan (day, hour, minute, second); // day, hour, minute, second

Coledatetime can also use a valid time string parse to initialize the instance itself:
Date_time.parsedatetime (cstring ("09:00:00 "));

In turn, coledatetime can be formatted into a time-Date Format String cstring:
Date_time.format (L "% Y % m % d"); // year, month, and day. Note that M indicates minute in uppercase.

Ii. cstring
Cstring is easy to use, but some usage won't be forgotten for a long time. In order to quickly find the answer through experience next time, write something.
Cstring has a format member that can be converted into a cstring. The parameter is similar to C's printf usage, for example:
Cstring STR;
Str. format ("% S % d % 02i", cstring ("example"), 9, 1); // The first parameter string, the second signed decimal number, the third is 2 in width. If not, the value is in the signed decimal number filled with 0,

Therefore, the STR value after the operation is:
Expample901

Common ctime coledatetime operations and comparison (to convert)

 

1) obtain the current time.
Datetime = coledatetime: getcurrenttime ();
Ctime and coledatetime have almost the same functions.
Compared with ctime, coledatetime supports DWORD variables.
The number of digits used by coledatetime is twice that of the Double Floating Point. Since ctime is only a simple calculation of the number of seconds passed since January 1, January 1, 1970, so it will reach 4 2 9 4 9 6 7 2 9 5 in 2 0 3 7 years, thus it cannot be used again.
// Cstring ---> coledatetime
Colevariant vtime (strcstring); coledatetime time4 = vtime;
// Ctime ---> time_t
Coledatetime time3 (time2 );
// Time_t ---> coledatetime.

Cstring coledatetime ctime mutual conversion
Strcstring = "6:24:37"; // cstring ---> coledatetime
Colevariant vtime (strcstring );
Vtime. changetype (vt_date );
Coledatetime time4 = vtime;

Coledatetime time1 (1977,4, 16,2,); // coledatatime ---> ctime
Systemtime receivime;
Varianttimetosystemtime (time1, & systime );
Ctime TM (systime );

Time_t time2 = TM. gettime (); // ctime ---> time_t
Coledatetime time3 (time2); // time_t ---> coledatetime

Cstring STR = tdate. Format (_ T ("% Y-% m-% d "));

 

I used the red part to convert the time type to the cstring type. I really want to convert the time type to the long type. For calculation. However, no other better and more direct methods were found. Therefore, we can switch between them indirectly. If any friend has a better solution, please leave a message for me... Thank you.

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.