[DemoCode下载] UTC时间转换为其他时区本地时间

[复制链接]
1151|3
 楼主| uytyu 发表于 2025-4-17 07:39 | 显示全部楼层 |阅读模式
tc, ST, DA, TI, ar
  1. typedef struct  _nmea_time {                                                                                    
  2.         unsigned short year;        //年份
  3.         unsigned char month;        //月份
  4.         unsigned char date;            //日期
  5.         unsigned char hour;         //小时
  6.         unsigned char min;             //分钟
  7.         unsigned char sec;             //秒钟
  8. } nmea_time;


  9. void UTC_to_ZoneTime(const nmea_time* utc_time, int timezone, nmea_time* local_time)
  10. {
  11.         int year, month, day, hour;
  12.     int lastday;                //last day of this month.
  13.     int lastlastday;            //last day of last month.

  14.     year    = utc_time->year;   //utc time.
  15.     month   = utc_time->month;
  16.     day     = utc_time->date;
  17.     hour    = utc_time->hour + timezone;

  18.     /* Step0: Based on the current month, calculate the number of days in the current and last month */
  19.     if (1==month || 3==month || 5==month || 7==month || 8==month || 10==month || 12==month) {
  20.         lastday = 31;
  21.         lastlastday = 30;
  22.         if (3 == month) {
  23.             if ((0 == year%400) || ((0 == year%4) && (year%100 != 0))) { //if this is lunar year.
  24.                 lastlastday = 29;
  25.             } else {
  26.                 lastlastday = 28;
  27.             }
  28.         } else if ((1 == month) || (8 == month)) {
  29.             lastlastday = 31;
  30.         }
  31.     } else if (4==month || 6==month || 9==month || 11==month) {
  32.         lastday = 30;
  33.         lastlastday = 31;
  34.     } else {
  35.         lastlastday = 31;
  36.         if ((0 == year%400) || ((0 == year%4) && (year%100 != 0))) {
  37.             lastday = 29;
  38.         } else {
  39.             lastday = 28;
  40.         }
  41.     }

  42.     /* Step1: Based on the current time zone, calculate the local time */
  43.     if (hour >= 24) {           // if >24, day+1
  44.         hour -= 24;
  45.         day += 1;

  46.         if (day > lastday) {    // next month,  day-lastday of this month
  47.             day -= lastday;
  48.             month += 1;

  49.             if (month > 12) {   //        next year , month-12
  50.                 month -= 12;
  51.                 year += 1;
  52.             }
  53.         }
  54.     } else if (hour < 0) {      // if <0, day-1
  55.         hour += 24;
  56.         day -= 1;

  57.         if (day < 1) {          // month-1, day=last day of last month
  58.             day = lastlastday;
  59.             month -= 1;

  60.             if (month < 1) {    // last year , month=12
  61.                 month = 12;
  62.                 year -= 1;
  63.             }
  64.         }
  65.     }

  66.     /* Step2: Output the local_time */
  67.         local_time->year  = year;
  68.         local_time->month = month;
  69.         local_time->date  = day;
  70.         local_time->hour  = hour;
  71.         local_time->min         = utc_time->min;
  72.         local_time->sec         = utc_time->sec;
  73. }


稳稳の幸福 发表于 2025-4-17 15:48 | 显示全部楼层
根据不同时区计算。
水星限定 发表于 2025-8-28 13:21 | 显示全部楼层
是的,新唐有 RISC-V 架构的芯片。据报道,其在 2025 年推出了首款 RISC-V 架构的量产芯片,将用于 Google Chromebook,可提供可信启动及硬件级密钥保护功能。
世纪女孩 发表于 2025-9-8 14:36 | 显示全部楼层
确定目标时区与 UTC 的偏移量(如东八区 + 8 小时、西五区 - 5 小时),含夏令时需额外加 1 小时。
计算本地时间:将 UTC 时间的时、分、秒与偏移量相加,处理跨日 / 跨月 / 跨年(如 UTC 23:00+8→次日 7:00)。
借助库函数更便捷,如 C 语言localtime_r、Pythonpytz,输入 UTC 时间和时区,直接获取转换后本地时间。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

37

主题

3598

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部