| 1 | /* melder_time.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011,2014-2018,2020,2021,2025,2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | #include <time.h> |
| 22 | #if defined (macintosh) || defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 23 | #include <sys/time.h> |
| 24 | #include <unistd.h> |
| 25 | #elif defined (_WIN32) |
| 26 | #include <windows.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <chrono> |
| 30 | |
| 31 | double Melder_clockcxx . . $ Melder_clock(31fa7155c10fdd13). () { |
| 32 | using namespace stdcxx . . $ std/::chrono; |
| 33 | auto timePoint = high_resolution_clockcxx . . $ std/chrono/_V2/high_resolution_clock#::nowcxx . . $ std/chrono/_V2/system_clock#now(3657dacbd9b4f607). (); |
| 34 | auto duration = timePoint. time_since_epochcxx . . $ std/chrono/time_point#time_since_epoch(7b7646ca7c721626). (); |
| 35 | return duration. countcxx . . $ std/chrono/duration#count(8f5363d873dd4781). () * double (high_resolution_clockcxx . . $ std/chrono/_V2/high_resolution_clock#::periodcxx . . $ std/chrono/_V2/system_clock#period#::numcxx . . $ std/ratio#num.) / double (high_resolution_clockcxx . . $ std/chrono/_V2/high_resolution_clock#::periodcxx . . $ std/chrono/_V2/system_clock#period#::dencxx . . $ std/ratio#den.); |
| 36 | } |
| 37 | |
| 38 | double Melder_stopwatchcxx . . $ Melder_stopwatch(31fa7155c10fdd13). () { |
| 39 | static thread_local double lastTimeInThisThread = 0.0; |
| 40 | const double now = Melder_clockcxx . . $ Melder_clock(31fa7155c10fdd13). (); |
| 41 | const double timeElapsedInThisThread = ( lastTimeInThisThread == 0.0 ? -1.0 : now - lastTimeInThisThread ); |
| 42 | //Melder_casual ("%lf %lf %lf", now, lastTimeInThisThread, now - lastTimeInThisThread, timeElapsedInThisThread); |
| 43 | lastTimeInThisThread = now; |
| 44 | return timeElapsedInThisThread; |
| 45 | } |
| 46 | |
| 47 | void Melder_sleepcxx . . $ Melder_sleep(ba8836b59fb42830). (double duration) { |
| 48 | if (duration <= 0.0) |
| 49 | return; // already past end time |
| 50 | #if defined (_WIN32) |
| 51 | Sleep (duration * 1e3); |
| 52 | #elif defined (macintosh) || defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 53 | unsigned int seconds = (unsigned int) duration; |
| 54 | unsigned int microseconds = (unsigned int) ((duration - seconds) * 1e6); |
| 55 | if (seconds > 0) |
| 56 | sleep (seconds); |
| 57 | if (microseconds > 0) |
| 58 | usleep (microseconds); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | autostring32cxx . . $ autostring32# date_STRcxx . . $ date_STR(19b925b58261db21). () { |
| 63 | time_t today = time (nullptr); |
| 64 | autostring32cxx . . $ autostring32# date = Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (ctime (& today)); |
| 65 | mutablestring32cxx . . $ mutablestring32# newline = str32chrcxx . . $ str32chr(328a9ede0fba853a). (date.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U'\n'); |
| 66 | if (newline) |
| 67 | *newline = U'\0'; |
| 68 | return datecxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 69 | } |
| 70 | |
| 71 | autostring32cxx . . $ autostring32# date_utc_STRcxx . . $ date_utc_STR(19b925b58261db21). () { |
| 72 | time_t today = time (nullptr); |
| 73 | tmcxx . . $ tm# *today_utc = gmtime (& today); |
| 74 | autostring32cxx . . $ autostring32# date = Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (asctime (today_utc)); |
| 75 | mutablestring32cxx . . $ mutablestring32# newline = str32chrcxx . . $ str32chr(328a9ede0fba853a). (date.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U'\n'); |
| 76 | if (newline) |
| 77 | *newline = U'\0'; |
| 78 | return datecxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 79 | } |
| 80 | |
| 81 | autoVECcxx . . $ autoVEC# date_VECcxx . . $ date_VEC(dc54b7e5180588ba). () { |
| 82 | time_t secondsSince1969 = time (nullptr); |
| 83 | tmcxx . . $ tm# *now_local = localtime (& secondsSince1969); |
| 84 | autoVECcxx . . $ autoVEC# result = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (6); |
| 85 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = now_local -> tm_yearcxx . . $ tm#tm_year. + 1900; |
| 86 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = now_local -> tm_moncxx . . $ tm#tm_mon. + 1; |
| 87 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] = now_local -> tm_mdaycxx . . $ tm#tm_mday.; |
| 88 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).4] = now_local -> tm_hourcxx . . $ tm#tm_hour.; |
| 89 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).5] = now_local -> tm_mincxx . . $ tm#tm_min.; |
| 90 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).6] = now_local -> tm_seccxx . . $ tm#tm_sec.; |
| 91 | return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 92 | } |
| 93 | |
| 94 | autoVECcxx . . $ autoVEC# date_utc_VECcxx . . $ date_utc_VEC(dc54b7e5180588ba). () { |
| 95 | time_t secondsSince1969 = time (nullptr); |
| 96 | tmcxx . . $ tm# *now_utc = gmtime (& secondsSince1969); |
| 97 | autoVECcxx . . $ autoVEC# result = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (6); |
| 98 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = now_utc -> tm_yearcxx . . $ tm#tm_year. + 1900; |
| 99 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = now_utc -> tm_moncxx . . $ tm#tm_mon. + 1; |
| 100 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] = now_utc -> tm_mdaycxx . . $ tm#tm_mday.; |
| 101 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).4] = now_utc -> tm_hourcxx . . $ tm#tm_hour.; |
| 102 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).5] = now_utc -> tm_mincxx . . $ tm#tm_min.; |
| 103 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).6] = now_utc -> tm_seccxx . . $ tm#tm_sec.; |
| 104 | return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 105 | } |
| 106 | |
| 107 | autostring32cxx . . $ autostring32# date_iso_STRcxx . . $ date_iso_STR(19b925b58261db21). () { |
| 108 | time_t secondsSince1969 = time (nullptr); |
| 109 | tmcxx . . $ tm# *now_local = localtime (& secondsSince1969); |
| 110 | char result8 [30]; |
| 111 | strftime (result8, sizeof (result8), "%FT%T%z", now_local); |
| 112 | /* |
| 113 | This is almost correct. The result will look like "2021-07-07T13:36:36+0200" |
| 114 | (at least if time zone information is available), |
| 115 | but what we actually need is "2021-07-07T13:36:36+02:00". |
| 116 | We therefore insert a colon. |
| 117 | */ |
| 118 | const bool isInTheExpectedRawTimeZoneFormat = ( strlen (result8) == 24 && result8 [16] == ':' ); |
| 119 | if (! isInTheExpectedRawTimeZoneFormat) |
| 120 | return Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (result8); // give up (perhaps time zone information is not available) |
| 121 | result8 [25] = '\0'; // new trailing null byte |
| 122 | result8 [24] = result8 [23]; // shift the last digit |
| 123 | result8 [23] = result8 [22]; // shift the penultimate digit |
| 124 | result8 [22] = ':'; // insert the semicolon between the 22 characters and the two digits |
| 125 | return Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (result8); |
| 126 | } |
| 127 | |
| 128 | autostring32cxx . . $ autostring32# date_utc_iso_STRcxx . . $ date_utc_iso_STR(19b925b58261db21). () { |
| 129 | time_t secondsSince1969 = time (nullptr); |
| 130 | tmcxx . . $ tm# *now_utc = gmtime (& secondsSince1969); |
| 131 | int year = now_utc -> tm_yearcxx . . $ tm#tm_year. + 1900; |
| 132 | int month = now_utc -> tm_moncxx . . $ tm#tm_mon. + 1; |
| 133 | int day = now_utc -> tm_mdaycxx . . $ tm#tm_mday.; |
| 134 | int hour = now_utc -> tm_hourcxx . . $ tm#tm_hour.; |
| 135 | int minute = now_utc -> tm_mincxx . . $ tm#tm_min.; |
| 136 | int second = now_utc -> tm_seccxx . . $ tm#tm_sec.; |
| 137 | return Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). ( |
| 138 | year, |
| 139 | U"-", |
| 140 | month <= 9 ? U"0" : U"", month, |
| 141 | U"-", |
| 142 | day <= 9 ? U"0" : U"", day, |
| 143 | U"T", |
| 144 | hour <= 9 ? U"0" : U"", hour, |
| 145 | U":", |
| 146 | minute <= 9 ? U"0" : U"", minute, |
| 147 | U":", |
| 148 | second <= 9 ? U"0" : U"", second, |
| 149 | U"Z" |
| 150 | )); |
| 151 | } |
| 152 | |
| 153 | /* End of file melder_time.cpp */ |