[73abe95] | 1 | // |
---|
[43725bd] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
[73abe95] | 6 | // |
---|
[dc8511c] | 7 | // time.cfa -- |
---|
[73abe95] | 8 | // |
---|
[43725bd] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Tue Mar 27 17:24:56 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[e3260aa1] | 12 | // Last Modified On : Thu Jul 18 22:45:43 2024 |
---|
| 13 | // Update Count : 65 |
---|
[73abe95] | 14 | // |
---|
[43725bd] | 15 | |
---|
[58b6d1b] | 16 | #include "time.hfa" |
---|
[73abe95] | 17 | #include <fstream.hfa> |
---|
[cd1d721] | 18 | #include <stdlib.h> // putenv |
---|
| 19 | |
---|
[e3260aa1] | 20 | extern "C" size_t malloc_unfreed() { return 385; } // unfreed storage from tzset |
---|
[43725bd] | 21 | |
---|
| 22 | int main() { |
---|
[cd1d721] | 23 | // Set fixed time location to obtain repeatable output where ever run. |
---|
[e3260aa1] | 24 | putenv( "TZ=America/Toronto" ); // set fixed time zone |
---|
[cd1d721] | 25 | tzset(); // set time zone |
---|
| 26 | |
---|
[ef2eade] | 27 | Duration d1 = 3`h, d2 = 2`s, d3 = 3.375`s, d4 = 12`s, d5 = 1`s + 10_000`ns; |
---|
[200fcb3] | 28 | sout | d1 | d2 | d3 | d4 | d5; |
---|
[030653a] | 29 | sout | d1`dd | d2`dm | d3`ds | d4`dms | d5`dus; |
---|
[43725bd] | 30 | d1 = 0; |
---|
[200fcb3] | 31 | sout | d1 | d2 | d3; |
---|
[43725bd] | 32 | d1 = 7`s; |
---|
| 33 | d3 = d2 = d1; |
---|
[200fcb3] | 34 | sout | d1 | d2 | d3; |
---|
[43725bd] | 35 | d1 = d1 + d2; |
---|
[200fcb3] | 36 | sout | d1; |
---|
| 37 | sout | d1 == 7`s | d1 == d2 | d1 == 0; |
---|
| 38 | sout | div( 7`s, 2`s ); |
---|
[5ea5b28] | 39 | sout | nl; |
---|
[43725bd] | 40 | |
---|
| 41 | Time t = { 1970, 1, 2, 0, 0, 0, 10_000_000 }; |
---|
[200fcb3] | 42 | sout | t; |
---|
[43725bd] | 43 | t = t + d1; |
---|
[e0c235c] | 44 | sout | t | t`ns; |
---|
[8eb2018] | 45 | Time t1 = (timespec){ 104_414, 10_000_000 }; |
---|
[e0c235c] | 46 | sout | t1 | t1`ns; |
---|
| 47 | sout | t - t | t + d5 | t`ns; |
---|
| 48 | char buf[64]; |
---|
[200fcb3] | 49 | sout | "yy/mm/dd" | [t, buf]`ymd | nonl; // shared buf => separate calls |
---|
| 50 | sout | "mm/dd/yy" | mm_dd_yy( t, buf ) | nonl; |
---|
[43725bd] | 51 | strftime( buf, 16, "%D", t ); // %D => mm/dd/yy |
---|
[200fcb3] | 52 | sout | "mm/dd/yy" | buf | nonl; |
---|
| 53 | sout | "dd/yy/mm" | [t, buf]`dmy; |
---|
[8eb2018] | 54 | Time t2 = { 2001, 7, 4, 0, 0, 1, 0 }, t3 = (timeval){ 994_219_201 }; |
---|
[e0c235c] | 55 | sout | t2 | t2`ns | nl | t3 | t3`ns; |
---|
[5ea5b28] | 56 | sout | nl; |
---|
[43725bd] | 57 | |
---|
| 58 | // Clock Newfoundland = { -3.5`h }, PST = { -8`h }; // distance from GMT (UTC) |
---|
[200fcb3] | 59 | // sout | "Clock Resolution" | getRes() |
---|
| 60 | // | "Newfoundland" | getTime( Newfoundland ) |
---|
| 61 | // | "local" | getTime() |
---|
[4aa495f] | 62 | // | "local nsec" | timeHiRes() |
---|
[200fcb3] | 63 | // | "PST" | PST(); // getTime short form |
---|
[5ea5b28] | 64 | // sout | nl; |
---|
[43725bd] | 65 | |
---|
| 66 | // http://en.cppreference.com/w/cpp/chrono/duration/operator_arith4 |
---|
| 67 | Duration s = 1`h + 2 * 10`m + 70`s / 10; |
---|
[200fcb3] | 68 | sout | "1 hour + 2*10 min + 70/10 sec = " | s | "seconds"; |
---|
| 69 | sout | "Dividing that by 2 minutes gives" | s / 2`m; |
---|
| 70 | sout | "Dividing that by 2 gives" | s / 2 | "seconds"; |
---|
| 71 | sout | s | "seconds is" | s`h | "hours," | (s % 1`h)`m | "minutes," | (s % 1`m)`s | "seconds"; |
---|
[e0c235c] | 72 | |
---|
| 73 | t1 = (Time){ 2020, 1, 5, 9, 0, 0, 100000000000LL }; |
---|
| 74 | t2 = (Time){ 1969, 13, 5, 9 }; |
---|
| 75 | t3 = (Time){ 1970, 25, 366, 48, 120, -120, 60000000000LL }; |
---|
| 76 | strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t1 ); |
---|
| 77 | sout | buf; |
---|
| 78 | strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t2 ); |
---|
| 79 | sout | buf; |
---|
| 80 | strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t3 ); |
---|
| 81 | sout | buf; |
---|
[43725bd] | 82 | } // main |
---|
| 83 | |
---|
| 84 | // Local Variables: // |
---|
| 85 | // mode: c // |
---|
| 86 | // tab-width: 4 // |
---|
[dc8511c] | 87 | // compile-command: "cfa time.cfa" // |
---|
[43725bd] | 88 | // End: // |
---|