1.类型 time_t中存储的是自1970年1月1日午时到现在的偏移量,以秒计时,因此Windows系统下多用此做时间计算。
typedef __time32_t time_t; /* time value */
typedef __time64_t time_t; /* time value */
#define _TIME_T_DEFINED /* avoid multiple def's of time_t */
typedef _W64 long __time32_t; /* 32-bit time value */
typedef __int64 __time64_t; /* 64-bit time value */
函数localtime, 会把传入的time_t时间转化为 tm类型的数据,并初始化为本地时间
struct tm * localTime(const time_t * timer);
struct tm *gmtime(const time_t * timer);
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
time_t mktime(struct tm * timeptr);
根据MSDN的解释,mktime会忽略tm_wday, tm_yday数据域.
5. 时区信息,可以通过下面的全局变量得到时区偏移,也可以通过当地时间和UTC时间的比较,得到时区偏移.
SimpleDateTime(int year, int month, int day);
SimpleDateTime(int year, int month, int day,
int hour, int minute, int second);
SimpleDateTime(int year, int month, int day, int timeZone);
SimpleDateTime(int year, int month, int day,
int hour, int minute, int second, int timeZone);
SimpleDateTime(const SimpleDateTime & right);
SimpleDateTime & operator=(const SimpleDateTime & right);
bool operator==(const SimpleDateTime & right) const;
bool operator!=(const SimpleDateTime & right) const;
SimpleDateTime::SimpleDateTime(void)
SimpleDateTime::SimpleDateTime(time_t time)
SimpleDateTime::SimpleDateTime(int year, int month, int day)
tm_time_.tm_year = year - 1900;
SimpleDateTime::SimpleDateTime(int year, int month, int day,
int hour, int minute, int second)
tm_time_.tm_year = year - 1900;
SimpleDateTime::SimpleDateTime(int year, int month, int day, int timeZone)
tm_time_.tm_year = year - 1900;
SimpleDateTime::SimpleDateTime(int year, int month, int day,
int hour, int minute, int second, int timeZone)
tm_time_.tm_year = year - 1900;
SimpleDateTime::SimpleDateTime(const SimpleDateTime & right)
SimpleDateTime::~SimpleDateTime(void)
SimpleDateTime & SimpleDateTime::operator=(const SimpleDateTime & right)
bool SimpleDateTime::operator==(const SimpleDateTime & right) const
return (time_ == right.time_);
bool SimpleDateTime::operator!=(const SimpleDateTime & right) const
void SimpleDateTime::currentTime(void)
SimpleDateTime SimpleDateTime::toLocalTime()
SimpleDateTime dateTime((time_ + timeZone_ * 60 * 60));
void SimpleDateTime::calcTimeZone()
// 由于使用系统变量有些问题,所以使用时间偏移来确定时区值
long tz = local_time - gm_time;
int tzHour = (int)(tz / (60 * 60));
time_t SimpleDateTime::toTime_t() const
void SimpleDateTime::formatTime()
int SimpleDateTime::getYear() const {
return tm_time_.tm_year + 1900;
int SimpleDateTime::getMonth() const {
int SimpleDateTime::getDay() const {
int SimpleDateTime::getHour() const {
int SimpleDateTime::getMinute() const {
int SimpleDateTime::getSecond() const {
int SimpleDateTime::getWeek() const {
int SimpleDateTime::getIsdst() const {
int SimpleDateTime::getTimeZone() const {
void print_dateTime(SimpleDateTime dt)
std::cout << "DateTime : " << dt.getYear()
<< "-" << dt.getMonth() << "-" << dt.getDay()
<< " " << dt.getHour() << ":" << dt.getMinute() << ":" << dt.getSecond()
<< " DST(" << dt.getIsdst() << ") TimeZone(" << dt.getTimeZone() << ")"
<< " Week(" << dt.getWeek() << ")"
std::cout << "GM time " << std::endl;
SimpleDateTime localTime = dt.toLocalTime();
std::cout << "LocalTime " << std::endl;
SimpleDateTime time1(2010,4,5);
std::cout << "2010-4-5 " << std::endl;
SimpleDateTime time2(2010,4,5,12,15,33);
std::cout << "2010,4,5,12,15,33 " << std::endl;
E:/root/workspace/MyWork/bin>TimerCalc.exe
DateTime : 2010-4-4 8:4:59 DST(0) TimeZone(8) Week(0)
DateTime : 2010-4-4 16:4:59 DST(0) TimeZone(8) Week(0)
DateTime : 2010-4-5 0:0:0 DST(0) TimeZone(8) Week(1)
DateTime : 2010-4-5 12:15:33 DST(0) TimeZone(8) Week(1)
感谢原作者:http://blog.youkuaiyun.com/lightlater/article/details/5449510