抑郁症健康,内容丰富有趣,生活中的好帮手!
抑郁症健康 > UTC时间如何转换成北京时间—C语言代码

UTC时间如何转换成北京时间—C语言代码

时间:2022-10-12 05:25:22

相关推荐

解析原理:

UTC + 时区差 = 本地时间

时区差东为正,西为负。在此,把东八区时区差记为 +0800,

UTC + (+0800) = 本地(北京)时间 (1)

那么,UTC = 本地时间(北京时间))- 0800 (2)

0942 - 0800 = 0142

即UTC是当天凌晨一点四十二分二十二秒。如果结果是负数就意味着是UTC前一天,把这个负数加上2400就是UTC在前一天的时间。例如,本地(北京)时间是 0432 (凌晨四点三十二分),那么,UTC就是 0432 - 0800 = -0368,负号意味着是前一天, -0368 + 2400 = 2032,既前一天的晚上八点三十二分。

解析代码:

//UTC转换为北京时间 函数可直接调用void UTCToBeijing(unsigned int UTCyear,unsigned char UTCmonth,unsigned char UTCday,unsigned int UTChour,unsigned char UTCminute,unsigned char UTCsecond){int year=0,month=0,day=0,hour=0;int lastday = 0;// 月的最后一天日期int lastlastday = 0;//上月的最后一天日期year=UTCyear;month=UTCmonth;day=UTCday;hour=UTChour+8;//UTC+8转换为北京时间if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){lastday = 31;if(month == 3){if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//判断是否为闰年lastlastday = 29;//闰年的2月为29天,平年为28天elselastlastday = 28;}if(month == 8)lastlastday = 31;}else if(month == 4 || month == 6 || month == 9 || month == 11){lastday = 30;lastlastday = 31;}else{lastlastday = 31;if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//闰年的2月为29天,平年为28天lastday = 29;elselastday = 28;}if(hour >= 24)//当算出的时大于或等于24:00时,应减去24:00,日期加一天{hour -= 24;day += 1; if(day > lastday)//当算出的日期大于该月最后一天时,应减去该月最后一天的日期,月份加上一个月{ day -= lastday;month += 1;if(month > 12)//当算出的月份大于12,应减去12,年份加上1年{month -= 12;year += 1;}}}sprintf((char *)bjttbuf,"%02d/%02d/%02d,%02d:%02d:%02d",year-2000,month,day,hour,gpsx.utc.min,gpsx.utc.sec); //UTC日期时分秒转换成北京时间// Uart1_SendStr(bjttbuf); // Uart1_SendStr("\r\n");

/news/chanpinceshi/utc-coding.html

如果觉得《UTC时间如何转换成北京时间—C语言代码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。