已知年月日,如何得到距离1970-1-1 00:00:00有多少时间?

PDD123 2017-01-05 11:37:41
程序中要求是计算有多少小时。
不考虑时差,倒是很简单,直接用CTime类就行。
但是,难点是:已知的年月日,用的是协调世界时。使用CTime类的话,就少了8个小时(北京时间)。并且程序有可能在任何地区运行
请问,有没有现成的库可以做?
...全文
442 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-01-06
  • 打赏
  • 举报
回复
仅供参考:
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
    COleDateTime t;
    COleDateTimeSpan ts;
    CString s,fmt;
    int nYear;
    int nMonth;
    int nDay;
    int nHour;
    int nMin;
    int nSec;
    int lDays;
    int nHours;
    int nMins;
    int nSecs;
    int i,N;
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
        printf("Fatal Error: MFC initialization failed\n");
        return 1;
    }
    if (argc<13) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 2;
    }
    if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
    else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
    else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
    else {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 3;
    }
    nYear =atoi(argv[ 1]);
    nMonth=atoi(argv[ 2]);
    nDay  =atoi(argv[ 3]);
    nHour =atoi(argv[ 4]);
    nMin  =atoi(argv[ 5]);
    nSec  =atoi(argv[ 6]);
    lDays =atoi(argv[ 7]);
    nHours=atoi(argv[ 8]);
    nMins =atoi(argv[ 9]);
    nSecs =atoi(argv[10]);
    N     =atoi(argv[11]);
    if (N<=0) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 4;
    }
    t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
    ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
    for (i=1;i<=N;i++) {
        s=t.Format(fmt);
        printf("%08d %s\n",i,s);
        t=t+ts;
    }
    return 0;
}
mLee79 2017-01-06
  • 打赏
  • 举报
回复
还是这样一行好看些.

int slove(int y, int m, int d)
{	
	return y*365+y/4-y/100+y/400+30*m+((signed char*)"\0\0\1\xff\0\0\1\1\2\3\3\4\4\5")[m]+d-((m<=2&&((y&3)==0)&&((y%100)!=0||y%400==0)));
}
mLee79 2017-01-06
  • 打赏
  • 举报
回复
如果不管 格里历 和儒略历 撕逼的话, 两行代码就可以了...
#include <stdio.h>

int slove(int year, int month, int day)
{
	static const int xdate_mdays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	return year * 365 + year / 4 - year / 100 + year / 400 + xdate_mdays[month - 1] + day - ((month <= 2 && ((year & 3) == 0) && ((year % 100) != 0 || year % 400 == 0)));
}

int main()
{
	int y, m, d;
	int d0 = slove(1970,1,1);
	
	while(3 == scanf("%d%d%d", &y, &m, &d))
	{
		if(m < 1 || m > 12 || d < 1 || d > 31)
		{
			fprintf(stderr, "INVALID: %04d-%02d-%02d\n", y, m, d);
			continue;			
		}
		
		printf("%04d-%02d-%02d : %d\n", y, m, d, slove(y,m,d) - d0);
	}
	return 0;
}
sdghchj 2017-01-06
  • 打赏
  • 举报
回复
时区转换是基础功能,库函数有提供。
ri_aje 2017-01-06
  • 打赏
  • 举报
回复
std::chrono

64,664

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