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