scc

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

localtime.c (920B)


      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	       &&
     42 	    ((yday + 1) < 60)  ||
     43 	    (FEBDAYS(tm->tm_year) < 29))
     44 		yday++;
     45 
     46 	if (yday >= _tzstart &&
     47 	    yday <= _tzend   &&
     48 	    tm->tm_hour >= 2) {
     49 		tm->tm_gmtoff = _tzdstoff;
     50 		tm->tm_zone = _tzname[1];
     51 		tm->tm_isdst = 1;
     52 	}
     53 
     54 	t += tm->tm_gmtoff;
     55 	tm = gmtime(&t);
     56 
     57 	return tm;
     58 }