| [58b6d1b] | 1 | //
 | 
|---|
| [2a84d06d] | 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.
 | 
|---|
| [58b6d1b] | 6 | //
 | 
|---|
 | 7 | // time.c --
 | 
|---|
 | 8 | //
 | 
|---|
| [2a84d06d] | 9 | // Author           : Peter A. Buhr
 | 
|---|
 | 10 | // Created On       : Tue Mar 27 13:33:14 2018
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [b12e4ad] | 12 | // Last Modified On : Fri Aug  2 07:41:24 2024
 | 
|---|
 | 13 | // Update Count     : 72
 | 
|---|
| [58b6d1b] | 14 | //
 | 
|---|
| [2a84d06d] | 15 | 
 | 
|---|
| [58b6d1b] | 16 | #include "time.hfa"
 | 
|---|
| [ff2a33e] | 17 | #include "fstream.hfa"
 | 
|---|
| [2a84d06d] | 18 | #include <stdio.h>                                                                              // snprintf
 | 
|---|
| [d7312ac] | 19 | #include <assert.h>
 | 
|---|
| [2a84d06d] | 20 | 
 | 
|---|
| [0aa4beb] | 21 | #pragma GCC visibility push(default)
 | 
|---|
 | 22 | 
 | 
|---|
| [2a84d06d] | 23 | static char * nanomsd( long int ns, char * buf ) {              // most significant digits
 | 
|---|
 | 24 |         snprintf( buf, 16, ".%09ld", ns );
 | 
|---|
 | 25 |         int i;
 | 
|---|
 | 26 |         for ( i = 9; buf[i] == '0' ; i -= 1 );                          // find least significant digit
 | 
|---|
 | 27 |         buf[i + 1] = '\0';
 | 
|---|
 | 28 |         return buf;
 | 
|---|
 | 29 | } // nanomsd
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | //######################### Duration #########################
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | 
 | 
|---|
| [fd54fef] | 35 | forall( ostype & | ostream( ostype ) ) {
 | 
|---|
| [200fcb3] | 36 |         ostype & ?|?( ostype & os, Duration dur ) with( dur ) {
 | 
|---|
| [e0c235c] | 37 |                 (ostype &)(os | tn / TIMEGRAN);                                 // print seconds
 | 
|---|
 | 38 |                 long int ns = (tn < 0 ? -tn : tn) % TIMEGRAN;   // compute nanoseconds
 | 
|---|
| [200fcb3] | 39 |                 if ( ns != 0 ) {                                                                // some ?
 | 
|---|
 | 40 |                         char buf[16];
 | 
|---|
| [65240bb] | 41 |                         (ostype &)(os | nanomsd( ns, buf ));            // print nanoseconds
 | 
|---|
| [200fcb3] | 42 |                 } // if
 | 
|---|
 | 43 |                 return os;
 | 
|---|
 | 44 |         } // ?|?
 | 
|---|
| [b12e4ad] | 45 |         OSTYPE_VOID_IMPL( os, Duration )
 | 
|---|
| [200fcb3] | 46 | } // distribution
 | 
|---|
| [2a84d06d] | 47 | 
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | //######################### Time #########################
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | #ifdef __CFA_DEBUG__
 | 
|---|
| [e0c235c] | 53 | static void tabort( int year, int month, int day, int hour, int min, int sec, int64_t nsec ) {
 | 
|---|
| [ff2a33e] | 54 |         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
 | 
|---|
| [e0c235c] | 55 |                   | "(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.";
 | 
|---|
| [ff2a33e] | 56 | } // tabort
 | 
|---|
| [2a84d06d] | 57 | #endif // __CFA_DEBUG__
 | 
|---|
 | 58 | 
 | 
