00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: time__ew_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.5 2001/01/23 16:49:43 dietz 00011 * Corrected roundoff problem in datestr23 and datestr23_local 00012 * 00013 * Revision 1.4 2000/11/30 22:12:07 lombard 00014 * fixed bug in timegm_ew: _timezone variable was not being set 00015 * by call to _tzset() before call to mktime(). And the changes 00016 * to the TZ environment variable were unnecessary. 00017 * 00018 * Revision 1.3 2000/09/14 19:26:23 lucky 00019 * Added datestr23_local which returns time string in local time 00020 * 00021 * Revision 1.2 2000/03/10 23:35:58 davidk 00022 * added includes from stdlib.h and stdio.h to resolve some compiler warnings. 00023 * 00024 * Revision 1.1 2000/02/14 18:53:30 lucky 00025 * Initial revision 00026 * 00027 * 00028 */ 00029 00030 /******************************************************** 00031 * time_ew.c Windows NT version * 00032 * * 00033 * This file contains earthworm multi-thread safe * 00034 * versions of time routines. * 00035 ********************************************************/ 00036 #include <stdlib.h> 00037 #include <stdio.h> 00038 #include <string.h> 00039 #include <sys\timeb.h> 00040 #include <sys\types.h> 00041 #include <time_ew.h> 00042 00043 /******************************************************** 00044 * gmtime_ew() converts time in seconds since 1970 to * 00045 * a time/date structure expressed as UTC (GMT) * 00046 ********************************************************/ 00047 struct tm *gmtime_ew( const time_t *epochsec, struct tm *res ) 00048 { 00049 *res = *gmtime( epochsec ); 00050 return( res ); 00051 } 00052 00053 /******************************************************** 00054 * localtime_ew() converts time in seconds since 1970 * 00055 * to a time/date structure expressed as local time * 00056 * (using time zone and daylight savings corrections) * 00057 ********************************************************/ 00058 struct tm *localtime_ew( const time_t *epochsec, struct tm *res ) 00059 { 00060 *res = *localtime( epochsec ); 00061 return( res ); 00062 } 00063 00064 /******************************************************** 00065 * ctime_ew() converts time in seconds since 1970 to * 00066 * a 26 character string expressed as local time * 00067 * (using time zone and daylight savings corrections) * 00068 * Example: "Fri Sep 13 00:00:00 1986\n\0" * 00069 ********************************************************/ 00070 char *ctime_ew( const time_t *epochsec, char *buf, int buflen ) 00071 { 00072 strcpy( buf, ctime( epochsec ) ); 00073 return( buf ); 00074 } 00075 00076 /******************************************************** 00077 * asctime_ew() converts time/date structure to * 00078 * a 26 character string * 00079 * Example: "Fri Sep 13 00:00:00 1986\n\0" * 00080 ********************************************************/ 00081 char *asctime_ew( const struct tm *tm, char *buf, int buflen ) 00082 { 00083 strcpy( buf, asctime( tm ) ); 00084 return( buf ); 00085 } 00086 00087 /******************************************************* 00088 * hrtime_ew() returns a high-resolution system clock * 00089 * time as a double in seconds since * 00090 * midnight Jan 1, 1970 * 00091 *******************************************************/ 00092 double hrtime_ew( double *tnow ) 00093 { 00094 // struct _timeb t; 00095 struct timeb t; 00096 00097 // _ftime( &t ); 00098 ftime( &t ); 00099 *tnow = (double)t.time + (double)t.millitm*0.001; 00100 return( *tnow ); 00101 } 00102 00103 /******************************************************** 00104 * timegm_ew() * 00105 * Convert struct tm to time_t using GMT as time zone * 00106 ********************************************************/ 00107 time_t timegm_ew( struct tm *stm ) 00108 { 00109 time_t tt; 00110 00111 /* Change time zone to GMT; do conversion 00112 ****************************************/ 00113 _timezone = 0L; 00114 tt = mktime( stm ); 00115 00116 /* Restore original _timezone setting 00117 *****************************/ 00118 _tzset(); 00119 00120 return( tt ); 00121 } 00122 00123 00124 /********************************************************** 00125 * Converts time (double, seconds since 1970:01:01) to * 00126 * a 22-character, null-terminated string in the form of * 00127 * yyyy/mm/dd hh:mm:ss.ss * 00128 * Time string returned is in UTC time * 00129 * Target buffer must be 23-chars long to have room for * 00130 * null-character * 00131 **********************************************************/ 00132 char *datestr23( double t, char *pbuf, int len ) 00133 { 00134 time_t tt; /* time as time_t */ 00135 struct tm stm; /* time as struct tm */ 00136 int t_hsec; /* hundredths-seconds part of time */ 00137 00138 /* Make sure target is big enough 00139 ********************************/ 00140 if( len < DATESTR23 ) return( (char *)NULL ); 00141 00142 /* Convert double time to other formats 00143 **************************************/ 00144 t += 0.005; /* prepare to round to the nearest 100th */ 00145 tt = (time_t) t; 00146 t_hsec = (int)( (t - tt) * 100. ); 00147 gmtime_ew( &tt, &stm ); 00148 00149 /* Build character string 00150 ************************/ 00151 sprintf( pbuf, 00152 "%04d/%02d/%02d %02d:%02d:%02d.%02d", 00153 stm.tm_year+1900, 00154 stm.tm_mon+1, 00155 stm.tm_mday, 00156 stm.tm_hour, 00157 stm.tm_min, 00158 stm.tm_sec, 00159 t_hsec ); 00160 00161 return( pbuf ); 00162 } 00163 00164 00165 /********************************************************** 00166 * Converts time (double, seconds since 1970:01:01) to * 00167 * a 22-character, null-terminated string in the form of * 00168 * yyyy/mm/dd hh:mm:ss.ss * 00169 * Time string returned is in LOCAL time * 00170 * Target buffer must be 23-chars long to have room for * 00171 * null-character * 00172 **********************************************************/ 00173 char *datestr23_local( double t, char *pbuf, int len ) 00174 { 00175 time_t tt; /* time as time_t */ 00176 struct tm stm; /* time as struct tm */ 00177 int t_hsec; /* hundredths-seconds part of time */ 00178 00179 /* Make sure target is big enough 00180 ********************************/ 00181 if( len < DATESTR23 ) return( (char *)NULL ); 00182 00183 /* Convert double time to other formats 00184 **************************************/ 00185 t += 0.005; /* prepare to round to the nearest 100th */ 00186 tt = (time_t) t; 00187 t_hsec = (int)( (t - tt) * 100. ); 00188 localtime_ew( &tt, &stm ); 00189 00190 /* Build character string 00191 ************************/ 00192 sprintf( pbuf, 00193 "%04d/%02d/%02d %02d:%02d:%02d.%02d", 00194 stm.tm_year+1900, 00195 stm.tm_mon+1, 00196 stm.tm_mday, 00197 stm.tm_hour, 00198 stm.tm_min, 00199 stm.tm_sec, 00200 t_hsec ); 00201 00202 return( pbuf ); 00203 }