📄 ansitime.c
字号:
const time_t timer, /* time represented as seconds from epoch */ struct tm * tmp /* pointer to broken-down time structure */ ) { long days; long timeOfDay; long year; long mon; ldiv_t result; /* Calulate number of days since epoch */ days = timer / SECSPERDAY; timeOfDay = timer % SECSPERDAY; /* If time of day is negative, subtract one day, and add SECSPERDAY * to make it positive. */ if(timeOfDay<0) { timeOfDay+=SECSPERDAY; days-=1; } /* Calulate number of years since epoch */ year = days / DAYSPERYEAR; while ( __daysSinceEpoch (year, 0) > days ) year--; /* Calulate the day of the week */ tmp->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; /* * If there is a negative weekday, add DAYSPERWEEK to make it positive */ if(tmp->tm_wday<0) tmp->tm_wday+=DAYSPERWEEK; /* Find year and remaining days */ days -= __daysSinceEpoch (year, 0); year += EPOCH_YEAR; /* Find month */ /* __jullday needs years since TM_YEAR_BASE (SPR 4251) */ for ( mon = 0; (days >= __julday (year - TM_YEAR_BASE, mon + 1, 0)) && (mon < 11); mon++ ) ; /* Initialise tm structure */ tmp->tm_year = year - TM_YEAR_BASE; /* years since 1900 */ tmp->tm_mon = mon; tmp->tm_mday = (days - __julday (tmp->tm_year, mon, 0)) + 1; tmp->tm_yday = __julday (tmp->tm_year, mon, tmp->tm_mday) - 1; tmp->tm_hour = timeOfDay / SECSPERHOUR; timeOfDay %= SECSPERHOUR; ldiv_r (timeOfDay, SECSPERMIN, &result); tmp->tm_min = result.quot; tmp->tm_sec = result.rem; return(OK); }/************************************************************************** daysSinceEpoch - calculate number days since ANSI C epoch * * The (year + 1)/4 term accounts for leap years, the * first of which was 1972 & should be added starting '73 * * RETURNS:* NOMANUAL*/int __daysSinceEpoch ( int year, /* Years since epoch */ int yday /* days since Jan 1 */ ) { if(year>=0) /* 1970 + */ return ( (365 * year) + (year + 1) / 4 + yday ); else /* 1969 - */ return ( (365 * year) + (year - 2) / 4 + yday ); } /************************************************************************** julday - calculate Julian Day given year, month, day * Inputs : year (years since 1900), month (0 - 11), * day (1 - 31) * Comment : Returns Julian day in range 1:366. * Unix wants 0:365 * RETURNS: Julian day * NOMANUAL*/int __julday ( int yr, /* year */ int mon, /* month */ int day /* day */ ) { static jdays[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int leap = 0; if (isleap (yr + TM_YEAR_BASE)) { /* * If it is a leap year, leap only gets set if the day is * after beginning of March (SPR #4251). */ if (mon > 1) leap = 1; } return (jdays [mon] + day + leap ); }/* localtime.c - localtime routine for the ANSI time library *//* Copyright 1992-1996 Wind River Systems, Inc. *//*modification history--------------------01d,20jun96,dbt corrected localtime_r (SPR #2521). timeBuffer must be initialized before a call of __getDstInfo. Updated copyright.01c,05feb93,jdi documentation cleanup for 5.1.01b,20sep92,smb documentation additions01a,25jul92,smb written.*//* DESCRIPTION INCLUDE FILE: time.h, stdlib.h SEE ALSO: American National Standard X3.159-1989 NOMANUAL*/#include "vxWorks.h"#include "time.h"#include "stdlib.h"#include "private/timeP.h"extern TIMELOCALE *__loctime; /* time locale information *//****************************************************************************** localtime - convert calendar time into broken-down time (ANSI)** This routine converts the calendar time pointed to by <timer> into* broken-down time, expressed as local time.** This routine is not reentrant. For a reentrant version, see localtime_r().** INCLUDE FILES: time.h** RETURNS: * A pointer to a `tm' structure containing the local broken-down time.*/struct tm *localtime ( const time_t * timer /* calendar time in seconds */ ) { static struct tm timeBuffer; localtime_r (timer, &timeBuffer); return (&timeBuffer); }/****************************************************************************** localtime_r - convert calendar time into broken-down time (POSIX)** This routine converts the calendar time pointed to by <timer> into* broken-down time, expressed as local time. The broken-down time is* stored in <timeBuffer>.** This routine is the POSIX re-entrant version of localtime().** INCLUDE FILES: time.h** RETURNS: OK.*/int localtime_r ( const time_t * timer, /* calendar time in seconds */ struct tm * timeBuffer /* buffer for the broken-down time */ ) { char zoneBuf [sizeof (ZONEBUFFER)]; int dstOffset; /* First get the zone info */ __getZoneInfo(zoneBuf, TIMEOFFSET, __loctime); /* Generate a broken-down time structure */ __getTime (*timer - ((atoi (zoneBuf)) * SECSPERMIN), timeBuffer); /* is Daylight Saving Time in effect ? */ dstOffset = __getDstInfo (timeBuffer,__loctime); timeBuffer->tm_isdst = dstOffset; /* Correct the broken-down time structure if necessary */ if (dstOffset) __getTime ((*timer - ((atoi (zoneBuf)) * SECSPERMIN)) + (dstOffset * SECSPERHOUR), timeBuffer); return (OK); /* __getTime always returns OK */ }/* mktime.c - mktime file for time *//* Copyright 1992-1996 Wind River Systems, Inc. *//*modification history--------------------01f,30jul96,dbt In function mktime, if tm_isdst flag is true substract one hour from timeIs (SPR #6954). Updated copyright.01e,25jul96,dbt fixed warnings in mktime.c.01d,24sep93,jmm _tmValidate() calls _julday() with the tm_mday parameter01c,05feb93,jdi documentation cleanup for 5.1.01b,20sep92,smb documentation additions01a,25jul92,smb written.*//* DESCRIPTION INCLUDE FILE: time.h, stdlib.h SEE ALSO: American National Standard X3.159-1989 NOMANUAL*/#include "vxWorks.h"#include "time.h"#include "stdlib.h"#include "private/timeP.h"extern TIMELOCALE *__loctime; /* locale time structure *//* LOCAL */LOCAL void __tmNormalize (int *,int *,int);LOCAL void __tmValidate (struct tm *); /********************************************************************************* mktime - convert broken-down time into calendar time (ANSI)** This routine converts the broken-down time, expressed as local time, in* the structure pointed to by <timeptr> into a calendar time value with the* same encoding as that of the values returned by the time() function. The* original values of the `tm_wday' and `tm_yday' components of the `tm'* structure are ignored, and the original values of the other components are* not restricted to the ranges indicated in time.h. On successful completion,* the values of `tm_wday' and `tm_yday' are set appropriately, and the other* components are set to represent the specified calendar time, but with* their values forced to the ranges indicated in time.h; the final value of* `tm_mday' is not set until `tm_mon' and `tm_year' are determined.** INCLUDE FILES: time.h** RETURNS:* The calendar time in seconds, or ERROR (-1)* if calendar time cannot be calculated.*/time_t mktime ( struct tm * timeptr /* pointer to broken-down structure */ ) { time_t timeIs = 0; int days = 0; char zoneBuf [sizeof (ZONEBUFFER)]; /* Validate tm structure */ __tmValidate (timeptr); /* Calulate time_t value */ /* time */ timeIs += (timeptr->tm_sec + (timeptr->tm_min * SECSPERMIN) + (timeptr->tm_hour * SECSPERHOUR)); /* date */ days += __julday (timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday); timeptr->tm_yday = (days - 1); if ((timeptr->tm_year + TM_YEAR_BASE) < EPOCH_YEAR ) return ((time_t) ERROR); /* days in previous years */ days = __daysSinceEpoch (timeptr->tm_year - (EPOCH_YEAR - TM_YEAR_BASE), timeptr->tm_yday ); timeptr->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; timeIs += (days * SECSPERDAY); /* correct for day light saving */ /* validate again for the extra DST hour */ if ((timeptr->tm_isdst = __getDstInfo (timeptr, __loctime))) { timeIs -= SECSPERHOUR; __tmValidate (timeptr); } /* correct for zone offset from UTC */ __getZoneInfo (zoneBuf, TIMEOFFSET, __loctime); timeIs += (atoi (zoneBuf) * SECSPERMIN); return(timeIs); } /********************************************************************************* __tmValidate - validate the broken-down structure, tmptr.** RETURNS: the validated structure.* NOMANUAL*/LOCAL void __tmValidate ( struct tm * tmptr /* pointer to broken-down structure */ ) { struct tm tmStruct; int jday; int mon; /* Adjust timeptr to reflect a legal time * Is it within range 1970-2038? */ tmStruct = *tmptr; __tmNormalize (&tmStruct.tm_min, &tmStruct.tm_sec, SECSPERMIN); __tmNormalize (&tmStruct.tm_hour, &tmStruct.tm_min, MINSPERHOUR); __tmNormalize (&tmStruct.tm_mday, &tmStruct.tm_hour, HOURSPERDAY); __tmNormalize (&tmStruct.tm_year, &tmStruct.tm_mon, MONSPERYEAR); /* tm_mday may not be in the correct range - check */ jday = __julday (tmStruct.tm_year, tmStruct.tm_mon , tmStruct.tm_mday); if (jday < 0) { tmStruct.tm_year--; jday += DAYSPERYEAR; } /* Calulate month and day */ for (mon = 0; (jday > __julday (tmStruct.tm_year, mon+1, 0)) && (mon < 11); mon++ ) ; tmStruct.tm_mon = mon; tmStruct.tm_mday = jday - __julday (tmStruct.tm_year, mon, 0); tmStruct.tm_wday = 0; tmStruct.tm_yday = 0; *tmptr = tmStruct; }/********************************************************************************* __tmNormalize - This function is used to reduce units to a range [0,base]* tens is used to store the number of times units is divisable* by base.** total = (tens * base) + units** RETURNS: no value* NOMANUAL*/LOCAL void __tmNormalize ( int * tens, /* tens */ int * units, /* units */ int base /* base */ ) { *tens += *units / base; *units %= base; if ((*units % base ) < 0) { (*tens)--; *units += base; } }/* strftime.c - strftime file for time *//* Copyright 1991-1996 Wind River Systems, Inc. *//*modification history--------------------01i,20jan97,dbt modified comment concerning seconds (SPR #4436).01h,04oct96,dbt reworked the fix for SPR #7277.01g,03oct96,dbt use memcpy with 'strlen + 1' instead of 'strlen' (SPR #7277)01f,27jun96,dbt corrected __getDstInfo() (spr 2521) Updated copyright.01g,10feb95,rhp internal doc tweak from ansiTime.c01f,15sep94,rhp fixed TIMEZONE example in comment (related to SPR #3490)01e,17aug93,dvs changed TIME to TIMEO to fix conflicting defines (SPR #2249)01d,05feb93,jdi documentation cleanup for 5.1.01c,20sep92,smb documentation additions01b,26jul92,rrr fixed decl of __weekOfYear to compile on mips.01a,25jul92,smb written.*//* DESCRIPTION INCLUDE FILE: time.h, stdlib.h, string.h, locale.h SEE ALSO: American National Standard X3.159-1989 NOMANUAL*/#include "vxWorks.h"#include "string.h"#include "stdlib.h"#include "locale.h"#include "private/timeP.h"extern TIMELOCALE * __loctime; /* time locale information *//* LOCAL */LOCAL size_t strftime_r (char *, size_t, const char *, const struct tm *, TIMELOCALE *);LOCAL void __generateTime (char *, const struct tm *, TIMELOCALE *, int *, const char *);LOCAL void __getDay (char *, int, int, TIMELOCALE *, int *);LOCAL void __getMonth (char *, int, int, TIMELOCALE *, int *);LOCAL void __getLocale (char *, int, const struct tm *, TIMELOCALE *, int *);LOCAL void __intToStr (char *, int, int);LOCAL int __weekOfYear (int, int, int);/********************************************************************************* strftime - convert broken-down time into a formatted string (ANSI)** This routine formats the broken-down time in <tptr> based on the conversion* specified in the string <format>, and places the result in the string <s>.** The format is a multibyte character sequence, beginning and ending in its* initial state. The <format> string consists of zero or more conversion* specifiers and ordinary multibyte characters. A conversion specifier* consists of a % character followed by a character that determines the* behavior of the conversion. All ordinary multibyte characters (including* the terminating NULL character) are copied unchanged to the array. If* copying takes place between objects that overlap, the behavior is* undefined. No more than <n> characters are placed into the array.** Each conversion specifier is replaced by appropriate characters as * described in the following list. The appropriate characters are determined * by the LC_TIME category of the current locale and by the values contained * in the structure pointed to by <tptr>.** .iP %a* the locale's abbreviated weekday name.* .iP %A* the locale's full weekday name.* .iP %b* the locale's abbreviated month name.* .iP %B* the locale's full month name.* .iP %c* the locale's appropriate date and time representation.* .iP %d* the day of the month as decimal number (01-31).* .iP %H* the hour (24-hour clock) as a decimal number (00-23).* .iP %I* the hour (12-hour clock) as a decimal number (01-12).* .iP %j* the day of the year as decimal number (001-366).* .iP %m* the month as a decimal number (01-12).* .iP %M* the minute as a decimal number (00-59).* .iP %P* the locale's equivalent of the AM/PM * designations associated with a 12-hour clock.* .iP %S* the second as a decimal number (00-59).* .iP %U* the week number of the year (first Sunday* as the first day of week 1) as a decimal number (00-53).* .iP %w* the weekday as a decimal number (0-6), where Sunday is 0.* .iP %W* the week number of the year (the first Monday* as the first day of week 1) as a decimal number (00-53).* .iP %x* the locale's appropriate date representation.* .iP %X* the locale's appropriate time representation.* .iP %y* the year without century as a decimal number (00-99).* .iP %Y* the year with century as a decimal number.* .iP %Z* the time zone name or abbreviation, or by no* characters if no time zone is determinable.* .iP %%* %.* .LP** For any other conversion specifier, the behavior is undefined.** INCLUDE FILES: time.h** RETURNS:* The number of characters in <s>, not including the terminating null* character -- or zero if the number of characters in <s>, including the null* character, is more than <n> (in which case the contents of <s> are* indeterminate).*/size_t strftime ( char * s, /* string array */ size_t n, /* maximum size of array */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -