source: libcfa/src/time.cfa @ ff2a33e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ff2a33e was ff2a33e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

switch from calling abort to using abort stream

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