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