C++ 时间

求以2000年1月1日0点0分为起始时间,到当前时间,换算成的秒数:

	SYSTEMTIME sysTime;
	struct tm basetime, nowtime;
	time_t tTime, tTimeBase; //time_t为64位整形__int64或长整形long
	
	GetSystemTime(&systime); //获取系统时间,以UTC表示

	nowtime.tm_year = systime.wYear-1900;
	nowtime.tm_mon = systime.wMonth-1;
	nowtime.tm_mday = systime.wDay;
	nowtime.tm_hour = sysTime.wHour;
	nowtime.tm_min = sysTime.wMinute;
	nowtime.tm_sec = sysTime.wSecond;
	nowtime.tm_isdst = 0;

	basetime.tm_year = 2000-1900;
	basetime.tm_mon = 0;
	basetime.tm_mday = 1;
	basetime.tm_hour = 0;
	basetime.tm_min = 0;
	basetime.tm_sec = 0;
	basetime.tm_isdst = 0;
	
	tTimeBase = mktime(&basetime); //将struct tm结构的时间转换为从1970.1.1 00:00:00至2000.1.1 00:00:00的秒数
	tTime = mktime(&nowtime); //将struct tm结构的时间转换为从1970.1.1 00:00:00至今的秒数

	//以2000年1月1日0点0分为起始日期,到当前时间,换算成秒数(4字节十六进制),高位在前
	DWORD dwTime = difftime(tTime, tTimeBase); //返回两个时间相差的秒数

头文件:WinBase.h,SYSTEMTIME定义:

typedef struct _SYSTEMTIME {
    WORD wYear; //The current year
    WORD wMonth; //The current month; January is 1 
    WORD wDayOfWeek; //The current day of the week; Sunday is 0, Monday is 1, and so on
    WORD wDay; //The current day of the month
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;

头文件:time.h,struct tm定义

struct tm {
        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 */
        };


找的资料,方便查看:

http://blog.sina.com.cn/s/blog_5669080b0100p2ox.html

http://blog.csdn.net/love_gaohz/article/details/6637625

http://blog.csdn.net/zzzzyu/article/details/44937835

http://www.cnblogs.com/renjiashuo/p/6913668.html





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值