scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

localtime.c (882B)


      1 #include <string.h>
      2 #include <time.h>
      3 
      4 #include "../libc.h"
      5 #undef localtime
      6 
      7 static time_t
      8 gmtoff(char *tz)
      9 {
     10 	for (struct tzone *t = tzones; t->name; t++)
     11 		if (!strcmp(t->name, tz))
     12 			return t->gmtoff;
     13 	return 0;
     14 }
     15 
     16 struct tm *
     17 localtime(const time_t *timep)
     18 {
     19 	struct tm *tm;
     20 	time_t t;
     21 	int yday;
     22 	static int first = 1;
     23 
     24 	t = *timep;
     25 	tm = gmtime(&t);
     26 	yday = tm->tm_yday;
     27 
     28 	if (first) {
     29 		_tzset();
     30 		if (_tzstdoff == -1)
     31 			_tzstdoff = gmtoff(_tzname[0]);
     32 		if (_tzdstoff == -1)
     33 			_tzdstoff = gmtoff(_tzname[1]);
     34 	}
     35 	first = 0;
     36 
     37 	tm->tm_gmtoff = _tzstdoff;
     38 	tm->tm_zone = _tzname[0];
     39 	tm->tm_isdst = 0;
     40 
     41 	if (_tzjulian && yday+1 < 60 || FEBDAYS(tm->tm_year) < 29)
     42 		yday++;
     43 
     44 	if (yday >= _tzstart && yday <= _tzend && tm->tm_hour >= 2) {
     45 		tm->tm_gmtoff = _tzdstoff;
     46 		tm->tm_zone = _tzname[1];
     47 		tm->tm_isdst = 1;
     48 	}
     49 
     50 	t += tm->tm_gmtoff;
     51 	tm = gmtime(&t);
     52 
     53 	return tm;
     54 }