/******************************************************************************* * c0xlongtime.h * Test implementation for the ISO C proposal at: * , Revision 2.0. * * This code is designed to run reasonably well on Unix and Win32 systems. * * Notes * Compile with macro '_unix' defined for Unix systems. * Compile with macro '_POSIX' defined for POSIX systems. * * Author * This code was written by David R. Tribble (david@tribble.com), Jul 2004 * thru Mar 2006. * It is hereby placed into the public domain, and may be used for all * purposes, both commercial and private. No warranty is provided for this * source code, and the author cannot be held liable for its use in any * way. * * See also * * * * * NTP v.3 * SNTP v.4 * * * * * * * * * * Since * 2004-07-10 */ #ifndef c0x_longtime_h #define c0x_longtime_h 208 /* Identification */ #ifndef NO_H_IDENT static const char c0x_longtime_h_rev[] = "@(#)drt/text/stdc/c0xlongtime.h $Revision: 1.8 $ $Date: 2006/03/22 03:40:02 $"; #endif /* System includes */ #ifndef _stdc_stddef_h #include #endif #ifndef _stdc_limits_h #include #endif #ifndef _stdc_time_h #include #endif #if defined(_WIN32) && _MSC_VER < 1300 /* For older versions of MS C */ #define LLONG_MIN _I64_MIN #define LLONG_MAX _I64_MAX typedef __int64 c0x_llong_t; /* 64-bit signed int */ #else typedef long long int c0x_llong_t; /* 64-bit signed int */ #endif /* Local includes */ struct calendar; struct timezone; /******************************************************************************* * Public constants *******************************************************************************/ #define _LONGTIME_VS 20060321 /* longtime_t version */ #define __LONGTIME_MIN_LEAPSEC 22 /* Min long time leap seconds */ #define __LONGTIME_400YR_TICKS 6776803840072089600 #define _LONGTIME_ERROR ((longtime_t) LLONG_MIN) /* Erroneous time value */ #define _LONGTIME_MIN ((longtime_t) -6776803851883249664) /* Min longtime_t value */ #define _LONGTIME_MAX ((longtime_t) +6776803968921108479) /* Max longtime_t value */ #define _LONGTIME_NEVER ((longtime_t) LLONG_MAX) /* Nonexistent time value */ #if defined(_WIN32) #define _LONGTIME_RESOLUTION 1000L /* Long time resolution */ #elif defined(_unix) #define _LONGTIME_RESOLUTION 1000000L /* Long time resolution */ #elif defined(_POSIX) #define _LONGTIME_RESOLUTION 1L /* Long time resolution */ #else #define _LONGTIME_RESOLUTION 1L /* Long time resolution */ #endif #define _LONGTIME_HAS_LEAP_SECS 1 /* Has leap seconds */ #ifndef _TIME_ERROR #if defined(_WIN32) #define _TIME_ERROR ((time_t) -1) /* Erroneous time value */ #define _TIME_MIN ((time_t) 0) /* Min valid time_t */ #define _TIME_MAX ((time_t) INT_MAX) /* Max valid time_t */ #elif defined(_unix) #define _TIME_ERROR ((time_t) -1) /* Erroneous time value */ #define _TIME_MIN ((time_t) 0) /* Min valid time_t */ #define _TIME_MAX ((time_t) INT_MAX) /* Max valid time_t */ #else #error Undefined O/S #endif #endif #ifndef c0x_calendar_h #define _CAL_YR_ERROR INT_MIN /* cal_year: Error date */ #define _CAL_ERA_COMMON 0 /* cal_era: AD/CE */ #define _CAL_TYPE_GREGORIAN 0 /* Gregorian type */ #endif /******************************************************************************* * Public types *******************************************************************************/ typedef c0x_llong_t longtime_t; /* Long system time */ #ifndef c0x_calendar_h /*------------------------------------------------------------------------------ * struct calendar * Contains a component calendar date for a specific calendric type and * timezone. Also used to hold a component delta time. * * See also * struct tm, * c0xcalendar.h */ #define _CALENDAR_VS 20060311 struct calendar { int cal__vers; /* Struct version */ int cal_type; /* Calendar type (_CAL_TYPE_XXX) */ int cal_era; /* Era [0] */ int cal_year; /* Year [1900,2399] */ int cal_mon; /* Month [1,12] */ int cal_week; /* Week of year [1,53] */ int cal_mday; /* Day of month [1,31] */ int cal_yday; /* Day of year [1,366] */ int cal_wday; /* Day of week [1,7] */ int cal_hour; /* Hour [0,23] */ int cal_min; /* Minute [0,59] */ int cal_sec; /* Second [0,60] */ long int cal_nsec; /* Nanosecond [0,999999999] */ long int cal_nmons; /* Total months difference */ long int cal_nweeks; /* Total weeks difference */ long int cal_ndays; /* Total days difference */ int cal_dsti; /* DST index [-1,1] */ int cal_leapsec; /* Accumulated leap seconds */ const struct timezone * cal_zone; /* Timezone applied */ int cal__r[12]; /* (Reserved for future use) */ }; #endif /******************************************************************************* * Public functions *******************************************************************************/ /*------------------------------------------------------------------------------ * getlongtime() * Retrieve the current system time. * * Returns * The current system time as a long time, or '_LONGTIME_ERROR' if the * system time cannot be obtained. * * Since * 1.1, 2004-07-10 */ extern longtime_t getlongtime(void); /*------------------------------------------------------------------------------ * longtimeleapsecs() * Determine the total accumulated inserted and deleted leap seconds * contained in a long time value. * * Param t * A long system time. * * Returns * The total accumulated inserted and deleted leap seconds added to long * time value 't' if 't' is a valid long time value, otherwise INT_MIN. * * Since * 1.6, 2006-03-11 */ extern int longtimeleapsecs(longtime_t t); /*------------------------------------------------------------------------------ * setcalendartime() * Converts a long time value into a calendar date relative to a given * timezone. * * Param date * A component calendar date object, which will be filled with the * converted date. * * Param zone * A timezone object designating the timezone and DST rules to apply to the * converted date. This can be null, in which case GMT and no DST is * assumed. * * Param t * A long system time to convert. * * Returns * Zero if successful, otherwise -1. * * Since * 1.2, 2004-07-11 */ extern int setcalendartime(struct calendar *date, const struct timezone *zone, longtime_t t); /*------------------------------------------------------------------------------ * longtimetotime() * Converts a long time value into its corresponding 'time_t' value. * * Param t * A long system time to convert. * * Returns * The converted 'time_t' value if successful, otherwise '_TIME_ERROR'. * If the long time value cannot be converted, or the resulting converted * value exceeds the representational limits of the 'time_t' type, the * function fails. * * Since * 1.1, 2004-07-10 */ extern time_t longtimetotime(longtime_t t); /*------------------------------------------------------------------------------ * mklongtime() * Converts a calendar date into its corresponding long time value. * * Param date * Pointer to a component calendar date object. The members of this object * are normalized to fall within their normal value ranges prior to being * converted into a long time value. The following members of the * structure are used to perform the conversion: * cal_type * cal_era * cal_year * cal_mon * cal_mday * cal_hour * cal_min * cal_sec * cal_nsec * * These members can contain values outside their normal range of values. * The remaining members of the structure are ignored for the conversion. * * Param zone * Pointer to a timezone object containing timezone and DST adjustments * that have been made to the date represented by the calendar date object. * The timezone and DST adjustments are applied in reverse (i.e., undone) * on the calendar date before it is converted into a long time value. * This can (and should) be the same object as 'date->cal_zone'. * This can be null, in which case GMT and no DST is assumed. * * Returns * If successful, the function returns the resulting long time value, * otherwise it returns '_LONGTIME_ERROR'. If the calendar date does not * represent a meaningful date, or cannot be converted into a valid long * time value after adjustments have been applied, the function fails. * * Since * 1.1, 2004-07-10 */ extern longtime_t mklongtime(struct calendar *date, const struct timezone *zone); /*------------------------------------------------------------------------------ * timetolongtime() * Converts a 'time_t' value into its corresponding long system time value. * * Param t * A system time to convert. * * Returns * The converted long time value if successful, otherwise * '_LONGTIME_ERROR'. If the time value cannot be converted, or the * resulting converted value exceeds the representational limits of the * 'longtime_t' type, the function fails. * * Since * 1.1, 2004-07-10 */ extern longtime_t timetolongtime(time_t t); #endif /* c0x_longtime_h */ /* End c0xlongtime.h */