From d1aaa84bcced38ca23f0c1e62f4f5ff38b0ea5be Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Mon, 24 Jan 2005 10:25:46 +0000 Subject: [PATCH] - Moved the common DateStamp to time_t conversion code into a shared function. - The fall-back function for converting time into a string in strftime() now calls itself for the "%x" and "%X" format specifiers. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14809 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/GNUmakefile.68k | 3 +- library/GNUmakefile.os4 | 3 +- library/changes | 6 ++ library/macros.h | 5 +- library/smakefile | 3 +- library/stat_convertfileinfo.c | 10 ++-- library/stat_stat.c | 10 ++-- library/time_gettimeofday.c | 10 +++- library/time_headers.h | 3 +- library/time_mktime.c | 15 +++-- library/time_strftime.c | 101 ++------------------------------- library/time_time.c | 15 ++--- library/utime_utime.c | 10 +++- 13 files changed, 63 insertions(+), 131 deletions(-) diff --git a/library/GNUmakefile.68k b/library/GNUmakefile.68k index 6d36709..a380f66 100644 --- a/library/GNUmakefile.68k +++ b/library/GNUmakefile.68k @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.68k,v 1.20 2005-01-14 09:07:16 obarthel Exp $ +# $Id: GNUmakefile.68k,v 1.21 2005-01-24 10:25:46 obarthel Exp $ # # :ts=8 # @@ -363,6 +363,7 @@ C_LIB = \ time_asctime_r.o \ time_clock.o \ time_converttime.o \ + time_convert_datestamp.o \ time_ctime.o \ time_ctime_r.o \ time_data.o \ diff --git a/library/GNUmakefile.os4 b/library/GNUmakefile.os4 index 5f37ddc..1e6ac9c 100644 --- a/library/GNUmakefile.os4 +++ b/library/GNUmakefile.os4 @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.os4,v 1.20 2005-01-10 00:56:15 theantony Exp $ +# $Id: GNUmakefile.os4,v 1.21 2005-01-24 10:25:46 obarthel Exp $ # # :ts=8 # @@ -365,6 +365,7 @@ C_LIB = \ time_asctime_r.o \ time_clock.o \ time_converttime.o \ + time_convert_datestamp.o \ time_ctime.o \ time_ctime_r.o \ time_data.o \ diff --git a/library/changes b/library/changes index 5878d3b..ec03b84 100644 --- a/library/changes +++ b/library/changes @@ -3,6 +3,12 @@ happen and is a side-effect of the new stdio initialization code which checks if the stdio streams are in non-blocking mode. Fixed. +- Moved the common DateStamp to time_t conversion code into a shared + function. + +- The fall-back function for converting time into a string in strftime() + now calls itself for the "%x" and "%X" format specifiers. + c.lib 1.186 (14.1.2005) diff --git a/library/macros.h b/library/macros.h index 18d7bf9..b04bfb9 100644 --- a/library/macros.h +++ b/library/macros.h @@ -1,5 +1,5 @@ /* - * $Id: macros.h,v 1.5 2005-01-12 09:15:50 obarthel Exp $ + * $Id: macros.h,v 1.6 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -60,6 +60,9 @@ /****************************************************************************/ +/* This is the difference (in seconds) between the Unix epoch (which began + on January 1st, 1970) and the AmigaOS epoch (which began eight years + later on January 1st 1978). */ #define UNIX_TIME_OFFSET 252460800 /****************************************************************************/ diff --git a/library/smakefile b/library/smakefile index 73dd883..ef60738 100644 --- a/library/smakefile +++ b/library/smakefile @@ -1,5 +1,5 @@ # -# $Id: smakefile,v 1.15 2004-12-26 10:28:56 obarthel Exp $ +# $Id: smakefile,v 1.16 2005-01-24 10:25:46 obarthel Exp $ # # :ts=8 # @@ -471,6 +471,7 @@ TIME_OBJ = \ time_asctime_r.o \ time_clock.o \ time_converttime.o \ + time_convert_datestamp.o \ time_ctime.o \ time_ctime_r.o \ time_data.o \ diff --git a/library/stat_convertfileinfo.c b/library/stat_convertfileinfo.c index 79f9518..7d9b233 100644 --- a/library/stat_convertfileinfo.c +++ b/library/stat_convertfileinfo.c @@ -1,5 +1,5 @@ /* - * $Id: stat_convertfileinfo.c,v 1.4 2005-01-02 09:07:08 obarthel Exp $ + * $Id: stat_convertfileinfo.c,v 1.5 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -39,6 +39,10 @@ #include "locale_headers.h" #endif /* _LOCALE_HEADERS_H */ +#ifndef _TIME_HEADERS_H +#include "time_headers.h" +#endif /* _TIME_HEADERS_H */ + /****************************************************************************/ void @@ -109,9 +113,7 @@ __convert_file_info_to_stat( if(FLAG_IS_SET(flags,FIBF_OTR_EXECUTE)) SET_FLAG(mode,S_IXOTH); - mtime = UNIX_TIME_OFFSET + fib->fib_Date.ds_Days * 24*60*60 + fib->fib_Date.ds_Minute * 60 + (fib->fib_Date.ds_Tick / TICKS_PER_SECOND); - if(__default_locale != NULL) - mtime += 60 * __default_locale->loc_GMTOffset; + mtime = __convert_datestamp_to_time(&fib->fib_Date); if(fib->fib_DirEntryType < 0) { diff --git a/library/stat_stat.c b/library/stat_stat.c index 671c991..50b228b 100644 --- a/library/stat_stat.c +++ b/library/stat_stat.c @@ -1,5 +1,5 @@ /* - * $Id: stat_stat.c,v 1.3 2005-01-02 09:07:08 obarthel Exp $ + * $Id: stat_stat.c,v 1.4 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -45,6 +45,10 @@ #include "locale_headers.h" #endif /* _LOCALE_HEADERS_H */ +#ifndef _TIME_HEADERS_H +#include "time_headers.h" +#endif /* _TIME_HEADERS_H */ + /****************************************************************************/ /* The following is not part of the ISO 'C' (1994) standard. */ @@ -108,9 +112,7 @@ stat(const char * path_name, struct stat * st) DateStamp(&ds); PROFILE_ON(); - mtime = UNIX_TIME_OFFSET + ds.ds_Days * 24*60*60 + ds.ds_Minute * 60 + (ds.ds_Tick / TICKS_PER_SECOND); - if(__default_locale != NULL) - mtime += 60 * __default_locale->loc_GMTOffset; + mtime = __convert_datestamp_to_time(&ds); st->st_mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; st->st_mtime = mtime; diff --git a/library/time_gettimeofday.c b/library/time_gettimeofday.c index bb8cdc7..de86a84 100644 --- a/library/time_gettimeofday.c +++ b/library/time_gettimeofday.c @@ -1,5 +1,5 @@ /* - * $Id: time_gettimeofday.c,v 1.4 2005-01-08 10:21:25 obarthel Exp $ + * $Id: time_gettimeofday.c,v 1.5 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -70,11 +70,17 @@ gettimeofday(struct timeval *tp, struct timezone *tzp) ENTER(); + /* Obtain the current system time. */ GetSysTime(&tv); + /* Convert the number of seconds so that they match the Unix epoch, which + starts (January 1st, 1970) eight years before the AmigaOS epoch. */ seconds = tv.tv_sec + UNIX_TIME_OFFSET; microseconds = tv.tv_usec; + /* If possible, adjust for the local time zone. We do this because the + AmigaOS system time is returned in local time and we want to return + it in UTC. */ if(__default_locale != NULL) seconds += 60 * __default_locale->loc_GMTOffset; @@ -94,6 +100,8 @@ gettimeofday(struct timeval *tp, struct timezone *tzp) else tzp->tz_minuteswest = 0; + /* The -1 means "we do not know if the time given is in + daylight savings time". */ tzp->tz_dsttime = -1; SHOWVALUE(tzp->tz_minuteswest); diff --git a/library/time_headers.h b/library/time_headers.h index e24458f..739d35f 100644 --- a/library/time_headers.h +++ b/library/time_headers.h @@ -1,5 +1,5 @@ /* - * $Id: time_headers.h,v 1.5 2005-01-02 09:07:19 obarthel Exp $ + * $Id: time_headers.h,v 1.6 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -94,6 +94,7 @@ extern const char * const NOCOMMON __month_names[12]; extern char * __asctime_r(const struct tm *tm,char * buffer,size_t buffer_size); extern char * __number_to_string(unsigned int number,char * string,size_t max_len,size_t min_len); extern struct tm * __convert_time(ULONG seconds, LONG gmt_offset, struct tm * tm); +extern time_t __convert_datestamp_to_time(const struct DateStamp * ds); /****************************************************************************/ diff --git a/library/time_mktime.c b/library/time_mktime.c index 765551f..15baa8f 100644 --- a/library/time_mktime.c +++ b/library/time_mktime.c @@ -1,5 +1,5 @@ /* - * $Id: time_mktime.c,v 1.2 2005-01-02 09:07:19 obarthel Exp $ + * $Id: time_mktime.c,v 1.3 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -100,14 +100,12 @@ mktime(struct tm *tm) int year; /* We need to have the full year number for the - * leap year calculation below. - */ + leap year calculation below. */ year = tm->tm_year + 1900; /* Now for the famous leap year calculation rules... We - * need to find out if the number of days in the month - * of February are appropriate for the data given. - */ + need to find out if the number of days in the month + of February are appropriate for the data given. */ if((year % 4) != 0) max_month_days = 28; else if ((year % 400) == 0) @@ -177,8 +175,7 @@ mktime(struct tm *tm) seconds = Date2Amiga(&clock_data); /* The AmigaOS "epoch" starts with January 1st, 1978, which was - * a Sunday. - */ + a Sunday. */ tm->tm_wday = (seconds / (24 * 60 * 60)) % 7; clock_data.mday = 1; @@ -188,6 +185,8 @@ mktime(struct tm *tm) tm->tm_yday = (seconds - delta) / (24 * 60 * 60); + /* Finally, adjust for the difference between the Unix and the + AmigaOS epochs, which differ by 8 years. */ result = seconds + UNIX_TIME_OFFSET; error = 0; diff --git a/library/time_strftime.c b/library/time_strftime.c index abef54b..7618a2d 100644 --- a/library/time_strftime.c +++ b/library/time_strftime.c @@ -1,5 +1,5 @@ /* - * $Id: time_strftime.c,v 1.2 2005-01-02 09:07:19 obarthel Exp $ + * $Id: time_strftime.c,v 1.3 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -207,55 +207,7 @@ format_date(const char *format,const struct tm *tm,time_t time_value,struct Hook /* Locale specific date and time ("%a %b %d %H:%M:%S %Y"). */ case 'c': - assert( 0 <= tm->tm_wday && tm->tm_wday <= 6 ); - - if(0 <= tm->tm_wday && tm->tm_wday <= 6) - str = __abbreviated_week_day_names[tm->tm_wday]; - else - str = "-"; - - store_string_via_hook(str,-1,hook); - store_string_via_hook(" ",1,hook); - - assert( 0 <= tm->tm_mon && tm->tm_mon <= 11 ); - - if(0 <= tm->tm_mon && tm->tm_mon <= 11) - str = __abbreviated_month_names[tm->tm_mon]; - else - str = "-"; - - store_string_via_hook(str,-1,hook); - store_string_via_hook(" ",1,hook); - - assert( 1 <= tm->tm_mday && tm->tm_mday <= 31 ); - - __number_to_string((unsigned int)tm->tm_mday,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(" ",1,hook); - - assert( 0 <= tm->tm_hour && tm->tm_hour <= 23 ); - - __number_to_string((unsigned int)tm->tm_hour,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(":",1,hook); - - assert( 0 <= tm->tm_min && tm->tm_min <= 59 ); - - __number_to_string((unsigned int)tm->tm_min,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(":",1,hook); - - assert( 0 <= tm->tm_sec && tm->tm_sec <= 59 ); - - __number_to_string((unsigned int)tm->tm_sec,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(" ",1,hook); - - assert( 0 <= tm->tm_year ); - - __number_to_string((unsigned int)1900 + tm->tm_year,buffer,sizeof(buffer),0); - store_string_via_hook(buffer,-1,hook); - + format_date("%a %b %d %H:%M:%S %Y",tm,time_value,hook); break; /* Day of the month ("01"-"31"). */ @@ -414,58 +366,13 @@ format_date(const char *format,const struct tm *tm,time_t time_value,struct Hook /* Locale-specific date ("%a %b %d, %Y"). */ case 'x': - assert( 0 <= tm->tm_wday && tm->tm_wday <= 6 ); - - if(0 <= tm->tm_wday && tm->tm_wday <= 6) - str = __abbreviated_week_day_names[tm->tm_wday]; - else - str = "-"; - - store_string_via_hook(str,-1,hook); - store_string_via_hook(" ",1,hook); - - assert( 0 <= tm->tm_mon && tm->tm_mon <= 11 ); - - if(0 <= tm->tm_mon && tm->tm_mon <= 11) - str = __abbreviated_month_names[tm->tm_mon]; - else - str = "-"; - - store_string_via_hook(str,-1,hook); - store_string_via_hook(" ",1,hook); - - assert( 1 <= tm->tm_mday && tm->tm_mday <= 31 ); - - __number_to_string((unsigned int)tm->tm_mday,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(", ",2,hook); - - assert( 0 <= tm->tm_year ); - - __number_to_string((unsigned int)1900 + tm->tm_year,buffer,sizeof(buffer),0); - store_string_via_hook(buffer,-1,hook); - + format_date("%a %b %d, %Y",tm,time_value,hook); break; /* Locale-specific time ("%H:%M:%S"). */ case 'X': - assert( 0 <= tm->tm_hour && tm->tm_hour <= 23 ); - - __number_to_string((unsigned int)tm->tm_hour,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(":",1,hook); - - assert( 0 <= tm->tm_min && tm->tm_min <= 59 ); - - __number_to_string((unsigned int)tm->tm_min,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); - store_string_via_hook(":",1,hook); - - assert( 0 <= tm->tm_sec && tm->tm_sec <= 59 ); - - __number_to_string((unsigned int)tm->tm_sec,buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); + format_date("%H:%M:%S",tm,time_value,hook); break; /* Year without century ("00"-"99"). */ diff --git a/library/time_time.c b/library/time_time.c index ef72e3f..10be595 100644 --- a/library/time_time.c +++ b/library/time_time.c @@ -1,5 +1,5 @@ /* - * $Id: time_time.c,v 1.2 2005-01-02 09:07:19 obarthel Exp $ + * $Id: time_time.c,v 1.3 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -45,22 +45,15 @@ time_t time(time_t * tptr) { struct DateStamp ds; - ULONG seconds; time_t result; PROFILE_OFF(); DateStamp(&ds); PROFILE_ON(); - seconds = 60 * ((ULONG)ds.ds_Minute + 24 * 60 * (ULONG)ds.ds_Days) + (ds.ds_Tick / TICKS_PER_SECOND); - - /* time() should try to return the current time in UTC form. Thus, - * we have to take the local time zone into account. - */ - if(__default_locale != NULL) - result = UNIX_TIME_OFFSET + seconds + 60 * __default_locale->loc_GMTOffset; - else - result = UNIX_TIME_OFFSET + seconds; + /* This converts the DateStamp contents into the number of + seconds elapsed since January 1st 1970. */ + result = __convert_datestamp_to_time(&ds); if(tptr != NULL) (*tptr) = result; diff --git a/library/utime_utime.c b/library/utime_utime.c index cbd69a5..fdf1862 100644 --- a/library/utime_utime.c +++ b/library/utime_utime.c @@ -1,5 +1,5 @@ /* - * $Id: utime_utime.c,v 1.3 2005-01-02 09:07:19 obarthel Exp $ + * $Id: utime_utime.c,v 1.4 2005-01-24 10:25:46 obarthel Exp $ * * :ts=4 * @@ -76,20 +76,26 @@ utime(const char * path_name,const struct utimbuf * times) if(__check_abort_enabled) __check_abort(); + /* If a modification time is provided, convert it into the local + DateStamp format, as used by the SetFileDate() function. */ if(times != NULL) { time_t seconds; seconds = times->modtime; + /* The modification time has to lie within the AmigaOS epoch. */ if(seconds < UNIX_TIME_OFFSET) { errno = EINVAL; goto out; } + /* Adjust the modification time to the AmigaOS epoch. */ seconds -= UNIX_TIME_OFFSET; + /* If possible, adjust the modification time to match the + local time zone settings. */ if(__default_locale != NULL) seconds -= 60 * __default_locale->loc_GMTOffset; @@ -99,6 +105,8 @@ utime(const char * path_name,const struct utimbuf * times) } else { + /* No special modification time provided; use the current + time instead. */ DateStamp(&ds); }