source: libcfa/src/time.cfa@ 4f102fa

ADT ast-experimental
Last change on this file since 4f102fa was 0aa4beb, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Visibility of some of the stdlib

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