欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

北京时间、时间戳互转

程序员文章站 2022-05-02 17:10:07
...
/*----------------------------------------------------------------
// 文件名:test.cpp;
// 文件功能描述:实现北京时间与时间戳的互转;
// author:杨洲(aaa@qq.com);
// 时间:2018/4/25
//----------------------------------------------------------------*/
#include <stdio.h>

typedef struct NetTime {
	unsigned int year;
	unsigned int month;
	unsigned int day;
	unsigned int hour;
	unsigned int minute;
	unsigned int second;
}NetTime;

//日期转时间戳;
time_t  NetTime2Stamp(NetTime& dt) {
	auto isLeap = [](int _year)->bool {
		return (_year % 4 == 0) &&\
			(_year % 100 != 0) ||\
			(_year % 400 == 0);
	};
	int _days = 0;
	//total year day;
	for (int _index = 1970; _index < dt.year;++_index) {
		_days += isLeap(_index) ? 366 : 365;
	}
	//total month day;
	for (int _index = 1; _index <= dt.month;_index++) {
		if (1==dt.month) {
			break;
		}
		if (_index==dt.month){
			break;
		}
		if (1==_index||\
			3 == _index ||\
			5 == _index || \
			7 == _index || \
			8 == _index || \
			10 == _index || \
			12 == _index) {
			_days += 31;
		}else if (4 == _index || \
			6 == _index || \
			9 == _index || \
			11 == _index) {
			_days += 30;
		}else if (2==_index) {
			_days += isLeap(dt.year)? 29 : 28;
		}
	}
	_days += dt.day - 1;
	return _days * 24 * 60 * 60 + \
		dt.hour * 60 * 60 + \
		dt.minute * 60 + \
		dt.second - 8 * 60 * 60;
}

//时间戳转时间;
NetTime Stamp2NetTime(time_t _stamp) {
	NetTime _netTime = { 0x00 };
	_stamp += 8 * 60 * 60;
	int _remainDays = (_stamp)/(24*60*60)+1;
	int _remainSeconds = (_stamp) % (24 * 60 * 60);
	int _nYear = 1970;
	int _nMonth = 1;
	int _nDay = 1;
	int _nHour, _nMinute, _nSecond = 0;

	auto isLeap = [](int _year)->bool {
		return (_year % 4 == 0) && \
			(_year % 100 != 0) || \
			(_year % 400 == 0);
	};
	//count year;
	while (true) {
		if (isLeap(_nYear)&& _remainDays <=366) {
			break;
		}else if(!isLeap(_nYear) && _remainDays <=365) {
			break;
		}
		if (isLeap(_nYear)) {
			_remainDays -= 366;
		}else {
			_remainDays -= 365;
		}
		_nYear++;
	}
	//count month;
	while (true) {
		if ((1==_nMonth||\
			3 == _nMonth ||\
			5 == _nMonth ||\
			7 == _nMonth ||\
			8 == _nMonth ||\
			10 == _nMonth ||\
			12 == _nMonth)&&_remainDays<=31) {
			break;
		}else if ((4 == _nMonth||\
			6 == _nMonth ||\
			9 == _nMonth ||\
			11 == _nMonth)&&_remainDays<=30) {
			break;
		}else if (isLeap(_nYear)&&2==_nMonth&&_remainDays<=29) {
			break;
		}else if (!isLeap(_nYear) && 2 == _nMonth&&_remainDays <=28) {
			break;
		}
		
		if (1 == _nMonth || \
			3 == _nMonth || \
			5 == _nMonth || \
			7 == _nMonth || \
			8 == _nMonth || \
			10 == _nMonth || \
			12 == _nMonth) {
			_remainDays -= 31;
		}else if (4 == _nMonth || \
			6 == _nMonth || \
			9 == _nMonth || \
			11 == _nMonth) {
			_remainDays -= 30;
		}else if (2==_nMonth) {
			_remainDays -= (isLeap(_nYear)?29:28);
		}
		_nMonth++;
	}

	//count day;
	_nDay = (_nDay>_remainDays)?_nDay:_remainDays;

	//count hour;
	_nHour = _remainSeconds / (60 * 60);
	//count minute;
	_nMinute = (_remainSeconds % (60 * 60)) / 60;
	//count second;
	_nSecond = (_remainSeconds % (60 * 60)) % 60;
	
	_netTime.year = _nYear;
	_netTime.month = _nMonth;
	_netTime.day = _nDay;
	_netTime.hour = _nHour;
	_netTime.minute = _nMinute;
	_netTime.second = _nSecond;
	
	return _netTime;
}

