| [2a84d06d] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
|  | 7 | // time.c -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Peter A. Buhr | 
|---|
|  | 10 | // Created On       : Tue Mar 27 13:33:14 2018 | 
|---|
|  | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [10a97adb] | 12 | // Last Modified On : Thu Apr 12 14:41:00 2018 | 
|---|
|  | 13 | // Update Count     : 22 | 
|---|
| [2a84d06d] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "time" | 
|---|
| [10a97adb] | 17 | #include "iostream" | 
|---|
| [2a84d06d] | 18 | #include <stdio.h>                                                                              // snprintf | 
|---|
|  | 19 |  | 
|---|
|  | 20 | static char * nanomsd( long int ns, char * buf ) {              // most significant digits | 
|---|
|  | 21 | snprintf( buf, 16, ".%09ld", ns ); | 
|---|
|  | 22 | int i; | 
|---|
|  | 23 | for ( i = 9; buf[i] == '0' ; i -= 1 );                          // find least significant digit | 
|---|
|  | 24 | buf[i + 1] = '\0'; | 
|---|
|  | 25 | return buf; | 
|---|
|  | 26 | } // nanomsd | 
|---|
|  | 27 |  | 
|---|
|  | 28 |  | 
|---|
|  | 29 | //######################### Duration ######################### | 
|---|
|  | 30 |  | 
|---|
|  | 31 |  | 
|---|
|  | 32 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
|  | 33 | ostype & ?|?( ostype & os, Duration dur ) with( dur ) { | 
|---|
|  | 34 | os | tv / TIMEGRAN;                                                                     // print seconds | 
|---|
|  | 35 | long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;           // compute nanoseconds | 
|---|
|  | 36 | if ( ns != 0 ) {                                                                        // some ? | 
|---|
|  | 37 | char buf[16]; | 
|---|
|  | 38 | os | nanomsd( ns, buf );                                                // print nanoseconds | 
|---|
|  | 39 | } // if | 
|---|
|  | 40 | return os; | 
|---|
|  | 41 | } // ?|? | 
|---|
|  | 42 |  | 
|---|
|  | 43 |  | 
|---|
|  | 44 | //######################### Time ######################### | 
|---|
|  | 45 |  | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 48 | #define CreateFmt "Attempt to create Time( year=%d (>=1970), month=%d (1-12), day=%d (1-31), hour=%d (0-23), min=%d (0-59), sec=%d (0-60), nsec=%d (0-999_999_999), " \ | 
|---|
|  | 49 | "which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038." | 
|---|
|  | 50 | #endif // __CFA_DEBUG__ | 
|---|
|  | 51 |  | 
|---|
| [8eb2018] | 52 | void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) { | 
|---|
| [2a84d06d] | 53 | tm tm; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | tm.tm_isdst = -1;                                                                       // let mktime determine if alternate timezone is in effect | 
|---|
|  | 56 | tm.tm_year = year - 1900;                                                       // mktime uses 1900 as its starting point | 
|---|
|  | 57 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 58 | if ( month < 1 || 12 < month ) { | 
|---|
|  | 59 | abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec ); | 
|---|
|  | 60 | } // if | 
|---|
|  | 61 | #endif // __CFA_DEBUG__ | 
|---|
|  | 62 | tm.tm_mon = month - 1;                                                          // mktime uses range 0-11 | 
|---|
|  | 63 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 64 | if ( day < 1 || 31 < day ) { | 
|---|
|  | 65 | abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec ); | 
|---|
|  | 66 | } // if | 
|---|
|  | 67 | #endif // __CFA_DEBUG__ | 
|---|
|  | 68 | tm.tm_mday = day;                                                                       // mktime uses range 1-31 | 
|---|
|  | 69 | tm.tm_hour = hour; | 
|---|
|  | 70 | tm.tm_min = min; | 
|---|
|  | 71 | tm.tm_sec = sec; | 
|---|
|  | 72 | time_t epochsec = mktime( &tm ); | 
|---|
|  | 73 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 74 | if ( epochsec == (time_t)-1 ) { | 
|---|
|  | 75 | abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec ); | 
|---|
|  | 76 | } // if | 
|---|
|  | 77 | #endif // __CFA_DEBUG__ | 
|---|
|  | 78 | tv = (int64_t)(epochsec) * TIMEGRAN + nsec;                     // convert to nanoseconds | 
|---|
|  | 79 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 80 | if ( tv > 2147483647LL * TIMEGRAN ) {                           // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038. | 
|---|
|  | 81 | abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec ); | 
|---|
|  | 82 | } // if | 
|---|
|  | 83 | #endif // __CFA_DEBUG__ | 
|---|
| [8eb2018] | 84 | } // ?{} | 
|---|
| [2a84d06d] | 85 |  | 
|---|
|  | 86 | char * yy_mm_dd( Time time, char * buf ) with( time ) { | 
|---|
|  | 87 | time_t s = tv / TIMEGRAN; | 
|---|
|  | 88 | tm tm; | 
|---|
|  | 89 | gmtime_r( &s, &tm ); | 
|---|
|  | 90 | snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon + 1, tm.tm_mday ); | 
|---|
|  | 91 | return buf; | 
|---|
|  | 92 | } // yy_mm_dd | 
|---|
|  | 93 |  | 
|---|
|  | 94 | char * mm_dd_yy( Time time, char * buf ) with( time ) { | 
|---|
|  | 95 | time_t s = tv / TIMEGRAN; | 
|---|
|  | 96 | tm tm; | 
|---|
|  | 97 | gmtime_r( &s, &tm ); | 
|---|
|  | 98 | snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_year % 99 ); | 
|---|
|  | 99 | return buf; | 
|---|
|  | 100 | } // mm_dd_yy | 
|---|
|  | 101 |  | 
|---|
|  | 102 | char * dd_mm_yy( Time time, char * buf ) with( time ) { | 
|---|
|  | 103 | time_t s = tv / TIMEGRAN; | 
|---|
|  | 104 | tm tm; | 
|---|
|  | 105 | gmtime_r( &s, &tm ); | 
|---|
|  | 106 | snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year % 99 ); | 
|---|
|  | 107 | return buf; | 
|---|
|  | 108 | } // dd_mm_yy | 
|---|
|  | 109 |  | 
|---|
|  | 110 | size_t strftime( char * buf, size_t size, const char * fmt, Time time ) with( time ) { | 
|---|
|  | 111 | time_t s = tv / TIMEGRAN; | 
|---|
|  | 112 | tm tm; | 
|---|
|  | 113 | gmtime_r( &s, &tm ); | 
|---|
|  | 114 | return strftime( buf, size, fmt, &tm ); | 
|---|
|  | 115 | } // strftime | 
|---|
|  | 116 |  | 
|---|
|  | 117 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
|  | 118 | ostype & ?|?( ostype & os, Time time ) with( time ) { | 
|---|
|  | 119 | char buf[32];                                                                           // at least 26 | 
|---|
|  | 120 | time_t s = tv / TIMEGRAN; | 
|---|
|  | 121 | ctime_r( &s, (char *)&buf );                                                // 26 characters: "Wed Jun 30 21:49:08 1993\n" | 
|---|
|  | 122 | buf[24] = '\0';                                                                         // remove trailing '\n' | 
|---|
|  | 123 | long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;           // compute nanoseconds | 
|---|
|  | 124 | if ( ns == 0 ) {                                                                        // none ? | 
|---|
|  | 125 | os | buf;                                                                               // print date/time/year | 
|---|
|  | 126 | } else { | 
|---|
|  | 127 | buf[19] = '\0';                                                                 // truncate to "Wed Jun 30 21:49:08" | 
|---|
|  | 128 | os | buf;                                                                               // print date/time | 
|---|
|  | 129 | char buf2[16]; | 
|---|
|  | 130 | nanomsd( ns, buf2 );                                                    // compute nanoseconds | 
|---|
|  | 131 | os | buf2 | ' ' | &buf[20];                                             // print nanoseconds and year | 
|---|
|  | 132 | } // if | 
|---|
|  | 133 | return os; | 
|---|
|  | 134 | } // ?|? | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // Local Variables: // | 
|---|
|  | 137 | // mode: c // | 
|---|
|  | 138 | // tab-width: 4 // | 
|---|
|  | 139 | // End: // | 
|---|