| 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.c -- | 
|---|
| 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 Mar 27 17:27:26 2018 | 
|---|
| 13 | // Update Count     : 2 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "time" | 
|---|
| 17 | #include <fstream> | 
|---|
| 18 |  | 
|---|
| 19 | int main() { | 
|---|
| 20 | Duration d1 = 3`h, d2 = 2`s, d3 = 3.07`s, d4 = (timeval){ 12 }, d5 = (timespec){ 1, 10000 }; | 
|---|
| 21 | sout | d1 | d2 | d3 | d4 | d5 | endl; | 
|---|
| 22 | int i; | 
|---|
| 23 | d1 = 0; | 
|---|
| 24 | sout | d1 | d2 | d3 | endl; | 
|---|
| 25 | d1 = 7`s; | 
|---|
| 26 | d3 = d2 = d1; | 
|---|
| 27 | sout | d1 | d2 | d3 | endl; | 
|---|
| 28 | d1 = d1 + d2; | 
|---|
| 29 | sout | d1 | endl; | 
|---|
| 30 | sout | d1 == 7`s | d1 == d2 | d1 == 0 | endl; | 
|---|
| 31 | sout | endl; | 
|---|
| 32 |  | 
|---|
| 33 | Time t = { 1970, 1, 2, 0, 0, 0, 10_000_000 }; | 
|---|
| 34 | sout | t | endl; | 
|---|
| 35 | t = t + d1; | 
|---|
| 36 | sout | t | t - t | t + d5 | t.tv | endl; | 
|---|
| 37 | char buf[16]; | 
|---|
| 38 | sout | "yy/mm/dd" | [t, buf]`ymd;                                       // shared buf => separate calls | 
|---|
| 39 | sout | "mm/dd/yy" | mm_dd_yy( t, buf ); | 
|---|
| 40 | strftime( buf, 16, "%D", t );                                           // %D => mm/dd/yy | 
|---|
| 41 | sout | "mm/dd/yy" | buf; | 
|---|
| 42 | sout | "dd/yy/mm" | [t, buf]`dmy | endl; | 
|---|
| 43 | Time c = { 2001, 7, 4, 0, 0, 1, 0 }; | 
|---|
| 44 | sout | c | c.tv | endl; | 
|---|
| 45 | sout | endl; | 
|---|
| 46 |  | 
|---|
| 47 | // Clock Newfoundland = { -3.5`h }, PST = { -8`h };     // distance from GMT (UTC) | 
|---|
| 48 | // sout | "Clock Resolution" | getRes() | endl | 
|---|
| 49 | //       | "Newfoundland" | getTime( Newfoundland ) | endl | 
|---|
| 50 | //       | "local" | getTime() | endl | 
|---|
| 51 | //       | "local nsec" | getTimeNsec() | endl | 
|---|
| 52 | //       | "PST" | PST() | endl;                                                // getTime short form | 
|---|
| 53 | // sout | endl; | 
|---|
| 54 |  | 
|---|
| 55 | // http://en.cppreference.com/w/cpp/chrono/duration/operator_arith4 | 
|---|
| 56 | Duration s = 1`h + 2 * 10`m + 70`s / 10; | 
|---|
| 57 | sout | "1 hour + 2*10 min + 70/10 sec = " | s | "seconds" | endl; | 
|---|
| 58 | sout | "Dividing that by 2 minutes gives" | s / 2`m | endl; | 
|---|
| 59 | sout | "Dividing that by 2 gives" | s / 2 | "seconds\n"; | 
|---|
| 60 | sout | s | "seconds is" | s`h | "hours," | (s % 1`h)`m | "minutes," | (s % 1`m)`s | "seconds" | endl; | 
|---|
| 61 | } // main | 
|---|
| 62 |  | 
|---|
| 63 | // Local Variables: // | 
|---|
| 64 | // mode: c // | 
|---|
| 65 | // tab-width: 4 // | 
|---|
| 66 | // End: // | 
|---|