source: tests/time.cfa @ 960665c

Last change on this file since 960665c was f9b42bd, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

modify time test to deal with unfreed storage from putenv/tzset

  • Property mode set to 100644
File size: 2.7 KB
Line 
1//
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.
6//
7// time.cfa --
8//
9// Author           : Peter A. Buhr
10// Created On       : Tue Mar 27 17:24:56 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Aug 13 09:09:47 2024
13// Update Count     : 76
14//
15
16#include "time.hfa"
17#include <fstream.hfa>
18#include <stdlib.h>                                                                             // putenv
19
20extern "C" size_t malloc_unfreed() { return 2048; }             // guess at unfreed storage from putenv/tzset
21
22int main() {
23        // Set fixed time location to obtain repeatable output where ever run.
24        putenv( "TZ=America/Toronto" );                                         // pick fixed time zone
25        // tzset has the stupidest interface I have ever seen.
26        tzset();                                                                                        // set time zone
27
28        Duration d1 = 3`h, d2 = 2`s, d3 = 3.375`s, d4 = 12`s, d5 = 1`s + 10_000`ns;
29        sout | d1 | d2 | d3 | d4 | d5;
30        sout | d1`dd | d2`dm | d3`ds | d4`dms | d5`dus;
31        d1 = 0;
32        sout | d1 | d2 | d3;
33        d1 = 7`s;
34        d3 = d2 = d1;
35        sout | d1 | d2 | d3;
36        d1 = d1 + d2;
37        sout | d1;
38        sout | d1 == 7`s | d1 == d2 | d1 == 0;
39        sout | div( 7`s, 2`s );
40        sout | nl;
41
42        Time t = { 1970, 1, 2, 0, 0, 0, 10_000_000 };
43        sout | t;
44        t = t + d1;
45        sout | t | t`ns;
46        Time t1 = (timespec){ 104_414, 10_000_000 };
47        sout | t1 | t1`ns;
48        sout | t - t  | t + d5 | t`ns;
49        char buf[64];
50        sout | "yy/mm/dd" | [t, buf]`ymd | nonl;                        // shared buf => separate calls
51        sout | "mm/dd/yy" | mm_dd_yy( t, buf ) | nonl;
52        strftime( buf, 16, "%D", t );                                           // %D => mm/dd/yy
53        sout | "mm/dd/yy" | buf | nonl;
54        sout | "dd/yy/mm" | [t, buf]`dmy;
55        Time t2 = { 2001, 7, 4, 0, 0, 1, 0 }, t3 = (timeval){ 994_219_201 };
56        sout | t2 | t2`ns | nl | t3 | t3`ns;
57        sout | nl;
58
59        // Clock Newfoundland = { -3.5`h }, PST = { -8`h };     // distance from GMT (UTC)
60        // sout | "Clock Resolution" | getRes()
61        //       | "Newfoundland" | getTime( Newfoundland )
62        //       | "local" | getTime()
63        //       | "local nsec" | timeHiRes()
64        //       | "PST" | PST();                                                               // getTime short form
65        // sout | nl;
66
67        // http://en.cppreference.com/w/cpp/chrono/duration/operator_arith4
68        Duration s = 1`h + 2 * 10`m + 70`s / 10;
69        sout | "1 hour + 2*10 min + 70/10 sec = " | s | "seconds";
70        sout | "Dividing that by 2 minutes gives" | s / 2`m;
71        sout | "Dividing that by 2 gives" | s / 2 | "seconds";
72        sout | s | "seconds is" | s`h | "hours," | (s % 1`h)`m | "minutes," | (s % 1`m)`s | "seconds";
73
74    t1 = (Time){ 2020, 1, 5, 9, 0, 0, 100000000000LL };
75    t2 = (Time){ 1969, 13, 5, 9 };
76    t3 = (Time){ 1970, 25, 366, 48, 120, -120, 60000000000LL };
77    strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t1 );
78    sout | buf;
79    strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t2 );
80    sout | buf;
81    strftime( buf, 128, "%Y %b %e %H:%M:%S (GMT)", t3 );
82    sout | buf;
83} // main
Note: See TracBrowser for help on using the repository browser.