/contrib/sdk/sources/newlib/time/asctime.c |
---|
0,0 → 1,64 |
/* |
* asctime.c |
* Original Author: G. Haley |
* |
* Converts the broken down time in the structure pointed to by tim_p into a |
* string of the form |
* |
* Wed Jun 15 11:38:07 1988\n\0 |
* |
* Returns a pointer to the string. |
*/ |
/* |
FUNCTION |
<<asctime>>---format time as string |
INDEX |
asctime |
INDEX |
_asctime_r |
ANSI_SYNOPSIS |
#include <time.h> |
char *asctime(const struct tm *<[clock]>); |
char *_asctime_r(const struct tm *<[clock]>, char *<[buf]>); |
TRAD_SYNOPSIS |
#include <time.h> |
char *asctime(<[clock]>) |
struct tm *<[clock]>; |
char *asctime_r(<[clock]>) |
struct tm *<[clock]>; |
char *<[buf]>; |
DESCRIPTION |
Format the time value at <[clock]> into a string of the form |
. Wed Jun 15 11:38:07 1988\n\0 |
The string is generated in a static buffer; each call to <<asctime>> |
overwrites the string generated by previous calls. |
RETURNS |
A pointer to the string containing a formatted timestamp. |
PORTABILITY |
ANSI C requires <<asctime>>. |
<<asctime>> requires no supporting OS subroutines. |
*/ |
#include <time.h> |
#include <_ansi.h> |
#include <reent.h> |
#ifndef _REENT_ONLY |
char * |
_DEFUN (asctime, (tim_p), |
_CONST struct tm *tim_p) |
{ |
_REENT_CHECK_ASCTIME_BUF(_REENT); |
return asctime_r (tim_p, _REENT_ASCTIME_BUF(_REENT)); |
} |
#endif |
/contrib/sdk/sources/newlib/time/asctime_r.c |
---|
0,0 → 1,27 |
/* |
* asctime_r.c |
*/ |
#include <stdio.h> |
#include <time.h> |
char * |
_DEFUN (asctime_r, (tim_p, result), |
_CONST struct tm *tim_p _AND |
char *result) |
{ |
static _CONST char day_name[7][3] = { |
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
}; |
static _CONST char mon_name[12][3] = { |
"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
}; |
siprintf (result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", |
day_name[tim_p->tm_wday], |
mon_name[tim_p->tm_mon], |
tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min, |
tim_p->tm_sec, 1900 + tim_p->tm_year); |
return result; |
} |
/contrib/sdk/sources/newlib/time/clock.c |
---|
0,0 → 1,78 |
/* NetWare can not use this implementation of clock, since it does not |
have times or any similar function. It provides its own version of |
clock in clib.nlm. If we can not use clib.nlm, then we must write |
clock in sys/netware. */ |
#ifdef CLOCK_PROVIDED |
int _dummy_clock = 1; |
#else |
/* |
* clock.c |
* Original Author: G. Haley |
* |
* Determines the processor time used by the program since invocation. The time |
* in seconds is the value returned divided by the value of the macro CLK_TCK. |
* If the processor time used is not available, (clock_t) -1 is returned. |
*/ |
/* |
FUNCTION |
<<clock>>---cumulative processor time |
INDEX |
clock |
ANSI_SYNOPSIS |
#include <time.h> |
clock_t clock(void); |
TRAD_SYNOPSIS |
#include <time.h> |
clock_t clock(); |
DESCRIPTION |
Calculates the best available approximation of the cumulative amount |
of time used by your program since it started. To convert the result |
into seconds, divide by the macro <<CLOCKS_PER_SEC>>. |
RETURNS |
The amount of processor time used so far by your program, in units |
defined by the machine-dependent macro <<CLOCKS_PER_SEC>>. If no |
measurement is available, the result is (clock_t)<<-1>>. |
PORTABILITY |
ANSI C requires <<clock>> and <<CLOCKS_PER_SEC>>. |
Supporting OS subroutine required: <<times>>. |
*/ |
#include <time.h> |
#include <sys/times.h> |
#include <reent.h> |
clock_t |
_DEFUN (_times_r, (ptr, ptms), |
struct _reent *ptr _AND |
struct tms *ptms) |
{ |
return -1; |
} |
clock_t |
clock () |
{ |
struct tms tim_s; |
clock_t res; |
if ((res = (clock_t) _times_r (_REENT, &tim_s)) != -1) |
res = (clock_t) (tim_s.tms_utime + tim_s.tms_stime + |
tim_s.tms_cutime + tim_s.tms_cstime); |
return res; |
} |
#endif /* CLOCK_PROVIDED */ |
/contrib/sdk/sources/newlib/time/ctime.c |
---|
0,0 → 1,55 |
/* |
* ctime.c |
* Original Author: G. Haley |
*/ |
/* |
FUNCTION |
<<ctime>>---convert time to local and format as string |
INDEX |
ctime |
INDEX |
ctime_r |
ANSI_SYNOPSIS |
#include <time.h> |
char *ctime(const time_t *<[clock]>); |
char *ctime_r(const time_t *<[clock]>, char *<[buf]>); |
TRAD_SYNOPSIS |
#include <time.h> |
char *ctime(<[clock]>) |
time_t *<[clock]>; |
char *ctime_r(<[clock]>, <[buf]>) |
time_t *<[clock]>; |
char *<[buf]>; |
DESCRIPTION |
Convert the time value at <[clock]> to local time (like <<localtime>>) |
and format it into a string of the form |
. Wed Jun 15 11:38:07 1988\n\0 |
(like <<asctime>>). |
RETURNS |
A pointer to the string containing a formatted timestamp. |
PORTABILITY |
ANSI C requires <<ctime>>. |
<<ctime>> requires no supporting OS subroutines. |
*/ |
#include <time.h> |
#ifndef _REENT_ONLY |
char * |
_DEFUN (ctime, (tim_p), |
_CONST time_t * tim_p) |
{ |
return asctime (localtime (tim_p)); |
} |
#endif |
/contrib/sdk/sources/newlib/time/ctime_r.c |
---|
0,0 → 1,15 |
/* |
* ctime_r.c |
*/ |
#include <time.h> |
char * |
_DEFUN (ctime_r, (tim_p, result), |
_CONST time_t * tim_p _AND |
char * result) |
{ |
struct tm tm; |
return asctime_r (localtime_r (tim_p, &tm), result); |
} |
/contrib/sdk/sources/newlib/time/difftime.c |
---|
0,0 → 1,44 |
/* |
* difftime.c |
* Original Author: G. Haley |
*/ |
/* |
FUNCTION |
<<difftime>>---subtract two times |
INDEX |
difftime |
ANSI_SYNOPSIS |
#include <time.h> |
double difftime(time_t <[tim1]>, time_t <[tim2]>); |
TRAD_SYNOPSIS |
#include <time.h> |
double difftime(<[tim1]>, <[tim2]>) |
time_t <[tim1]>; |
time_t <[tim2]>; |
DESCRIPTION |
Subtracts the two times in the arguments: `<<<[tim1]> - <[tim2]>>>'. |
RETURNS |
The difference (in seconds) between <[tim2]> and <[tim1]>, as a <<double>>. |
PORTABILITY |
ANSI C requires <<difftime>>, and defines its result to be in seconds |
in all implementations. |
<<difftime>> requires no supporting OS subroutines. |
*/ |
#include <time.h> |
double |
_DEFUN (difftime, (tim1, tim2), |
time_t tim1 _AND |
time_t tim2) |
{ |
return (double)(tim1 - tim2); |
} |
/contrib/sdk/sources/newlib/time/gettzinfo.c |
---|
0,0 → 1,15 |
#include <sys/types.h> |
#include "local.h" |
/* Shared timezone information for libc/time functions. */ |
static __tzinfo_type tzinfo = {1, 0, |
{ {'J', 0, 0, 0, 0, (time_t)0, 0L }, |
{'J', 0, 0, 0, 0, (time_t)0, 0L } |
} |
}; |
__tzinfo_type * |
__gettzinfo (void) |
{ |
return &tzinfo; |
} |
/contrib/sdk/sources/newlib/time/gmtime.c |
---|
0,0 → 1,68 |
/* |
* gmtime.c |
* Original Author: G. Haley |
* |
* Converts the calendar time pointed to by tim_p into a broken-down time |
* expressed as Greenwich Mean Time (GMT). Returns a pointer to a structure |
* containing the broken-down time, or a null pointer if GMT is not |
* available. |
*/ |
/* |
FUNCTION |
<<gmtime>>---convert time to UTC traditional form |
INDEX |
gmtime |
INDEX |
gmtime_r |
ANSI_SYNOPSIS |
#include <time.h> |
struct tm *gmtime(const time_t *<[clock]>); |
struct tm *gmtime_r(const time_t *<[clock]>, struct tm *<[res]>); |
TRAD_SYNOPSIS |
#include <time.h> |
struct tm *gmtime(<[clock]>) |
const time_t *<[clock]>; |
struct tm *gmtime_r(<[clock]>, <[res]>) |
const time_t *<[clock]>; |
struct tm *<[res]>; |
DESCRIPTION |
<<gmtime>> takes the time at <[clock]> representing the number |
of elapsed seconds since 00:00:00 on January 1, 1970, Universal |
Coordinated Time (UTC, also known in some countries as GMT, |
Greenwich Mean time) and converts it to a <<struct tm>> |
representation. |
<<gmtime>> constructs the traditional time representation in static |
storage; each call to <<gmtime>> or <<localtime>> will overwrite the |
information generated by previous calls to either function. |
RETURNS |
A pointer to the traditional time representation (<<struct tm>>). |
PORTABILITY |
ANSI C requires <<gmtime>>. |
<<gmtime>> requires no supporting OS subroutines. |
*/ |
#include <stdlib.h> |
#include <time.h> |
#define _GMT_OFFSET 0 |
#ifndef _REENT_ONLY |
struct tm * |
_DEFUN (gmtime, (tim_p), |
_CONST time_t * tim_p) |
{ |
_REENT_CHECK_TM(_REENT); |
return gmtime_r (tim_p, (struct tm *)_REENT_TM(_REENT)); |
} |
#endif |
/contrib/sdk/sources/newlib/time/gmtime_r.c |
---|
0,0 → 1,14 |
/* |
* gmtime_r.c |
*/ |
#include <time.h> |
#include "local.h" |
struct tm * |
_DEFUN (gmtime_r, (tim_p, res), |
_CONST time_t * tim_p _AND |
struct tm *res) |
{ |
return (_mktm_r (tim_p, res, 1)); |
} |
/contrib/sdk/sources/newlib/time/lcltime.c |
---|
0,0 → 1,60 |
/* |
* localtime.c |
*/ |
/* |
FUNCTION |
<<localtime>>---convert time to local representation |
INDEX |
localtime |
INDEX |
localtime_r |
ANSI_SYNOPSIS |
#include <time.h> |
struct tm *localtime(time_t *<[clock]>); |
struct tm *localtime_r(time_t *<[clock]>, struct tm *<[res]>); |
TRAD_SYNOPSIS |
#include <time.h> |
struct tm *localtime(<[clock]>) |
time_t *<[clock]>; |
struct tm *localtime(<[clock]>, <[res]>) |
time_t *<[clock]>; |
struct tm *<[res]>; |
DESCRIPTION |
<<localtime>> converts the time at <[clock]> into local time, then |
converts its representation from the arithmetic representation to the |
traditional representation defined by <<struct tm>>. |
<<localtime>> constructs the traditional time representation in static |
storage; each call to <<gmtime>> or <<localtime>> will overwrite the |
information generated by previous calls to either function. |
<<mktime>> is the inverse of <<localtime>>. |
RETURNS |
A pointer to the traditional time representation (<<struct tm>>). |
PORTABILITY |
ANSI C requires <<localtime>>. |
<<localtime>> requires no supporting OS subroutines. |
*/ |
#include <time.h> |
#include <reent.h> |
#ifndef _REENT_ONLY |
struct tm * |
_DEFUN (localtime, (tim_p), |
_CONST time_t * tim_p) |
{ |
_REENT_CHECK_TM(_REENT); |
return localtime_r (tim_p, (struct tm *)_REENT_TM(_REENT)); |
} |
#endif |
/contrib/sdk/sources/newlib/time/lcltime_r.c |
---|
0,0 → 1,18 |
/* |
* localtime_r.c |
* |
* Converts the calendar time pointed to by tim_p into a broken-down time |
* expressed as local time. Returns a pointer to a structure containing the |
* broken-down time. |
*/ |
#include <time.h> |
#include "local.h" |
struct tm * |
_DEFUN (localtime_r, (tim_p, res), |
_CONST time_t * tim_p _AND |
struct tm *res) |
{ |
return _mktm_r (tim_p, res, 0); |
} |
/contrib/sdk/sources/newlib/time/local.h |
---|
0,0 → 1,36 |
/* local header used by libc/time routines */ |
#include <_ansi.h> |
#include <time.h> |
#define SECSPERMIN 60L |
#define MINSPERHOUR 60L |
#define HOURSPERDAY 24L |
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) |
#define SECSPERDAY (SECSPERHOUR * HOURSPERDAY) |
#define DAYSPERWEEK 7 |
#define MONSPERYEAR 12 |
#define YEAR_BASE 1900 |
#define EPOCH_YEAR 1970 |
#define EPOCH_WDAY 4 |
#define EPOCH_YEARS_SINCE_LEAP 2 |
#define EPOCH_YEARS_SINCE_CENTURY 70 |
#define EPOCH_YEARS_SINCE_LEAP_CENTURY 370 |
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) |
struct tm * _EXFUN (_mktm_r, (_CONST time_t *, struct tm *, int __is_gmtime)); |
int _EXFUN (__tzcalc_limits, (int __year)); |
/* locks for multi-threading */ |
#ifdef __SINGLE_THREAD__ |
#define TZ_LOCK |
#define TZ_UNLOCK |
#else |
#define TZ_LOCK __tz_lock() |
#define TZ_UNLOCK __tz_unlock() |
#endif |
void _EXFUN(__tz_lock,(_VOID)); |
void _EXFUN(__tz_unlock,(_VOID)); |
/contrib/sdk/sources/newlib/time/mktime.c |
---|
0,0 → 1,287 |
/* |
* mktime.c |
* Original Author: G. Haley |
* |
* Converts the broken-down time, expressed as local time, in the structure |
* pointed to by tim_p into a calendar time value. The original values of the |
* tm_wday and tm_yday fields of the structure are ignored, and the original |
* values of the other fields have no restrictions. On successful completion |
* the fields of the structure are set to represent the specified calendar |
* time. Returns the specified calendar time. If the calendar time can not be |
* represented, returns the value (time_t) -1. |
* |
* Modifications: Fixed tm_isdst usage - 27 August 2008 Craig Howland. |
*/ |
/* |
FUNCTION |
<<mktime>>---convert time to arithmetic representation |
INDEX |
mktime |
ANSI_SYNOPSIS |
#include <time.h> |
time_t mktime(struct tm *<[timp]>); |
TRAD_SYNOPSIS |
#include <time.h> |
time_t mktime(<[timp]>) |
struct tm *<[timp]>; |
DESCRIPTION |
<<mktime>> assumes the time at <[timp]> is a local time, and converts |
its representation from the traditional representation defined by |
<<struct tm>> into a representation suitable for arithmetic. |
<<localtime>> is the inverse of <<mktime>>. |
RETURNS |
If the contents of the structure at <[timp]> do not form a valid |
calendar time representation, the result is <<-1>>. Otherwise, the |
result is the time, converted to a <<time_t>> value. |
PORTABILITY |
ANSI C requires <<mktime>>. |
<<mktime>> requires no supporting OS subroutines. |
*/ |
#include <stdlib.h> |
#include <time.h> |
#include "local.h" |
#define _SEC_IN_MINUTE 60L |
#define _SEC_IN_HOUR 3600L |
#define _SEC_IN_DAY 86400L |
static _CONST int DAYS_IN_MONTH[12] = |
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
#define _DAYS_IN_MONTH(x) ((x == 1) ? days_in_feb : DAYS_IN_MONTH[x]) |
static _CONST int _DAYS_BEFORE_MONTH[12] = |
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
#define _ISLEAP(y) (((y) % 4) == 0 && (((y) % 100) != 0 || (((y)+1900) % 400) == 0)) |
#define _DAYS_IN_YEAR(year) (_ISLEAP(year) ? 366 : 365) |
static void |
_DEFUN(validate_structure, (tim_p), |
struct tm *tim_p) |
{ |
div_t res; |
int days_in_feb = 28; |
/* calculate time & date to account for out of range values */ |
if (tim_p->tm_sec < 0 || tim_p->tm_sec > 59) |
{ |
res = div (tim_p->tm_sec, 60); |
tim_p->tm_min += res.quot; |
if ((tim_p->tm_sec = res.rem) < 0) |
{ |
tim_p->tm_sec += 60; |
--tim_p->tm_min; |
} |
} |
if (tim_p->tm_min < 0 || tim_p->tm_min > 59) |
{ |
res = div (tim_p->tm_min, 60); |
tim_p->tm_hour += res.quot; |
if ((tim_p->tm_min = res.rem) < 0) |
{ |
tim_p->tm_min += 60; |
--tim_p->tm_hour; |
} |
} |
if (tim_p->tm_hour < 0 || tim_p->tm_hour > 23) |
{ |
res = div (tim_p->tm_hour, 24); |
tim_p->tm_mday += res.quot; |
if ((tim_p->tm_hour = res.rem) < 0) |
{ |
tim_p->tm_hour += 24; |
--tim_p->tm_mday; |
} |
} |
if (tim_p->tm_mon < 0 || tim_p->tm_mon > 11) |
{ |
res = div (tim_p->tm_mon, 12); |
tim_p->tm_year += res.quot; |
if ((tim_p->tm_mon = res.rem) < 0) |
{ |
tim_p->tm_mon += 12; |
--tim_p->tm_year; |
} |
} |
if (_DAYS_IN_YEAR (tim_p->tm_year) == 366) |
days_in_feb = 29; |
if (tim_p->tm_mday <= 0) |
{ |
while (tim_p->tm_mday <= 0) |
{ |
if (--tim_p->tm_mon == -1) |
{ |
tim_p->tm_year--; |
tim_p->tm_mon = 11; |
days_in_feb = |
((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ? |
29 : 28); |
} |
tim_p->tm_mday += _DAYS_IN_MONTH (tim_p->tm_mon); |
} |
} |
else |
{ |
while (tim_p->tm_mday > _DAYS_IN_MONTH (tim_p->tm_mon)) |
{ |
tim_p->tm_mday -= _DAYS_IN_MONTH (tim_p->tm_mon); |
if (++tim_p->tm_mon == 12) |
{ |
tim_p->tm_year++; |
tim_p->tm_mon = 0; |
days_in_feb = |
((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ? |
29 : 28); |
} |
} |
} |
} |
time_t |
_DEFUN(mktime, (tim_p), |
struct tm *tim_p) |
{ |
time_t tim = 0; |
long days = 0; |
int year, isdst=0; |
__tzinfo_type *tz = __gettzinfo (); |
/* validate structure */ |
validate_structure (tim_p); |
/* compute hours, minutes, seconds */ |
tim += tim_p->tm_sec + (tim_p->tm_min * _SEC_IN_MINUTE) + |
(tim_p->tm_hour * _SEC_IN_HOUR); |
/* compute days in year */ |
days += tim_p->tm_mday - 1; |
days += _DAYS_BEFORE_MONTH[tim_p->tm_mon]; |
if (tim_p->tm_mon > 1 && _DAYS_IN_YEAR (tim_p->tm_year) == 366) |
days++; |
/* compute day of the year */ |
tim_p->tm_yday = days; |
if (tim_p->tm_year > 10000 || tim_p->tm_year < -10000) |
return (time_t) -1; |
/* compute days in other years */ |
if ((year = tim_p->tm_year) > 70) |
{ |
for (year = 70; year < tim_p->tm_year; year++) |
days += _DAYS_IN_YEAR (year); |
} |
else if (year < 70) |
{ |
for (year = 69; year > tim_p->tm_year; year--) |
days -= _DAYS_IN_YEAR (year); |
days -= _DAYS_IN_YEAR (year); |
} |
/* compute total seconds */ |
tim += (days * _SEC_IN_DAY); |
TZ_LOCK; |
if (_daylight) |
{ |
int tm_isdst; |
int y = tim_p->tm_year + YEAR_BASE; |
/* Convert user positive into 1 */ |
tm_isdst = tim_p->tm_isdst > 0 ? 1 : tim_p->tm_isdst; |
isdst = tm_isdst; |
if (y == tz->__tzyear || __tzcalc_limits (y)) |
{ |
/* calculate start of dst in dst local time and |
start of std in both std local time and dst local time */ |
time_t startdst_dst = tz->__tzrule[0].change |
- (time_t) tz->__tzrule[1].offset; |
time_t startstd_dst = tz->__tzrule[1].change |
- (time_t) tz->__tzrule[1].offset; |
time_t startstd_std = tz->__tzrule[1].change |
- (time_t) tz->__tzrule[0].offset; |
/* if the time is in the overlap between dst and std local times */ |
if (tim >= startstd_std && tim < startstd_dst) |
; /* we let user decide or leave as -1 */ |
else |
{ |
isdst = (tz->__tznorth |
? (tim >= startdst_dst && tim < startstd_std) |
: (tim >= startdst_dst || tim < startstd_std)); |
/* if user committed and was wrong, perform correction, but not |
* if the user has given a negative value (which |
* asks mktime() to determine if DST is in effect or not) */ |
if (tm_isdst >= 0 && (isdst ^ tm_isdst) == 1) |
{ |
/* we either subtract or add the difference between |
time zone offsets, depending on which way the user got it |
wrong. The diff is typically one hour, or 3600 seconds, |
and should fit in a 16-bit int, even though offset |
is a long to accomodate 12 hours. */ |
int diff = (int) (tz->__tzrule[0].offset |
- tz->__tzrule[1].offset); |
if (!isdst) |
diff = -diff; |
tim_p->tm_sec += diff; |
tim += diff; /* we also need to correct our current time calculation */ |
int mday = tim_p->tm_mday; |
validate_structure (tim_p); |
mday = tim_p->tm_mday - mday; |
/* roll over occurred */ |
if (mday) { |
/* compensate for month roll overs */ |
if (mday > 1) |
mday = -1; |
else if (mday < -1) |
mday = 1; |
/* update days for wday calculation */ |
days += mday; |
/* handle yday */ |
if ((tim_p->tm_yday += mday) < 0) { |
--year; |
tim_p->tm_yday = _DAYS_IN_YEAR(year) - 1; |
} else { |
mday = _DAYS_IN_YEAR(year); |
if (tim_p->tm_yday > (mday - 1)) |
tim_p->tm_yday -= mday; |
} |
} |
} |
} |
} |
} |
/* add appropriate offset to put time in gmt format */ |
if (isdst == 1) |
tim += (time_t) tz->__tzrule[1].offset; |
else /* otherwise assume std time */ |
tim += (time_t) tz->__tzrule[0].offset; |
TZ_UNLOCK; |
/* reset isdst flag to what we have calculated */ |
tim_p->tm_isdst = isdst; |
/* compute day of the week */ |
if ((tim_p->tm_wday = (days + 4) % 7) < 0) |
tim_p->tm_wday += 7; |
return tim; |
} |
/contrib/sdk/sources/newlib/time/mktm_r.c |
---|
0,0 → 1,259 |
/* |
* mktm_r.c |
* Original Author: Adapted from tzcode maintained by Arthur David Olson. |
* Modifications: Changed to mktm_r and added __tzcalc_limits - 04/10/02, Jeff Johnston |
* Fixed bug in mday computations - 08/12/04, Alex Mogilnikov <alx@intellectronika.ru> |
* Fixed bug in __tzcalc_limits - 08/12/04, Alex Mogilnikov <alx@intellectronika.ru> |
* |
* Converts the calendar time pointed to by tim_p into a broken-down time |
* expressed as local time. Returns a pointer to a structure containing the |
* broken-down time. |
*/ |
#include <stdlib.h> |
#include <time.h> |
#include "local.h" |
static _CONST int mon_lengths[2][MONSPERYEAR] = { |
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
} ; |
static _CONST int year_lengths[2] = { |
365, |
366 |
} ; |
struct tm * |
_DEFUN (_mktm_r, (tim_p, res, is_gmtime), |
_CONST time_t * tim_p _AND |
struct tm *res _AND |
int is_gmtime) |
{ |
long days, rem; |
time_t lcltime; |
int y; |
int yleap; |
_CONST int *ip; |
__tzinfo_type *tz = __gettzinfo (); |
/* base decision about std/dst time on current time */ |
lcltime = *tim_p; |
days = ((long)lcltime) / SECSPERDAY; |
rem = ((long)lcltime) % SECSPERDAY; |
while (rem < 0) |
{ |
rem += SECSPERDAY; |
--days; |
} |
while (rem >= SECSPERDAY) |
{ |
rem -= SECSPERDAY; |
++days; |
} |
/* compute hour, min, and sec */ |
res->tm_hour = (int) (rem / SECSPERHOUR); |
rem %= SECSPERHOUR; |
res->tm_min = (int) (rem / SECSPERMIN); |
res->tm_sec = (int) (rem % SECSPERMIN); |
/* compute day of week */ |
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0) |
res->tm_wday += DAYSPERWEEK; |
/* compute year & day of year */ |
y = EPOCH_YEAR; |
if (days >= 0) |
{ |
for (;;) |
{ |
yleap = isleap(y); |
if (days < year_lengths[yleap]) |
break; |
y++; |
days -= year_lengths[yleap]; |
} |
} |
else |
{ |
do |
{ |
--y; |
yleap = isleap(y); |
days += year_lengths[yleap]; |
} while (days < 0); |
} |
res->tm_year = y - YEAR_BASE; |
res->tm_yday = days; |
ip = mon_lengths[yleap]; |
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon) |
days -= ip[res->tm_mon]; |
res->tm_mday = days + 1; |
if (!is_gmtime) |
{ |
long offset; |
int hours, mins, secs; |
TZ_LOCK; |
if (_daylight) |
{ |
if (y == tz->__tzyear || __tzcalc_limits (y)) |
res->tm_isdst = (tz->__tznorth |
? (*tim_p >= tz->__tzrule[0].change |
&& *tim_p < tz->__tzrule[1].change) |
: (*tim_p >= tz->__tzrule[0].change |
|| *tim_p < tz->__tzrule[1].change)); |
else |
res->tm_isdst = -1; |
} |
else |
res->tm_isdst = 0; |
offset = (res->tm_isdst == 1 |
? tz->__tzrule[1].offset |
: tz->__tzrule[0].offset); |
hours = (int) (offset / SECSPERHOUR); |
offset = offset % SECSPERHOUR; |
mins = (int) (offset / SECSPERMIN); |
secs = (int) (offset % SECSPERMIN); |
res->tm_sec -= secs; |
res->tm_min -= mins; |
res->tm_hour -= hours; |
if (res->tm_sec >= SECSPERMIN) |
{ |
res->tm_min += 1; |
res->tm_sec -= SECSPERMIN; |
} |
else if (res->tm_sec < 0) |
{ |
res->tm_min -= 1; |
res->tm_sec += SECSPERMIN; |
} |
if (res->tm_min >= MINSPERHOUR) |
{ |
res->tm_hour += 1; |
res->tm_min -= MINSPERHOUR; |
} |
else if (res->tm_min < 0) |
{ |
res->tm_hour -= 1; |
res->tm_min += MINSPERHOUR; |
} |
if (res->tm_hour >= HOURSPERDAY) |
{ |
++res->tm_yday; |
++res->tm_wday; |
if (res->tm_wday > 6) |
res->tm_wday = 0; |
++res->tm_mday; |
res->tm_hour -= HOURSPERDAY; |
if (res->tm_mday > ip[res->tm_mon]) |
{ |
res->tm_mday -= ip[res->tm_mon]; |
res->tm_mon += 1; |
if (res->tm_mon == 12) |
{ |
res->tm_mon = 0; |
res->tm_year += 1; |
res->tm_yday = 0; |
} |
} |
} |
else if (res->tm_hour < 0) |
{ |
res->tm_yday -= 1; |
res->tm_wday -= 1; |
if (res->tm_wday < 0) |
res->tm_wday = 6; |
res->tm_mday -= 1; |
res->tm_hour += 24; |
if (res->tm_mday == 0) |
{ |
res->tm_mon -= 1; |
if (res->tm_mon < 0) |
{ |
res->tm_mon = 11; |
res->tm_year -= 1; |
res->tm_yday = 364 + isleap(res->tm_year + 1900); |
} |
res->tm_mday = ip[res->tm_mon]; |
} |
} |
TZ_UNLOCK; |
} |
else |
res->tm_isdst = 0; |
return (res); |
} |
int |
_DEFUN (__tzcalc_limits, (year), |
int year) |
{ |
int days, year_days, years; |
int i, j; |
__tzinfo_type *tz = __gettzinfo (); |
if (year < EPOCH_YEAR) |
return 0; |
tz->__tzyear = year; |
years = (year - EPOCH_YEAR); |
year_days = years * 365 + |
(years - 1 + EPOCH_YEARS_SINCE_LEAP) / 4 - (years - 1 + EPOCH_YEARS_SINCE_CENTURY) / 100 + |
(years - 1 + EPOCH_YEARS_SINCE_LEAP_CENTURY) / 400; |
for (i = 0; i < 2; ++i) |
{ |
if (tz->__tzrule[i].ch == 'J') { |
/* The Julian day n (1 <= n <= 365). */ |
days = year_days + tz->__tzrule[i].d + |
(isleap(year) && tz->__tzrule[i].d >= 60); |
/* Convert to yday */ |
--days; |
} else if (tz->__tzrule[i].ch == 'D') |
days = year_days + tz->__tzrule[i].d; |
else |
{ |
int yleap = isleap(year); |
int m_day, m_wday, wday_diff; |
_CONST int *ip = mon_lengths[yleap]; |
days = year_days; |
for (j = 1; j < tz->__tzrule[i].m; ++j) |
days += ip[j-1]; |
m_wday = (EPOCH_WDAY + days) % DAYSPERWEEK; |
wday_diff = tz->__tzrule[i].d - m_wday; |
if (wday_diff < 0) |
wday_diff += DAYSPERWEEK; |
m_day = (tz->__tzrule[i].n - 1) * DAYSPERWEEK + wday_diff; |
while (m_day >= ip[j-1]) |
m_day -= DAYSPERWEEK; |
days += m_day; |
} |
/* store the change-over time in GMT form by adding offset */ |
tz->__tzrule[i].change = days * SECSPERDAY + |
tz->__tzrule[i].s + tz->__tzrule[i].offset; |
} |
tz->__tznorth = (tz->__tzrule[0].change < tz->__tzrule[1].change); |
return 1; |
} |
/contrib/sdk/sources/newlib/time/strftime.c |
---|
0,0 → 1,333 |
/* NOTE: This file defines both strftime() and wcsftime(). Take care when |
* making changes. See also wcsftime.c, and note the (small) overlap in the |
* manual description, taking care to edit both as needed. */ |
/* |
* strftime.c |
* Original Author: G. Haley |
* Additions from: Eric Blake |
* Changes to allow dual use as wcstime, also: Craig Howland |
* |
* Places characters into the array pointed to by s as controlled by the string |
* pointed to by format. If the total number of resulting characters including |
* the terminating null character is not more than maxsize, returns the number |
* of characters placed into the array pointed to by s (not including the |
* terminating null character); otherwise zero is returned and the contents of |
* the array indeterminate. |
*/ |
/* |
FUNCTION |
<<strftime>>---convert date and time to a formatted string |
INDEX |
strftime |
ANSI_SYNOPSIS |
#include <time.h> |
size_t strftime(char *<[s]>, size_t <[maxsize]>, |
const char *<[format]>, const struct tm *<[timp]>); |
TRAD_SYNOPSIS |
#include <time.h> |
size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>) |
char *<[s]>; |
size_t <[maxsize]>; |
char *<[format]>; |
struct tm *<[timp]>; |
DESCRIPTION |
<<strftime>> converts a <<struct tm>> representation of the time (at |
<[timp]>) into a null-terminated string, starting at <[s]> and occupying |
no more than <[maxsize]> characters. |
You control the format of the output using the string at <[format]>. |
<<*<[format]>>> can contain two kinds of specifications: text to be |
copied literally into the formatted string, and time conversion |
specifications. Time conversion specifications are two- and |
three-character sequences beginning with `<<%>>' (use `<<%%>>' to |
include a percent sign in the output). Each defined conversion |
specification selects only the specified field(s) of calendar time |
data from <<*<[timp]>>>, and converts it to a string in one of the |
following ways: |
o+ |
o %a |
The abbreviated weekday name according to the current locale. [tm_wday] |
o %A |
The full weekday name according to the current locale. |
In the default "C" locale, one of `<<Sunday>>', `<<Monday>>', `<<Tuesday>>', |
`<<Wednesday>>', `<<Thursday>>', `<<Friday>>', `<<Saturday>>'. [tm_wday] |
o %b |
The abbreviated month name according to the current locale. [tm_mon] |
o %B |
The full month name according to the current locale. |
In the default "C" locale, one of `<<January>>', `<<February>>', |
`<<March>>', `<<April>>', `<<May>>', `<<June>>', `<<July>>', |
`<<August>>', `<<September>>', `<<October>>', `<<November>>', |
`<<December>>'. [tm_mon] |
o %c |
The preferred date and time representation for the current locale. |
[tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday] |
o %C |
The century, that is, the year divided by 100 then truncated. For |
4-digit years, the result is zero-padded and exactly two characters; |
but for other years, there may a negative sign or more digits. In |
this way, `<<%C%y>>' is equivalent to `<<%Y>>'. [tm_year] |
o %d |
The day of the month, formatted with two digits (from `<<01>>' to |
`<<31>>'). [tm_mday] |
o %D |
A string representing the date, in the form `<<"%m/%d/%y">>'. |
[tm_mday, tm_mon, tm_year] |
o %e |
The day of the month, formatted with leading space if single digit |
(from `<<1>>' to `<<31>>'). [tm_mday] |
o %E<<x>> |
In some locales, the E modifier selects alternative representations of |
certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>. |
o %F |
A string representing the ISO 8601:2000 date format, in the form |
`<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year] |
o %g |
The last two digits of the week-based year, see specifier %G (from |
`<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday] |
o %G |
The week-based year. In the ISO 8601:2000 calendar, week 1 of the year |
includes January 4th, and begin on Mondays. Therefore, if January 1st, |
2nd, or 3rd falls on a Sunday, that day and earlier belong to the last |
week of the previous year; and if December 29th, 30th, or 31st falls |
on Monday, that day and later belong to week 1 of the next year. For |
consistency with %Y, it always has at least four characters. |
Example: "%G" for Saturday 2nd January 1999 gives "1998", and for |
Tuesday 30th December 1997 gives "1998". [tm_year, tm_wday, tm_yday] |
o %h |
Synonym for "%b". [tm_mon] |
o %H |
The hour (on a 24-hour clock), formatted with two digits (from |
`<<00>>' to `<<23>>'). [tm_hour] |
o %I |
The hour (on a 12-hour clock), formatted with two digits (from |
`<<01>>' to `<<12>>'). [tm_hour] |
o %j |
The count of days in the year, formatted with three digits |
(from `<<001>>' to `<<366>>'). [tm_yday] |
o %k |
The hour (on a 24-hour clock), formatted with leading space if single |
digit (from `<<0>>' to `<<23>>'). Non-POSIX extension (c.p. %I). [tm_hour] |
o %l |
The hour (on a 12-hour clock), formatted with leading space if single |
digit (from `<<1>>' to `<<12>>'). Non-POSIX extension (c.p. %H). [tm_hour] |
o %m |
The month number, formatted with two digits (from `<<01>>' to `<<12>>'). |
[tm_mon] |
o %M |
The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min] |
o %n |
A newline character (`<<\n>>'). |
o %O<<x>> |
In some locales, the O modifier selects alternative digit characters |
for certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>. |
o %p |
Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for |
the current locale. [tm_hour] |
o %P |
Same as '<<%p>>', but in lowercase. This is a GNU extension. [tm_hour] |
o %r |
Replaced by the time in a.m. and p.m. notation. In the "C" locale this |
is equivalent to "%I:%M:%S %p". In locales which don't define a.m./p.m. |
notations, the result is an empty string. [tm_sec, tm_min, tm_hour] |
o %R |
The 24-hour time, to the minute. Equivalent to "%H:%M". [tm_min, tm_hour] |
o %S |
The second, formatted with two digits (from `<<00>>' to `<<60>>'). The |
value 60 accounts for the occasional leap second. [tm_sec] |
o %t |
A tab character (`<<\t>>'). |
o %T |
The 24-hour time, to the second. Equivalent to "%H:%M:%S". [tm_sec, |
tm_min, tm_hour] |
o %u |
The weekday as a number, 1-based from Monday (from `<<1>>' to |
`<<7>>'). [tm_wday] |
o %U |
The week number, where weeks start on Sunday, week 1 contains the first |
Sunday in a year, and earlier days are in week 0. Formatted with two |
digits (from `<<00>>' to `<<53>>'). See also <<%W>>. [tm_wday, tm_yday] |
o %V |
The week number, where weeks start on Monday, week 1 contains January 4th, |
and earlier days are in the previous year. Formatted with two digits |
(from `<<01>>' to `<<53>>'). See also <<%G>>. [tm_year, tm_wday, tm_yday] |
o %w |
The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>'). |
[tm_wday] |
o %W |
The week number, where weeks start on Monday, week 1 contains the first |
Monday in a year, and earlier days are in week 0. Formatted with two |
digits (from `<<00>>' to `<<53>>'). [tm_wday, tm_yday] |
o %x |
Replaced by the preferred date representation in the current locale. |
In the "C" locale this is equivalent to "%m/%d/%y". |
[tm_mon, tm_mday, tm_year] |
o %X |
Replaced by the preferred time representation in the current locale. |
In the "C" locale this is equivalent to "%H:%M:%S". [tm_sec, tm_min, tm_hour] |
o %y |
The last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year] |
(Implementation interpretation: always positive, even for negative years.) |
o %Y |
The full year, equivalent to <<%C%y>>. It will always have at least four |
characters, but may have more. The year is accurate even when tm_year |
added to the offset of 1900 overflows an int. [tm_year] |
o %z |
The offset from UTC. The format consists of a sign (negative is west of |
Greewich), two characters for hour, then two characters for minutes |
(-hhmm or +hhmm). If tm_isdst is negative, the offset is unknown and no |
output is generated; if it is zero, the offset is the standard offset for |
the current time zone; and if it is positive, the offset is the daylight |
savings offset for the current timezone. The offset is determined from |
the TZ environment variable, as if by calling tzset(). [tm_isdst] |
o %Z |
The time zone name. If tm_isdst is negative, no output is generated. |
Otherwise, the time zone name is based on the TZ environment variable, |
as if by calling tzset(). [tm_isdst] |
o %% |
A single character, `<<%>>'. |
o- |
RETURNS |
When the formatted time takes up no more than <[maxsize]> characters, |
the result is the length of the formatted string. Otherwise, if the |
formatting operation was abandoned due to lack of room, the result is |
<<0>>, and the string starting at <[s]> corresponds to just those |
parts of <<*<[format]>>> that could be completely filled in within the |
<[maxsize]> limit. |
PORTABILITY |
ANSI C requires <<strftime>>, but does not specify the contents of |
<<*<[s]>>> when the formatted string would require more than |
<[maxsize]> characters. Unrecognized specifiers and fields of |
<<timp>> that are out of range cause undefined results. Since some |
formats expand to 0 bytes, it is wise to set <<*<[s]>>> to a nonzero |
value beforehand to distinguish between failure and an empty string. |
This implementation does not support <<s>> being NULL, nor overlapping |
<<s>> and <<format>>. |
<<strftime>> requires no supporting OS subroutines. |
BUGS |
<<strftime>> ignores the LC_TIME category of the current locale, hard-coding |
the "C" locale settings. |
*/ |
#include <newlib.h> |
#include <sys/config.h> |
#include <stddef.h> |
#include <stdio.h> |
#include <time.h> |
#include <string.h> |
#include <stdlib.h> |
#include <limits.h> |
#include <ctype.h> |
#include <wctype.h> |
/* Defines to make the file dual use for either strftime() or wcsftime(). |
* To get wcsftime, define MAKE_WCSFTIME. |
* To get strftime, do not define MAKE_WCSFTIME. |
* Names are kept friendly to strftime() usage. The biggest ugliness is the |
* use of the CQ() macro to make either regular character constants and |
* string literals or wide-character constants and wide-character-string |
* literals, as appropriate. */ |
#if !defined(MAKE_WCSFTIME) |
# define CHAR char /* string type basis */ |
# define CQ(a) a /* character constant qualifier */ |
# define SFLG /* %s flag (null for normal char) */ |
# define _ctloc(x) (ctloclen = strlen (ctloc = _CurrentTimeLocale->x), ctloc) |
# define TOLOWER(c) tolower((int)(unsigned char)(c)) |
# define STRTOUL(c,p,b) strtoul((c),(p),(b)) |
# define STRCPY(a,b) strcpy((a),(b)) |
# define STRCHR(a,b) strchr((a),(b)) |
# define STRLEN(a) strlen(a) |
# else |
# define strftime wcsftime /* Alternate function name */ |
# define CHAR wchar_t /* string type basis */ |
# define CQ(a) L##a /* character constant qualifier */ |
# define snprintf swprintf /* wide-char equivalent function name */ |
# define strncmp wcsncmp /* wide-char equivalent function name */ |
# define TOLOWER(c) towlower((wint_t)(c)) |
# define STRTOUL(c,p,b) wcstoul((c),(p),(b)) |
# define STRCPY(a,b) wcscpy((a),(b)) |
# define STRCHR(a,b) wcschr((a),(b)) |
# define STRLEN(a) wcslen(a) |
# define SFLG "l" /* %s flag (l for wide char) */ |
# ifdef __HAVE_LOCALE_INFO_EXTENDED__ |
# define _ctloc(x) (ctloclen = wcslen (ctloc = _CurrentTimeLocale->w##x), \ |
ctloc) |
# else |
# define CTLOCBUFLEN 256 /* Arbitrary big buffer size */ |
const wchar_t * |
__ctloc (wchar_t *buf, const char *elem, size_t *len_ret) |
{ |
buf[CTLOCBUFLEN - 1] = L'\0'; |
*len_ret = mbstowcs (buf, elem, CTLOCBUFLEN - 1); |
if (*len_ret == (size_t) -1 ) |
*len_ret = 0; |
return buf; |
} |
# define _ctloc(x) (ctloc = __ctloc (ctlocbuf, _CurrentTimeLocale->x, \ |
&ctloclen)) |
# endif |
#endif /* MAKE_WCSFTIME */ |
size_t _DEFUN (strftime, (s, maxsize, format, tim_p), |
CHAR *s _AND |
size_t maxsize _AND |
_CONST CHAR *format _AND |
_CONST struct tm *tim_p) |
{ |
return 0; |
} |
/contrib/sdk/sources/newlib/time/time.c |
---|
0,0 → 1,61 |
/* |
FUNCTION |
<<time>>---get current calendar time (as single number) |
INDEX |
time |
ANSI_SYNOPSIS |
#include <time.h> |
time_t time(time_t *<[t]>); |
TRAD_SYNOPSIS |
#include <time.h> |
time_t time(<[t]>) |
time_t *<[t]>; |
DESCRIPTION |
<<time>> looks up the best available representation of the current |
time and returns it, encoded as a <<time_t>>. It stores the same |
value at <[t]> unless the argument is <<NULL>>. |
RETURNS |
A <<-1>> result means the current time is not available; otherwise the |
result represents the current time. |
PORTABILITY |
ANSI C requires <<time>>. |
Supporting OS subroutine required: Some implementations require |
<<gettimeofday>>. |
*/ |
/* Most times we have a system call in newlib/libc/sys/.. to do this job */ |
#include <_ansi.h> |
#include <reent.h> |
#include <sys/types.h> |
#include <sys/time.h> |
time_t |
_DEFUN (time, (t), |
time_t * t) |
{ |
struct timeval now; |
if (_gettimeofday_r (_REENT, &now, NULL) >= 0) |
{ |
if (t) |
*t = now.tv_sec; |
return now.tv_sec; |
} |
return -1; |
} |
int |
_DEFUN (gettimeofday, (ptimeval, ptimezone), |
struct timeval *ptimeval _AND |
void *ptimezone) |
{ |
return _gettimeofday_r (_REENT, ptimeval, ptimezone); |
} |
/contrib/sdk/sources/newlib/time/tzlock.c |
---|
0,0 → 1,56 |
/* |
FUNCTION |
<<__tz_lock>>, <<__tz_unlock>>---lock time zone global variables |
INDEX |
__tz_lock |
INDEX |
__tz_unlock |
ANSI_SYNOPSIS |
#include "local.h" |
void __tz_lock (void); |
void __tz_unlock (void); |
TRAD_SYNOPSIS |
void __tz_lock(); |
void __tz_unlock(); |
DESCRIPTION |
The <<tzset>> facility functions call these functions when they need to |
ensure the values of global variables. The version of these routines |
supplied in the library use the lock API defined in sys/lock.h. If multiple |
threads of execution can call the time functions and give up scheduling in |
the middle, then you you need to define your own versions of these functions |
in order to safely lock the time zone variables during a call. If you do |
not, the results of <<localtime>>, <<mktime>>, <<ctime>>, and <<strftime>> |
are undefined. |
The lock <<__tz_lock>> may not be called recursively; that is, |
a call <<__tz_lock>> will always lock all subsequent <<__tz_lock>> calls |
until the corresponding <<__tz_unlock>> call on the same thread is made. |
*/ |
#include <_ansi.h> |
#include "local.h" |
#include <sys/lock.h> |
#ifndef __SINGLE_THREAD__ |
__LOCK_INIT(static, __tz_lock_object); |
#endif |
_VOID |
_DEFUN_VOID (__tz_lock) |
{ |
#ifndef __SINGLE_THREAD__ |
__lock_acquire(__tz_lock_object); |
#endif |
} |
_VOID |
_DEFUN_VOID (__tz_unlock) |
{ |
#ifndef __SINGLE_THREAD__ |
__lock_release(__tz_lock_object); |
#endif |
} |
/contrib/sdk/sources/newlib/time/tzvars.c |
---|
0,0 → 1,10 |
#include <time.h> |
/* Global timezone variables. */ |
/* Default timezone to GMT */ |
char *_tzname[2] = {"GMT", "GMT"}; |
int _daylight = 0; |
long _timezone = 0; |