Advertisement
teknoraver

seconds to tm

May 17th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <time.h>
  5.  
  6. typedef uint32_t _u32;
  7.  
  8. typedef struct  
  9. {
  10.     /* time */
  11.     _u32                sl_tm_sec;
  12.     _u32                sl_tm_min;
  13.     _u32                sl_tm_hour;
  14.     /* date */
  15.     _u32                sl_tm_day; /* 1-31 */
  16.     _u32                sl_tm_mon; /* 1-12 */
  17.     _u32                sl_tm_year; /*  YYYY 4 digits  */
  18.     _u32                sl_tm_week_day; /* not required */
  19.     _u32                sl_tm_year_day; /* not required */
  20.     _u32                reserved[3];
  21. } SlDateTime_t;
  22.  
  23. #define DAY 86400
  24. #define YEAR    (DAY * 365)
  25. #define YEAR4   (YEAR * 4 + DAY)
  26.  
  27. static int secs2tm(time_t t, SlDateTime_t *st)
  28. {
  29.     /* days in a month, starting March */
  30.     uint8_t dom[] = { 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29 };
  31.     int i;
  32.  
  33.     /* start just after 29 Feb 1972, the first leap day in the UNIX history*/
  34.     st->sl_tm_year = 1972;
  35.     t -= 68256000;
  36.    
  37.     if (t > YEAR4) {
  38.         st->sl_tm_year += t / YEAR4 * 4;
  39.         t %= YEAR4;
  40.     }
  41.     if (t > YEAR) {
  42.         st->sl_tm_year += t / YEAR;
  43.         t %= YEAR;
  44.         dom[11]--;
  45.     }
  46.     for (i = 0; i < 11; i++) {
  47.         if(t < DAY * dom[i])
  48.             break;
  49.         t -= DAY * dom[i];
  50.     }
  51.     st->sl_tm_mon = i + 3;
  52.     if (st->sl_tm_mon > 12) {
  53.         st->sl_tm_year++;
  54.         st->sl_tm_mon -= 12;
  55.     }
  56.  
  57.     st->sl_tm_day = t / DAY + 1;
  58.     t %= DAY;
  59.  
  60.     st->sl_tm_hour = t / 3600;
  61.     t %= 3600;
  62.  
  63.     st->sl_tm_min = t / 60;
  64.     t %= 60;
  65.  
  66.     st->sl_tm_sec = t;
  67.  
  68.     return 0;
  69. }
  70.  
  71. /*
  72.   test with:
  73.  
  74. while : ; do
  75.     d=$RANDOM$RANDOM
  76.     [ $d -lt 68256000 ] && continue
  77.     [ "$(./s2t $d)" = "$(date -u -d"@$d" +"%F %T")" ] || echo "error @$d: $(./s2t $d) != ($(date -u -d"@$d" +"%F %T"))"
  78. done
  79.  
  80.  */
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84.     SlDateTime_t tm;
  85.     uint32_t date;
  86.  
  87.     if(argc < 2) {
  88.         fprintf(stderr, "usage: %s <secs from epoch>\n", *argv);
  89.         return 1;
  90.     }
  91.     date = atoi(argv[1]);
  92.  
  93.     secs2tm(date, &tm);
  94.  
  95.     printf("%04u-%02u-%02u %02u:%02u:%02u\n",
  96.         tm.sl_tm_year,
  97.         tm.sl_tm_mon,
  98.         tm.sl_tm_day,
  99.         tm.sl_tm_hour,
  100.         tm.sl_tm_min,
  101.         tm.sl_tm_sec
  102.     );
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement