source: libcfa/src/time.cfa@ 3f0b062

Last change on this file since 3f0b062 was 5454d77, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

update types to use new void-creation stream macros

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