1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 * Time_t conversion support
32 #include "FEATURE/tmlib"
35 * convert Tm_t to Time_t
37 * if west==TM_LOCALZONE then the local timezone is used
38 * otherwise west is the number of minutes west
39 * of GMT with DST taken into account
41 * this routine works with a copy of Tm_t to avoid clashes
42 * with other tm*() that may return static Tm_t*
46 tmxtime(register Tm_t
* tm
, int west
)
49 register Tm_leap_t
* lp
;
64 if (y
< 69 || y
> (TMX_MAXYEAR
- 1900))
67 t
= y
* 365 + y
/ 4 - y
/ 100 + (y
+ (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
68 if ((n
= tm
->tm_mon
) > 11)
71 if (n
> 1 && tmisleapyear(y
))
73 t
+= tm_data
.sum
[n
] + tm
->tm_mday
- 1;
79 t
+= sec
= tm
->tm_sec
;
80 if (west
!= TM_UTCZONE
&& !(tm_info
.flags
& TM_UTC
))
83 * time zone adjustments
86 if (west
== TM_LOCALZONE
)
88 t
+= tm_info
.zone
->west
* 60;
89 if (!tm_info
.zone
->daylight
)
94 tm
->tm_year
= tmequiv(tm
) - 1900;
95 now
= tmxsec(tmxtime(tm
, tm_info
.zone
->west
));
97 if (!(tl
= tmlocaltime(&now
)))
99 if (tm
->tm_isdst
= tl
->tm_isdst
)
100 t
+= tm_info
.zone
->dst
* 60;
106 if (!tm_info
.zone
->daylight
)
108 else if (tm
->tm_isdst
< 0)
111 tm
->tm_year
= tmequiv(tm
) - 1900;
113 now
= tmxsec(tmxtime(tm
, tm_info
.zone
->west
));
115 if (!(tl
= tmlocaltime(&now
)))
117 tm
->tm_isdst
= tl
->tm_isdst
;
121 else if (tm
->tm_isdst
)
124 if (tm_info
.flags
& TM_LEAP
)
127 * leap second adjustments
130 for (lp
= &tm_data
.leap
[0]; t
< lp
->time
- (lp
+1)->total
; lp
++);
132 n
= lp
->total
- (lp
+1)->total
;
133 if (t
<= (lp
->time
+ n
) && (n
> 0 && sec
> 59 || n
< 0 && sec
> (59 + n
) && sec
<= 59))
136 return tmxsns(t
, tm
->tm_nsec
);