Changeset e0c235c for libcfa/src/time.cfa
- Timestamp:
- Jan 5, 2020, 9:31:00 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 4834563
- Parents:
- 3c67255
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/time.cfa
r3c67255 re0c235c 10 10 // Created On : Tue Mar 27 13:33:14 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S at Jul 13 08:41:55 201913 // Update Count : 6 512 // Last Modified On : Sun Jan 5 17:27:40 2020 13 // Update Count : 69 14 14 // 15 15 … … 33 33 forall( dtype ostype | ostream( ostype ) ) { 34 34 ostype & ?|?( ostype & os, Duration dur ) with( dur ) { 35 (ostype &)(os | t v/ TIMEGRAN); // print seconds36 long int ns = (t v < 0 ? -tv : tv) % TIMEGRAN; // compute nanoseconds35 (ostype &)(os | tn / TIMEGRAN); // print seconds 36 long int ns = (tn < 0 ? -tn : tn) % TIMEGRAN; // compute nanoseconds 37 37 if ( ns != 0 ) { // some ? 38 38 char buf[16]; … … 52 52 53 53 #ifdef __CFA_DEBUG__ 54 static void tabort( int year, int month, int day, int hour, int min, int sec, int nsec ) {54 static void tabort( int year, int month, int day, int hour, int min, int sec, int64_t nsec ) { 55 55 abort | "Attempt to create Time( year=" | year | "(>=1970), month=" | month | "(1-12), day=" | day | "(1-31), hour=" | hour | "(0-23), min=" | min | "(0-59), sec=" | sec 56 | "(0-60), nsec=" | nsec | "(0-999_999_999), which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038.";56 | "(0-60), nsec=" | nsec | "(0-999_999_999), which is not in the range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038, where month and day have 1 origin."; 57 57 } // tabort 58 58 #endif // __CFA_DEBUG__ 59 59 60 void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) {60 void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int64_t nsec ) with( time ) { 61 61 tm tm; 62 62 63 tm.tm_isdst = -1; // let mktime determine if alternate timezone is in effect63 // Values can be in any range (+/-) but result must be in the epoch. 64 64 tm.tm_year = year - 1900; // mktime uses 1900 as its starting point 65 #ifdef __CFA_DEBUG__ 66 if ( month < 1 || 12 < month ) { 67 tabort( year, month, day, hour, min, sec, nsec ); 68 } // if 69 #endif // __CFA_DEBUG__ 65 // Make month in range 1-12 to match with day. 70 66 tm.tm_mon = month - 1; // mktime uses range 0-11 71 #ifdef __CFA_DEBUG__72 if ( day < 1 || 31 < day ) {73 tabort( year, month, day, hour, min, sec, nsec );74 } // if75 #endif // __CFA_DEBUG__76 67 tm.tm_mday = day; // mktime uses range 1-31 77 68 tm.tm_hour = hour; 78 69 tm.tm_min = min; 79 70 tm.tm_sec = sec; 71 tm.tm_isdst = -1; // let mktime determine if alternate timezone is in effect 80 72 time_t epochsec = mktime( &tm ); 81 73 #ifdef __CFA_DEBUG__ 82 if ( epochsec == (time_t)-1 ) {74 if ( epochsec <= (time_t)-1 ) { // MUST BE LESS THAN OR EQUAL! 83 75 tabort( year, month, day, hour, min, sec, nsec ); 84 76 } // if 85 77 #endif // __CFA_DEBUG__ 86 t v= (int64_t)(epochsec) * TIMEGRAN + nsec; // convert to nanoseconds78 tn = (int64_t)(epochsec) * TIMEGRAN + nsec; // convert to nanoseconds 87 79 #ifdef __CFA_DEBUG__ 88 if ( t v> 2147483647LL * TIMEGRAN ) { // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.80 if ( tn > 2147483647LL * TIMEGRAN ) { // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038. 89 81 tabort( year, month, day, hour, min, sec, nsec ); 90 82 } // if … … 93 85 94 86 char * yy_mm_dd( Time time, char * buf ) with( time ) { 95 time_t s = t v/ TIMEGRAN;87 time_t s = tn / TIMEGRAN; 96 88 tm tm; 97 89 gmtime_r( &s, &tm ); // tm_mon <= 11, tm_mday <= 31 … … 108 100 109 101 char * mm_dd_yy( Time time, char * buf ) with( time ) { 110 time_t s = t v/ TIMEGRAN;102 time_t s = tn / TIMEGRAN; 111 103 tm tm; 112 104 gmtime_r( &s, &tm ); // tm_mon <= 11, tm_mday <= 31 … … 123 115 124 116 char * dd_mm_yy( Time time, char * buf ) with( time ) { 125 time_t s = t v/ TIMEGRAN;117 time_t s = tn / TIMEGRAN; 126 118 tm tm; 127 119 gmtime_r( &s, &tm ); // tm_mon <= 11, tm_mday <= 31 … … 138 130 139 131 size_t strftime( char * buf, size_t size, const char * fmt, Time time ) with( time ) { 140 time_t s = t v/ TIMEGRAN;132 time_t s = tn / TIMEGRAN; 141 133 tm tm; 142 134 gmtime_r( &s, &tm ); … … 147 139 ostype & ?|?( ostype & os, Time time ) with( time ) { 148 140 char buf[32]; // at least 26 149 time_t s = t v/ TIMEGRAN;141 time_t s = tn / TIMEGRAN; 150 142 ctime_r( &s, (char *)&buf ); // 26 characters: "Wed Jun 30 21:49:08 1993\n" 151 143 buf[24] = '\0'; // remove trailing '\n' 152 long int ns = (t v < 0 ? -tv : tv) % TIMEGRAN; // compute nanoseconds144 long int ns = (tn < 0 ? -tn : tn) % TIMEGRAN; // compute nanoseconds 153 145 if ( ns == 0 ) { // none ? 154 146 (ostype &)(os | buf); // print date/time/year
Note: See TracChangeset
for help on using the changeset viewer.