NetTime _arr[] = { \

{ 1970, 1, 1, 8, 0, 0 },\
{ 1970, 1, 1, 23, 59, 59 },\
{ 1970, 2, 28, 0, 0, 0 },\
{ 1970, 2, 28, 23, 59, 59 },\
{ 1970, 3, 1, 0, 0, 0 },\
{ 1970, 3, 1, 23, 59, 59 },\
{ 1970, 12, 31, 0, 0, 0 }, \
{ 1970, 12, 31, 23, 59, 59 }, \

{ 1971, 1, 1, 0, 0, 0 }, \
{ 1971, 1, 1, 23, 59, 59 }, \
{ 1971, 2, 28, 0, 0, 0 }, \
{ 1971, 2, 28, 23, 59, 59 }, \
{ 1971, 3, 1, 0, 0, 0 }, \
{ 1971, 3, 1, 23, 59, 59 }, \
{ 1971, 12, 31, 0, 0, 0 }, \
{ 1971, 12, 31, 23, 59, 59 },\

{ 1972, 1, 1, 0, 0, 0 }, \
{ 1972, 1, 1, 23, 59, 59 }, \
{ 1972, 2, 29, 0, 0, 0 }, \
{ 1972, 2, 29, 23, 59, 59 }, \
{ 1972, 3, 1, 0, 0, 0 }, \
{ 1972, 3, 1, 23, 59, 59 }, \
{ 1972, 12, 31, 0, 0, 0 }, \
{ 1972, 12, 31, 23, 59, 59 }, \

{ 1973, 1, 1, 0, 0, 0 }, \
{ 1973, 1, 1, 23, 59, 59 }, \
{ 1973, 2, 28, 0, 0, 0 }, \
{ 1973, 2, 28, 23, 59, 59 }, \
{ 1973, 3, 1, 0, 0, 0 }, \
{ 1973, 3, 1, 23, 59, 59 }, \
{ 1973, 12, 31, 0, 0, 0 }, \
{ 1973, 12, 31, 23, 59, 59 }, \

{ 2000, 1, 1, 0, 0, 0 }, \
{ 2000, 1, 1, 23, 59, 59 }, \
{ 2000, 2, 29, 0, 0, 0 }, \
{ 2000, 2, 29, 23, 59, 59 }, \
{ 2000, 3, 1, 0, 0, 0 }, \
{ 2000, 3, 1, 23, 59, 59 }, \
{ 2000, 12, 31, 0, 0, 0 }, \
{ 2000, 12, 31, 23, 59, 59 }, \

{ 2001, 1, 1, 0, 0, 0 }, \
{ 2001, 1, 1, 23, 59, 59 }, \
{ 2001, 2, 28, 0, 0, 0 }, \
{ 2001, 2, 28, 23, 59, 59 }, \
{ 2001, 3, 1, 0, 0, 0 }, \
{ 2001, 3, 1, 23, 59, 59 }, \
{ 2001, 12, 31, 0, 0, 0 }, \
{ 2001, 12, 31, 23, 59, 59 }, \

{ 2002, 1, 1, 0, 0, 0 }, \
{ 2002, 1, 1, 23, 59, 59 }, \
{ 2002, 2, 28, 0, 0, 0 }, \
{ 2002, 2, 28, 23, 59, 59 }, \
{ 2002, 3, 1, 0, 0, 0 }, \
{ 2002, 3, 1, 23, 59, 59 }, \
{ 2002, 12, 31, 0, 0, 0 }, \
{ 2002, 12, 31, 23, 59, 59 },

{ 2012, 1, 1, 0, 0, 0 }, \
{ 2018, 10, 1, 23, 59, 59 }, \
{ 1998, 2, 28, 13, 59, 34 }, \
{ 2020, 2, 29, 23, 59, 59 }, \
{ 2008, 3, 1,0, 0, 0 }, \
{ 2033, 3, 1, 23, 59, 59 }, \
{ 2005, 12, 31, 0, 0, 0 }, \
{ 2003, 12, 31, 23, 59, 59 }
};

int main(int args, char* argv[]) {
	for (int _index = 0; _index < sizeof(_arr)/sizeof(NetTime); _index++) {
		time_t _stamp = NetTime2Stamp(_arr[_index]);
		printf("timeStamp: %ld; ", _stamp);

		NetTime _resTime = Stamp2NetTime(_stamp);
		char show[512] = { 0x00 };
		sprintf_s(show, 512, "%d-%02d-%02d %02d:%02d:%02d", _resTime.year, \
			_resTime.month, \
			_resTime.day, \
			_resTime.hour, \
			_resTime.minute, \
			_resTime.second);
		printf("time: %s\n", show);
		if (0==(_index+1)%8) {
			printf("\n");
		}
	}
	getchar();
	return 0;
} 

运行结果:

北京时间、时间戳互转