Advertisement

UTC时间戳和北京时间的转化 C嵌入式代码

阅读量:

由于工作需要,经常需要把当前时间转换成UTC格式,每次都是去如下链接查询,比较麻烦,故想着用C嵌入式代码将此实现。

Unix Time Stamp - Epoch Converter

复制代码
 #include <string.h>

    
 #include <stdint.h>
    
 #include <stdbool.h>
    
 #include <stdio.h>
    
  
    
 /** * @brief  CALENDAR_Time calendar time structure definition
    
   */
    
 typedef struct _calendar_time
    
 {
    
     uint8_t sec;                    /**< Specifies the Calendar time seconds.
    
                                      This parameter must be a number between min_value = 0 and max_value = 59. */
    
  
    
     uint8_t min;                    /**< Specifies the Calendar time minutes.
    
                                      This parameter must be a number between min_value = 0 and max_value = 59. */
    
  
    
     uint8_t hour;                   /**< Specifies the Calendar time hour.
    
                                      This parameter must be a number between min_value = 0 and max_value = 23. */
    
  
    
     uint8_t date;                   /**< Specifies the Calendar date.
    
                                      This parameter must be a number between min_value = 1 and max_value = 31. */
    
  
    
     uint8_t mon;                    /**< Specifies the Calendar month.
    
                                      This parameter must be a number between min_value = 1 and max_value = 12. */
    
  
    
     uint8_t year;                   /**< Specifies the Calendar year which stars from 2010.
    
                                      This parameter must be a number between min_value = 0 and max_value = 99. */
    
  
    
     uint8_t week;                   /**< Specifies the Calendar weekday.
    
                                      This parameter must be a number between min_value = 0 and max_value = 6.  */
    
  
    
     uint16_t ms;                    /**< Specifies the Calendar time milliseconds.
    
                                     This parameter must be a number between min_value = 0 and max_value = 999. */
    
 } current_calendar_time_t;
    
  
    
 /* Caculate seconds from 01.01.2000 00:00 to the time */
    
 void current_calendar_time2seconds(current_calendar_time_t *p_time, uint32_t *p_seconds)
    
 {
    
     uint16_t year;
    
     uint32_t utc;
    
  
    
     // 10957 is the days between 1970/1/1 and 2000/1/1
    
     year = (p_time->year + 2000) % 100;
    
     utc  = 10957;
    
     utc += (year * 365 + (year + 3) / 4);
    
     utc += (367 * p_time->mon - 362) / 12 - (p_time->mon <= 2 ? 0 : ((year % 4 == 0) ? 1 : 2));
    
     utc += (p_time->date - 1);
    
     utc *= 86400;
    
     utc += (p_time->hour * 3600 + p_time->min * 60 + p_time->sec);
    
  
    
     *p_seconds = utc;
    
 			
    
 }
    
  
    
 /* Caculate time with seconds from 01.01.2000 00:00 to the p_time */
    
 void seconds2_current_calendar_time(current_calendar_time_t *p_time, uint32_t seconds)
    
 {
    
     const uint16_t days0[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
    
     const uint16_t days1[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    
  
    
     current_calendar_time_t time;
    
     uint32_t days, secs;
    
     uint16_t year;
    
     const uint16_t * dayp;
    
  
    
     // 10957 is the days between 1970/1/1 and 2000/1/1
    
     if (seconds >= 10957 * 86400)
    
     {
    
     days = seconds / 86400 - 10957;
    
     secs = seconds % 86400;
    
     }
    
     else
    
     {
    
     days = 0;
    
     secs = 0;
    
     }
    
  
    
     time.sec    = secs % 60;    secs /= 60;
    
     time.min    = secs % 60;    secs /= 60;
    
     time.hour   = secs;
    
  
    
     year        = 2000;
    
     time.week   = (days + 6) % 7;
    
  
    
     year        += (days / 1461) * 4; days %= 1461;
    
     if (days >= 366)
    
     dayp = days1;
    
     else
    
     dayp = days0;
    
     if (days >= 366)
    
     {
    
     year    += (days - 1) / 365;
    
     days    = (days - 1) % 365;
    
     }
    
  
    
     time.mon    = days / 31 + 1;
    
     if (days >= dayp[time.mon])
    
     time.mon += 1;
    
  
    
     time.date   = days - dayp[time.mon - 1] + 1;
    
     time.year   = year - 2000;
    
  
    
     memcpy(p_time, &time, sizeof(current_calendar_time_t));
    
 }
    
  
    
  
    
 uint32_t seconds ;
    
 current_calendar_time_t         g_current_calendar_time;
    
  
    
  void current_calendar_init(void)
    
 {
    
  
    
     g_current_calendar_time.year = 23;
    
     g_current_calendar_time.mon  = 12;
    
     g_current_calendar_time.date = 29;
    
     g_current_calendar_time.hour = 15;
    
     g_current_calendar_time.min  = 51;
    
     g_current_calendar_time.sec  = 00;
    
 	
    
 	current_calendar_time2seconds(&g_current_calendar_time, &seconds);
    
 	printf("current_calendar_init->seconds2_calendar_time %d \n",seconds);
    
 }

全部评论 (0)

还没有任何评论哟~