|---|
| [e0c235c] | 59 | void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int64_t nsec ) with( time ) {
 | 
|---|
| [2a84d06d] | 60 |         tm tm;
 | 
|---|
 | 61 | 
 | 
|---|
| [e0c235c] | 62 |         // Values can be in any range (+/-) but result must be in the epoch.
 | 
|---|
| [2a84d06d] | 63 |         tm.tm_year = year - 1900;                                                       // mktime uses 1900 as its starting point
 | 
|---|
| [e0c235c] | 64 |         // Make month in range 1-12 to match with day.
 | 
|---|
| [2a84d06d] | 65 |         tm.tm_mon = month - 1;                                                          // mktime uses range 0-11
 | 
|---|
 | 66 |         tm.tm_mday = day;                                                                       // mktime uses range 1-31
 | 
|---|
 | 67 |         tm.tm_hour = hour;
 | 
|---|
 | 68 |         tm.tm_min = min;
 | 
|---|
 | 69 |         tm.tm_sec = sec;
 | 
|---|
| [e0c235c] | 70 |         tm.tm_isdst = -1;                                                                       // let mktime determine if alternate timezone is in effect
 | 
|---|
| [2a84d06d] | 71 |         time_t epochsec = mktime( &tm );
 | 
|---|
 | 72 | #ifdef __CFA_DEBUG__
 | 
|---|
| [e0c235c] | 73 |         if ( epochsec <= (time_t)-1 ) {                                         // MUST BE LESS THAN OR EQUAL!
 | 
|---|
| [ff2a33e] | 74 |                 tabort( year, month, day, hour, min, sec, nsec );
 | 
|---|
| [2a84d06d] | 75 |         } // if
 | 
|---|
 | 76 | #endif // __CFA_DEBUG__
 | 
|---|
| [e0c235c] | 77 |         tn = (int64_t)(epochsec) * TIMEGRAN + nsec;                     // convert to nanoseconds
 | 
|---|
| [2a84d06d] | 78 | #ifdef __CFA_DEBUG__
 | 
|---|
| [e0c235c] | 79 |         if ( tn > 2147483647LL * TIMEGRAN ) {                           // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
 | 
|---|
| [ff2a33e] | 80 |                 tabort( year, month, day, hour, min, sec, nsec );
 | 
|---|
| [2a84d06d] | 81 |         } // if
 | 
|---|
 | 82 | #endif // __CFA_DEBUG__
 | 
|---|
| [8eb2018] | 83 | } // ?{}
 | 
|---|
| [2a84d06d] | 84 | 
 | 
|---|
 | 85 | char * yy_mm_dd( Time time, char * buf ) with( time ) {
 | 
|---|
| [e0c235c] | 86 |         time_t s = tn / TIMEGRAN;
 | 
|---|
| [2a84d06d] | 87 |         tm tm;
 | 
|---|
| [d7312ac] | 88 |         gmtime_r( &s, &tm );                                                            // tm_mon <= 11, tm_mday <= 31
 | 
|---|
| [b6d7f44] | 89 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 90 | #pragma GCC diagnostic push
 | 
|---|
 | 91 | #pragma GCC diagnostic ignored "-Wformat-truncation"
 | 
|---|
| [b6d7f44] | 92 | #endif
 | 
|---|
| [2a84d06d] | 93 |         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon + 1, tm.tm_mday );
 | 
|---|
| [b6d7f44] | 94 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 95 | #pragma GCC diagnostic pop
 | 
|---|
| [b6d7f44] | 96 | #endif
 | 
|---|
| [2a84d06d] | 97 |         return buf;
 | 
|---|
 | 98 | } // yy_mm_dd
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | char * mm_dd_yy( Time time, char * buf ) with( time ) {
 | 
|---|
| [e0c235c] | 101 |         time_t s = tn / TIMEGRAN;
 | 
|---|
| [2a84d06d] | 102 |         tm tm;
 | 
|---|
| [d7312ac] | 103 |         gmtime_r( &s, &tm );                                                            // tm_mon <= 11, tm_mday <= 31
 | 
|---|
| [b6d7f44] | 104 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 105 | #pragma GCC diagnostic push
 | 
|---|
 | 106 | #pragma GCC diagnostic ignored "-Wformat-truncation"
 | 
|---|
| [b6d7f44] | 107 | #endif
 | 
|---|
| [2a84d06d] | 108 |         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_year % 99 );
 | 
|---|
| [b6d7f44] | 109 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 110 | #pragma GCC diagnostic pop
 | 
|---|
| [b6d7f44] | 111 | #endif
 | 
