unix time 相关

来源:互联网 发布:你见过的神人知乎 编辑:程序博客网 时间:2024/05/19 21:01

struct tm

<ctime>
Time structure
Structure containing a calendar date and time broken down into its components.

The structure contains nine members of type int, which are (in any order):
123456789
int tm_sec;int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;


The meaning of each is:
MemberMeaningRangetm_secseconds after the minute0-61*tm_minminutes after the hour0-59tm_hourhours since midnight0-23tm_mdayday of the month1-31tm_monmonths since January0-11tm_yearyears since 1900tm_wdaydays since Sunday0-6tm_ydaydays since January 10-365tm_isdstDaylight Saving Time flagThe Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.

type

time_t

<ctime>
Time type
Type capable of representing times and support arithmetical operations.

This type is returned by the time function and is used as parameter by some other functions of the <ctime> header.

It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. This is due to historical reasons, since it corresponds to a unix timestamp, but is widely implemented in C libraries across all platforms.


time_t rawtime;

struct tm * timeinfo;

time ( &rawtime );

timeinfo = localtime ( &rawtime );

std::cout<<"today is "<<timeinfo->tm_wday<<std::endl;

timeinfo->tm_year = lexical_cast<int>(ymd[0]) - 1900;

timeinfo->tm_mon = lexical_cast<int>(ymd[1]) - 1;

timeinfo->tm_mday = lexical_cast<int>(ymd[2]);

 /*  call mktime: timeinfo->tm_wday will be set */

 mktime ( timeinfo );

std::cout<<"that day is "<<timeinfo->tm_wday<<std::endl;