Functions | |||||||||||||
__inline__ unsigned long | mktime (unsigned int year, unsigned int mon, unsigned int day, unsigned int hour, unsigned int min, unsigned int sec) | ||||||||||||
Converts Gregorian date to seconds since 1970-01-01 00:00:00.
| |||||||||||||
void | time_init () | ||||||||||||
Initialize the Real-Time Clock. | |||||||||||||
time_t | time_read () | ||||||||||||
Get the seconds passed since midnight 1970-01-01 (Unix timestamp).
|
|
Converts Gregorian date to seconds since 1970-01-01 00:00:00.
Assumes input in normal date format, i.e. 1980-12-31 23:59:59 => year=1980, mon=12, day=31, hour=23, min=59, sec=59. [For the Julian calendar (which was used in Russia before 1917, Britain & colonies before 1752, anywhere else before 1582, and is still in use by some communities) leave out the -year/100+year/400 terms, and add 10.] This algorithm was first published by Gauss (I think).
WARNING: this function will overflow on 2106-02-07 06:28:16 on machines were long is 32-bit! (However, as time_t is signed, we will already get problems at other places on 2038-01-19 03:14:08) Definition at line 132 of file time.h.
00135 { 00136 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */ 00137 mon += 12; /* Puts Feb last since it has leap day */ 00138 year -= 1; 00139 } 00140 00141 return ((( 00142 (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) + 00143 year*365 - 719499 00144 )*24 + hour /* now have hours */ 00145 )*60 + min /* now have minutes */ 00146 )*60 + sec; /* finally seconds */ 00147 } |
|
Initialize the Real-Time Clock.
Definition at line 19 of file time.c.
00020 { 00021 INIT_MUTEX(&rtc_lock); 00022 } |
|
Get the seconds passed since midnight 1970-01-01 (Unix timestamp).
Definition at line 29 of file time.c.
00030 { 00031 unsigned int year, mon, day, hour, min, sec; 00032 int i; 00033 00034 DOWN(&rtc_lock); 00035 00036 // When the Update-In-Progress (UIP) flag goes from 1 to 0, // 00037 // the RTC registers show the second which has precisely just // 00038 // started. // 00039 // Read RTC exactly on falling edge of update flag. // 00040 00041 for (i=0; i<1000000 ; i++) // may take up to 1 second... // 00042 if ( CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP ) 00043 break; 00044 00045 for (i=0 ; i<1000000 ; i++) // must try at least 2.228 ms // 00046 if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) 00047 break; 00048 00049 do { /* Isn't this overkill ? UIP above should guarantee consistency */ 00050 sec = CMOS_READ(RTC_SECONDS); 00051 min = CMOS_READ(RTC_MINUTES); 00052 hour = CMOS_READ(RTC_HOURS); 00053 day = CMOS_READ(RTC_DAY_OF_MONTH); 00054 mon = CMOS_READ(RTC_MONTH); 00055 year = CMOS_READ(RTC_YEAR); 00056 } while (sec != CMOS_READ(RTC_SECONDS)); 00057 00058 if ( !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ) 00059 { 00060 sec = BCD2BIN( sec ); 00061 min = BCD2BIN( min ); 00062 hour = BCD2BIN( hour ); 00063 day = BCD2BIN( day ); 00064 mon = BCD2BIN( mon ); 00065 year = BCD2BIN( year ); 00066 } 00067 00068 UP(&rtc_lock); 00069 00070 if ( (year += 1900) < 1970 ) 00071 year += 100; 00072 00073 return( mktime(year, mon, day, hour, min, sec) ); 00074 } |