|---|
| [2a84d06d] | 112 |         return buf;
 | 
|---|
 | 113 | } // mm_dd_yy
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | char * dd_mm_yy( Time time, char * buf ) with( time ) {
 | 
|---|
| [e0c235c] | 116 |         time_t s = tn / TIMEGRAN;
 | 
|---|
| [2a84d06d] | 117 |         tm tm;
 | 
|---|
| [d7312ac] | 118 |         gmtime_r( &s, &tm );                                                            // tm_mon <= 11, tm_mday <= 31
 | 
|---|
| [b6d7f44] | 119 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 120 | #pragma GCC diagnostic push
 | 
|---|
 | 121 | #pragma GCC diagnostic ignored "-Wformat-truncation"
 | 
|---|
| [b6d7f44] | 122 | #endif
 | 
|---|
| [2a84d06d] | 123 |         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year % 99 );
 | 
|---|
| [b6d7f44] | 124 | #if defined(__GNUC__) && __GNUC__ >= 7
 | 
|---|
| [d7312ac] | 125 | #pragma GCC diagnostic pop
 | 
|---|
| [b6d7f44] | 126 | #endif
 | 
|---|
| [2a84d06d] | 127 |         return buf;
 | 
|---|
 | 128 | } // dd_mm_yy
 | 
|---|
 | 129 | 
 | 
|---|
| [e3fea42] | 130 | size_t strftime( char buf[], size_t size, const char fmt[], Time time ) with( time ) {
 | 
|---|
| [e0c235c] | 131 |         time_t s = tn / TIMEGRAN;
 | 
|---|
| [2a84d06d] | 132 |         tm tm;
 | 
|---|
 | 133 |         gmtime_r( &s, &tm );
 | 
|---|
 | 134 |         return strftime( buf, size, fmt, &tm );
 | 
|---|
 | 135 | } // strftime
 | 
|---|
 | 136 | 
 | 
|---|
| [fd54fef] | 137 | forall( ostype & | ostream( ostype ) ) {
 | 
|---|
| [200fcb3] | 138 |         ostype & ?|?( ostype & os, Time time ) with( time ) {
 | 
|---|
 | 139 |                 char buf[32];                                                                   // at least 26
 | 
|---|
| [e0c235c] | 140 |                 time_t s = tn / TIMEGRAN;
 | 
|---|
| [200fcb3] | 141 |                 ctime_r( &s, (char *)&buf );                                    // 26 characters: "Wed Jun 30 21:49:08 1993\n"
 | 
|---|
 | 142 |                 buf[24] = '\0';                                                                 // remove trailing '\n'
 | 
|---|
| [e0c235c] | 143 |                 long int ns = (tn < 0 ? -tn : tn) % TIMEGRAN;   // compute nanoseconds
 | 
|---|
| [200fcb3] | 144 |                 if ( ns == 0 ) {                                                                // none ?
 | 
|---|
| [65240bb] | 145 |                         (ostype &)(os | buf);                                           // print date/time/year
 | 
|---|
| [200fcb3] | 146 |                 } else {
 | 
|---|
 | 147 |                         buf[19] = '\0';                                                         // truncate to "Wed Jun 30 21:49:08"
 | 
|---|
 | 148 |                         char buf2[16];
 | 
|---|
 | 149 |                         nanomsd( ns, buf2 );                                            // compute nanoseconds
 | 
|---|
| [65240bb] | 150 |                         (ostype &)(os | buf | buf2 | ' ' | &buf[20]); // print date/time, nanoseconds and year
 | 
|---|
| [200fcb3] | 151 |                 } // if
 | 
|---|
 | 152 |                 return os;
 | 
|---|
 | 153 |         } // ?|?
 | 
|---|
| [b12e4ad] | 154 |         OSTYPE_VOID_IMPL( os, Time )
 | 
|---|
| [200fcb3] | 155 | } // distribution
 | 
|---|
| [2a84d06d] | 156 | 
 | 
|---|
 | 157 | // Local Variables: //
 | 
|---|
 | 158 | // mode: c //
 | 
|---|
 | 159 | // tab-width: 4 //
 | 
|---|
 | 160 | // End: //
 | 
|---|