]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/compat/gmtime_r.c
19eb637e3c9b85562f120d5aa66be887e4173f32
2 * Taken from FreeBSD src / lib / libc / stdtime / localtime.c 1.43 revision.
5 * adapted to be replacement gmtime_r.
13 #define MONSPERYEAR 12
14 #define DAYSPERNYEAR 365
15 #define DAYSPERLYEAR 366
17 #define SECSPERHOUR (60*60)
18 #define SECSPERDAY (24*60*60)
23 #define TM_WEDNESDAY 3
28 #define TM_YEAR_BASE 1900
30 #define EPOCH_YEAR 1970
31 #define EPOCH_WDAY TM_THURSDAY
33 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
35 static const int mon_lengths
[2][MONSPERYEAR
] = {
36 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
37 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
40 static const int year_lengths
[2] = {
41 DAYSPERNYEAR
, DAYSPERLYEAR
45 timesub(timep
, offset
, tmp
)
46 const time_t * const timep
;
48 struct tm
* const tmp
;
56 days
= *timep
/ SECSPERDAY
;
57 rem
= *timep
% SECSPERDAY
;
63 while (rem
>= SECSPERDAY
) {
67 tmp
->tm_hour
= (int) (rem
/ SECSPERHOUR
);
68 rem
= rem
% SECSPERHOUR
;
69 tmp
->tm_min
= (int) (rem
/ SECSPERMIN
);
71 ** A positive leap second requires a special
72 ** representation. This uses "... ??:59:60" et seq.
74 tmp
->tm_sec
= (int) (rem
% SECSPERMIN
) ;
75 tmp
->tm_wday
= (int) ((EPOCH_WDAY
+ days
) % DAYSPERWEEK
);
77 tmp
->tm_wday
+= DAYSPERWEEK
;
79 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
80 while (days
< 0 || days
>= (long) year_lengths
[yleap
= isleap(y
)]) {
83 newy
= y
+ days
/ DAYSPERNYEAR
;
86 days
-= (newy
- y
) * DAYSPERNYEAR
+
87 LEAPS_THRU_END_OF(newy
- 1) -
88 LEAPS_THRU_END_OF(y
- 1);
91 tmp
->tm_year
= y
- TM_YEAR_BASE
;
92 tmp
->tm_yday
= (int) days
;
93 ip
= mon_lengths
[yleap
];
94 for (tmp
->tm_mon
= 0; days
>= (long) ip
[tmp
->tm_mon
]; ++(tmp
->tm_mon
))
95 days
= days
- (long) ip
[tmp
->tm_mon
];
96 tmp
->tm_mday
= (int) (days
+ 1);
101 * Re-entrant version of gmtime.
103 struct tm
* gmtime_r(const time_t* timep
, struct tm
*tm
)
105 timesub(timep
, 0L, tm
);