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