00001
00002
00003
00004
00005
00006
00007
00008 #include <rtc.h>
00009
00010 __BEGIN_SYS
00011
00012 RTC_Common::Seconds RTC_Common::date2offset(unsigned int epoch_days,
00013 unsigned int Y, unsigned int M, unsigned int D,
00014 unsigned int h, unsigned int m, unsigned int s)
00015 {
00016 M -= 2;
00017 if(M < 0) {
00018 M += 12;
00019 Y -= 1;
00020 }
00021 return ((((unsigned long)(Y/4 - Y/100 + Y/400 + 367 * M/12 + D)
00022 + Y * 365 - epoch_days) * 24 + h) * 60 + m) * 60 + s;
00023 }
00024
00025 void RTC_Common::offset2date(
00026 RTC_Common::Seconds t, unsigned int epoch_days,
00027 unsigned int * Y, unsigned int * M, unsigned int * D,
00028 unsigned int * h, unsigned int * m, unsigned int * s)
00029 {
00030 static int days_per_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
00031
00032 *s = t % 60;
00033 t /= 60;
00034 *m = t % 60;
00035 t /= 60;
00036 *h = t % 24;
00037 t /= 24;
00038 t += epoch_days;
00039 for(*Y = 1; t - 365 > 0; *Y++, t -= 365)
00040 if(((*Y % 4 == 0) && (*Y % 100 != 0)) || (*Y % 400 == 0))
00041 t--;
00042 days_per_month[1] = 28;
00043 if(((*Y % 4 == 0) && (*Y % 100 != 0)) || (*Y % 400 == 0))
00044 days_per_month[1] = 29;
00045 for(*M = 1; t - days_per_month[*M] > 0; *M++, t -= days_per_month[*M]);
00046 *D = t;
00047 }
00048
00049 __END_SYS