tmxtime.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder/***********************************************************************
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* This software is part of the ast package *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* Copyright (c) 1985-2011 AT&T Intellectual Property *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* and is licensed under the *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* Eclipse Public License, Version 1.0 *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* by AT&T Intellectual Property *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* A copy of the License is available at *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* http://www.eclipse.org/org/documents/epl-v10.html *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* Information and Software Systems Research *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* AT&T Research *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* Florham Park NJ *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder* Glenn Fowler <gsf@research.att.com> *
6b7d53eb1a93b9a9d7e3ce48964128fcd142fe53Christian Maeder* David Korn <dgk@research.att.com> *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* Phong Vo <kpv@research.att.com> *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder* *
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder***********************************************************************/
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder#pragma prototyped
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder/*
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * Glenn Fowler
2083784cdbe448a3d8f94c7a0a0a011bbabf3fd0Christian Maeder * AT&T Research
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * Time_t conversion support
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder */
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder#include <tmx.h>
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder#include "FEATURE/tmlib"
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder/*
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * convert Tm_t to Time_t
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * if west==TM_LOCALZONE then the local timezone is used
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * otherwise west is the number of minutes west
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * of GMT with DST taken into account
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder *
4ea69d9a971bbb8b4082bcd60350fdabd5411c23Christian Maeder * this routine works with a copy of Tm_t to avoid clashes
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maeder * with other tm*() that may return static Tm_t*
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maeder */
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maeder
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian MaederTime_t
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maedertmxtime(register Tm_t* tm, int west)
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maeder{
5334aa8fe0b0d1eb8a1cad40b741aa07172773c9Christian Maeder register Time_t t;
register Tm_leap_t* lp;
register int32_t y;
int n;
int sec;
time_t now;
struct tm* tl;
Tm_t* to;
Tm_t ts;
ts = *tm;
to = tm;
tm = &ts;
tmset(tm_info.zone);
tmfix(tm);
y = tm->tm_year;
if (y < 69 || y > (TMX_MAXYEAR - 1900))
return TMX_NOTIME;
y--;
t = y * 365 + y / 4 - y / 100 + (y + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
if ((n = tm->tm_mon) > 11)
n = 11;
y += 1901;
if (n > 1 && tmisleapyear(y))
t++;
t += tm_data.sum[n] + tm->tm_mday - 1;
t *= 24;
t += tm->tm_hour;
t *= 60;
t += tm->tm_min;
t *= 60;
t += sec = tm->tm_sec;
if (west != TM_UTCZONE && !(tm_info.flags & TM_UTC))
{
/*
* time zone adjustments
*/
if (west == TM_LOCALZONE)
{
t += tm_info.zone->west * 60;
if (!tm_info.zone->daylight)
tm->tm_isdst = 0;
else
{
y = tm->tm_year;
tm->tm_year = tmequiv(tm) - 1900;
now = tmxsec(tmxtime(tm, tm_info.zone->west));
tm->tm_year = y;
if (!(tl = tmlocaltime(&now)))
return TMX_NOTIME;
if (tm->tm_isdst = tl->tm_isdst)
t += tm_info.zone->dst * 60;
}
}
else
{
t += west * 60;
if (!tm_info.zone->daylight)
tm->tm_isdst = 0;
else if (tm->tm_isdst < 0)
{
y = tm->tm_year;
tm->tm_year = tmequiv(tm) - 1900;
tm->tm_isdst = 0;
now = tmxsec(tmxtime(tm, tm_info.zone->west));
tm->tm_year = y;
if (!(tl = tmlocaltime(&now)))
return TMX_NOTIME;
tm->tm_isdst = tl->tm_isdst;
}
}
}
else if (tm->tm_isdst)
tm->tm_isdst = 0;
*to = *tm;
if (tm_info.flags & TM_LEAP)
{
/*
* leap second adjustments
*/
for (lp = &tm_data.leap[0]; t < lp->time - (lp+1)->total; lp++);
t += lp->total;
n = lp->total - (lp+1)->total;
if (t <= (lp->time + n) && (n > 0 && sec > 59 || n < 0 && sec > (59 + n) && sec <= 59))
t -= n;
}
return tmxsns(t, tm->tm_nsec);
}