source: libcfa/src/time.cfa @ ce44c5f

ADTast-experimental
Last change on this file since ce44c5f was 0aa4beb, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Visibility of some of the stdlib

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