source: libcfa/src/time.cfa @ a254fa56

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

change "const char *" to "const char []"

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