C++ 如何将世界时间转化成北京时间?(用localtime函数是如何区分大月还是小月还有平月?

我知道北京时间=世界时间+8,我现在的问题是要在世界时间+8得到北京时间,但问题是函数无法区分大月,我把每个月的30+8小时以后都能得出本月的31号,也就是所有的月份都有... 我知道北京时间 = 世界时间 + 8,我现在的问题是要在世界时间 +8 得到北京时间,但问题是函数无法区分大月,我把每个月的30 +8小时以后都能得出本月的31号,也就是所有的月份都有31天了!!
下面是详细的介绍
//首先有一个时间字符串,在原有的这个字符串上加上28800秒
const char* testchar = "2013-02-28 23:35:04.268";
int d_year,d_month,d_day,d_hour,d_min,d_second;
sscanf(testchar, "%d-%d-%d %d:%d:%d", &d_year, &d_month,&d_day,&d_hour,&d_min,&d_second);
struct tm t;
t.tm_year = d_year - 1900;
t.tm_mon = d_month;
t.tm_mday = d_day;
t.tm_hour = d_hour;
t.tm_min = d_min;
t.tm_sec = d_second;
time_t s = mktime(&t);
//加上8个小时也就是过了2月28号这一天
s += 28800;
struct tm * t1 = localtime(&s);
CString timestr;
timestr.Format("%d-%02d-%02d %02d:%02d:%02d",t1->tm_year +1900,t1->tm_mon,t1->tm_mday,t1->tm_hour,t1->tm_min,t1->tm_sec);
这样导出来的时间竟然变成"2013-02-29 07:35:04.268"
请问应该怎么办呢?
展开
 我来答
金色潜鸟
2013-03-02 · TA获得超过3.2万个赞
知道大有可为答主
回答量:1.3万
采纳率:89%
帮助的人:6680万
展开全部
为方便调试程序,输入数据我用了空格。

#include <stdio.h>
#include <time.h>
void main(){
struct tm *ptm;
time_t rawtime, t;
int year,month,mday,hh,mm,ss;

time ( &rawtime );
ptm = gmtime ( &rawtime ); //建 UTC 时间

printf("Please enter year month day hour minute second\n");
printf("For example: \n");
printf("2013 2 28 23 35 4\n");
scanf("%d %d %d %d %d %d", &year, &month, &mday, &hh,&mm,&ss);
ptm->tm_year = year - 1900;
ptm->tm_mon= month - 1;
ptm->tm_mday = mday ;
ptm->tm_hour = hh ;
ptm->tm_min = mm ;
ptm->tm_sec = ss ;
t = mktime (ptm); //得 UTC 时间 年月日时分秒
printf("%s ",ctime(&t)); // 打印出 Thu Feb 28 23:35:04 2013
t = t+8*3600; //北京时间,加 8 小时就是
printf("%s ",ctime(&t)); // 打印出 Fri Mar 01 07:35:04 2013
}

// MS VC++ 6.0 编译器
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式