Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "time.h"
  2. #include "kolibrisys.h"
  3.  
  4. struct tm __buffertime;
  5.  
  6. struct tm * localtime (const time_t * timer)
  7. /* non-standard!  ignore parameter and return just time now */
  8. {
  9.     int kos_date, kos_time;
  10.     kos_date = _ksys_get_date();
  11.     kos_time = _ksys_get_system_clock();
  12.    
  13.     int bcd_day = (kos_date >> 16);
  14.     int bcd_mon = ((kos_date & 0xFF00) >> 8);
  15.     int bcd_year = (kos_date & 0xFF);
  16.     __buffertime.tm_mday = ((bcd_day & 0xF0)>>4)*10 + (bcd_day & 0x0F);
  17.     __buffertime.tm_mon = ((bcd_mon & 0xF0)>>4)*10 + (bcd_mon & 0x0F) - 1;
  18.     __buffertime.tm_year = ((bcd_year & 0xF0)>>4)*10 + (bcd_year & 0x0F) + 100;
  19.     //printf("%d %d %d\n", __buffertime.tm_mday, __buffertime.tm_mon, __buffertime.tm_year);
  20.    
  21.     __buffertime.tm_wday = __buffertime.tm_yday = __buffertime.tm_isdst = -1; /* temporary */
  22.    
  23.     int bcd_sec = (kos_time >> 16);
  24.     int bcd_min = ((kos_time & 0xFF00) >> 8);
  25.     int bcd_hour = (kos_time & 0xFF);
  26.  
  27.     __buffertime.tm_sec = ((bcd_sec & 0xF0)>>4)*10 + (bcd_sec & 0x0F);
  28.     __buffertime.tm_min = ((bcd_min & 0xF0)>>4)*10 + (bcd_min & 0x0F);
  29.     __buffertime.tm_hour = ((bcd_hour & 0xF0)>>4)*10 + (bcd_hour & 0x0F);
  30.     //printf("%d %d %d\n", __buffertime.tm_sec, __buffertime.tm_min, __buffertime.tm_hour);
  31.  
  32.     return &__buffertime;
  33. }
  34.  
  35. time_t time (time_t* timer)
  36. {
  37.     time_t t = mktime(localtime(0));  
  38.     if (timer) *timer = t;  
  39.     return t;
  40. }
  41.  
  42. time_t mktime (struct tm * timeptr)
  43. {
  44.     /*int y, m, d;
  45.     time_t  t;
  46.     y = timeptr->tm_year + 1900;
  47.     m = timeptr->tm_mon + 1;
  48.     d = timeptr->tm_mday; // to -1 or not to -1?
  49.    
  50.     if (m < 3) { m += 12; y -= 1; }
  51.    
  52.     t = y * 365 + y / 4 + y /400 - y / 100; // years - > days
  53.     t += 30 * m + 3 * (m + 1) / 5 + d;            // add month days
  54.    
  55.     t -= 719561;  // 01 jan 1970
  56.     t *= 86400;    
  57.  
  58.     t += 3600 * timeptr->tm_hour + 60 * timeptr->tm_min + timeptr->tm_sec;
  59.  
  60.     return t;*/
  61.  
  62.     int utcdiff = -3;
  63.     const int mon_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  64.     unsigned long int tyears, tdays, leaps, utc_hrs;
  65.     int i;
  66.    
  67.     tyears = timeptr->tm_year - 70 ;// tm->tm_year is from 1900.
  68.     leaps = (tyears + 2) / 4; // no of next two lines until year 2100.
  69.     i = (timeptr->tm_year - 100) / 100;
  70.     leaps -= ( (i/4)*3 + i%4 );
  71.     tdays = 0;
  72.     for (i=0; i < timeptr->tm_mon; i++) tdays += mon_days[i];
  73.    
  74.     tdays += timeptr->tm_mday-1; // days of month passed.
  75.     tdays = tdays + (tyears * 365) + leaps;
  76.  
  77.     utc_hrs = timeptr->tm_hour + utcdiff; // for your time zone.
  78.     return (tdays * 86400) + (utc_hrs * 3600) + (timeptr->tm_min * 60) + timeptr->tm_sec;
  79. }
  80.  
  81. double difftime (time_t end, time_t beginning)
  82. {
  83.     return end - beginning;
  84. }
  85.  
  86.  
  87.