source: libcfa/src/time.cfa @ 341aa39

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 341aa39 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

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