source: src/libcfa/time.c @ 8eb2018

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 8eb2018 was 8eb2018, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

cleanup, remove conversion of timeval/timespec to duration

  • Property mode set to 100644
File size: 4.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 : Sun Apr  1 17:30:08 2018
13// Update Count     : 21
14//
15
16#include "time"
17#include <stdio.h>                                                                              // snprintf
18
19static char * nanomsd( long int ns, char * buf ) {              // most significant digits
20        snprintf( buf, 16, ".%09ld", ns );
21        int i;
22        for ( i = 9; buf[i] == '0' ; i -= 1 );                          // find least significant digit
23        buf[i + 1] = '\0';
24        return buf;
25} // nanomsd
26
27
28//######################### Duration #########################
29
30
31forall( dtype ostype | ostream( ostype ) )
32ostype & ?|?( ostype & os, Duration dur ) with( dur ) {
33        os | tv / TIMEGRAN;                                                                     // print seconds
34        long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;           // compute nanoseconds
35        if ( ns != 0 ) {                                                                        // some ?
36                char buf[16];
37                os | nanomsd( ns, buf );                                                // print nanoseconds
38        } // if
39        return os;
40} // ?|?
41
42
43//######################### Time #########################
44
45
46#ifdef __CFA_DEBUG__
47#define CreateFmt "Attempt to create Time( year=%d (>=1970), month=%d (1-12), day=%d (1-31), hour=%d (0-23), min=%d (0-59), sec=%d (0-60), nsec=%d (0-999_999_999), " \
48        "which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038."
49#endif // __CFA_DEBUG__
50
51void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) {
52        tm tm;
53
54        tm.tm_isdst = -1;                                                                       // let mktime determine if alternate timezone is in effect
55        tm.tm_year = year - 1900;                                                       // mktime uses 1900 as its starting point
56#ifdef __CFA_DEBUG__
57        if ( month < 1 || 12 < month ) {
58                abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec );
59        } // if
60#endif // __CFA_DEBUG__
61        tm.tm_mon = month - 1;                                                          // mktime uses range 0-11
62#ifdef __CFA_DEBUG__
63        if ( day < 1 || 31 < day ) {
64                abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec );
65        } // if
66#endif // __CFA_DEBUG__
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        time_t epochsec = mktime( &tm );
72#ifdef __CFA_DEBUG__
73        if ( epochsec == (time_t)-1 ) {
74                abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec );
75        } // if
76#endif // __CFA_DEBUG__
77        tv = (int64_t)(epochsec) * TIMEGRAN + nsec;                     // convert to nanoseconds
78#ifdef __CFA_DEBUG__
79        if ( tv > 2147483647LL * TIMEGRAN ) {                           // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
80                abort( CreateFmt, year, month, day, hour, (int)min, sec, nsec );
81        } // if
82#endif // __CFA_DEBUG__
83} // ?{}
84
85char * yy_mm_dd( Time time, char * buf ) with( time ) {
86        time_t s = tv / TIMEGRAN;
87        tm tm;
88        gmtime_r( &s, &tm );
89        snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon + 1, tm.tm_mday );
90        return buf;
91} // yy_mm_dd
92
93char * mm_dd_yy( Time time, char * buf ) with( time ) {
94        time_t s = tv / TIMEGRAN;
95        tm tm;
96        gmtime_r( &s, &tm );
97        snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_year % 99 );
98        return buf;
99} // mm_dd_yy
100
101char * dd_mm_yy( Time time, char * buf ) with( time ) {
102        time_t s = tv / TIMEGRAN;
103        tm tm;
104        gmtime_r( &s, &tm );
105        snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year % 99 );
106        return buf;
107} // dd_mm_yy
108
109size_t strftime( char * buf, size_t size, const char * fmt, Time time ) with( time ) {
110        time_t s = tv / TIMEGRAN;
111        tm tm;
112        gmtime_r( &s, &tm );
113        return strftime( buf, size, fmt, &tm );
114} // strftime
115
116forall( dtype ostype | ostream( ostype ) )
117ostype & ?|?( ostype & os, Time time ) with( time ) {
118        char buf[32];                                                                           // at least 26
119        time_t s = tv / TIMEGRAN;
120    ctime_r( &s, (char *)&buf );                                                // 26 characters: "Wed Jun 30 21:49:08 1993\n"
121        buf[24] = '\0';                                                                         // remove trailing '\n'
122        long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;           // compute nanoseconds
123        if ( ns == 0 ) {                                                                        // none ?
124                os | buf;                                                                               // print date/time/year
125        } else {
126                buf[19] = '\0';                                                                 // truncate to "Wed Jun 30 21:49:08"
127                os | buf;                                                                               // print date/time
128                char buf2[16];
129                nanomsd( ns, buf2 );                                                    // compute nanoseconds
130                os | buf2 | ' ' | &buf[20];                                             // print nanoseconds and year
131        } // if
132        return os;
133} // ?|?
134
135// Local Variables: //
136// mode: c //
137// tab-width: 4 //
138// End: //
Note: See TracBrowser for help on using the repository browser.