分享

Linux系统UTC和CST时间转换

 XeonGate 2020-09-28

        在Linux系统中,用date查看时间的时候显示:

http://blog./uid-29392544-id-4042320.html

点击(此处)折叠或打开

  1. # date
  2. Wed Dec 18 13:26:04 CST 2013
  3. # date -u
  4. Wed Dec 18 05:26:13 UTC 2013


  CST China Standard Time UTC+8:00 中国沿海时间(北京时间)

  世界协调时间(Universal Time Coordinated,UTC)

  GPS 系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,地方时为本地时间,如北京为早上八点(东八区),UTC时间就为零点,时间比北京时晚八小时,以此计算即可

        下面代码实现了CST和UTC时间的转换:
        cst_utc.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4. #if 0
  5. typedef long __kernel_suseconds_t;
  6. typedef __kernel_suseconds_t suseconds_t;

  7. typedef struct timeval {
  8.         time_t tv_sec; /* seconds */
  9.         suseconds_t tv_usec; /* microseconds */
  10. }TIMEVAL;

  11. typedef struct timespec {
  12.         time_t tv_sec; /* seconds */
  13.         long tv_nsec; /* nanoseconds */
  14. }TIMESPEC;

  15. typedef struct timezone {
  16.         int tz_minuteswest; /* minutes west of Greenwich */
  17.         int tz_dsttime; /* type of dst correction */
  18. }TIMEZONE;
  19. #endif
  20. /*
  21.  * * The struct used to pass data via the following ioctl. Similar to the
  22.  * * struct tm in <time.h>, but it needs to be here so that the kernel
  23.  * * source is self contained, allowing cross-compiles, etc. etc.
  24.  * */

  25. typedef struct rtc_time {
  26.         int tm_sec;
  27.         int tm_min;
  28.         int tm_hour;
  29.         int tm_mday;
  30.         int tm_mon;
  31.         int tm_year;
  32.         int tm_wday;
  33.         int tm_yday;
  34.         int tm_isdst;
  35. }RTC_TIME;

  36. static const unsigned char rtc_days_in_month[] = {
  37.         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  38. };

  39. static const unsigned short rtc_ydays[2][13] = {
  40.         /* Normal years */
  41.         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  42.         /* Leap years */
  43.         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  44. };

  45. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
  46. #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))

  47. /*
  48.  * The number of days in the month.
  49.  */
  50. int rtc_month_days(unsigned int month, unsigned int year)
  51. {
  52.         return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
  53. }

  54. /*
  55.  * The number of days since January 1. (0 to 365)
  56.  */
  57. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  58. {
  59.         return rtc_ydays[LEAP_YEAR(year)][month] + day-1;
  60. }

  61. /*
  62.  * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  63.  */
  64. void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  65. {
  66.         register int days, month, year;

  67.         days = time / 86400;
  68.         time -= days * 86400;

  69.         /* day of the week, 1970-01-01 was a Thursday */
  70.         tm->tm_wday = (days + 4) % 7;

  71.         year = 1970 + days / 365;
  72.         days -= (year - 1970) * 365
  73.                 + LEAPS_THRU_END_OF(year - 1)
  74.                 - LEAPS_THRU_END_OF(1970 - 1);
  75.         if (days < 0) {
  76.                 year -= 1;
  77.                 days += 365 + LEAP_YEAR(year);
  78.         }
  79.         tm->tm_year = year - 1900;
  80.         tm->tm_yday = days + 1;

  81.         for (month = 0; month < 11; month++) {
  82.                 int newdays;

  83.                 newdays = days - rtc_month_days(month, year);
  84.                 if (newdays < 0)
  85.                         break;
  86.                 days = newdays;
  87.         }
  88.         tm->tm_mon = month;
  89.         tm->tm_mday = days + 1;

  90.         tm->tm_hour = time / 3600;
  91.         time -= tm->tm_hour * 3600;
  92.         tm->tm_min = time / 60;
  93.         tm->tm_sec = time - tm->tm_min * 60;
  94. }

  95. int main(void){

  96.         struct timeval tv;
  97.         struct timezone tz;
  98.         RTC_TIME tm;
  99.         gettimeofday(&tv, &tz);

  100.         rtc_time_to_tm(tv.tv_sec, &tm);
  101.         fprintf(stdout, "UTC time :%04d-%02d-%02d %02d:%02d:%02d \n",
  102.                         tm.tm_year+1900,tm.tm_mon, tm.tm_mday, tm.tm_hour,
  103.                         tm.tm_min, tm.tm_sec);

  104.         rtc_time_to_tm(tv.tv_sec + 60*60*8, &tm);
  105.         fprintf(stdout, "After adjust :%04d-%02d-%02d %02d:%02d:%02d \n",
  106.                         tm.tm_year+1900,tm.tm_mon, tm.tm_mday, tm.tm_hour,
  107.                         tm.tm_min, tm.tm_sec);

  108.         return 0;

  109. }


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约