| 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" |
| 20 |
|
| 21 |
#include <time.h> |
| 22 |
#if defined (macintosh) || defined (UNIX) |
| 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_clock () { |
| 32 |
using namespace std::chrono; |
| 33 |
auto timePoint = high_resolution_clock::now (); |
| 34 |
auto duration = timePoint. time_since_epoch (); |
| 35 |
return duration. count () * double (high_resolution_clock::period::num) / double (high_resolution_clock::period::den); |
| 36 |
} |
| 37 |
|
| 38 |
double Melder_stopwatch () { |
| 39 |
static thread_local double lastTimeInThisThread = 0.0; |
| 40 |
const double now = Melder_clock (); |
| 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_sleep (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 (UNIX) |
| 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 |
autostring32 date_STR () { |
| 63 |
time_t today = time (nullptr); |
| 64 |
autostring32 date = Melder_8to32_e (ctime (& today)); |
| 65 |
mutablestring32 newline = str32chr (date.get(), U'\n'); |
| 66 |
if (newline) |
| 67 |
*newline = U'\0'; |
| 68 |
return date; |
| 69 |
} |
| 70 |
|
| 71 |
autostring32 date_utc_STR () { |
| 72 |
time_t today = time (nullptr); |
| 73 |
tm *today_utc = gmtime (& today); |
| 74 |
autostring32 date = Melder_8to32_e (asctime (today_utc)); |
| 75 |
mutablestring32 newline = str32chr (date.get(), U'\n'); |
| 76 |
if (newline) |
| 77 |
*newline = U'\0'; |
| 78 |
return date; |
| 79 |
} |
| 80 |
|
| 81 |
autoVEC date_VEC () { |
| 82 |
time_t secondsSince1969 = time (nullptr); |
| 83 |
tm *now_local = localtime (& secondsSince1969); |
| 84 |
autoVEC result = zero_VEC (6); |
| 85 |
result [1] = now_local -> tm_year + 1900; |
| 86 |
result [2] = now_local -> tm_mon + 1; |
| 87 |
result [3] = now_local -> tm_mday; |
| 88 |
result [4] = now_local -> tm_hour; |
| 89 |
result [5] = now_local -> tm_min; |
| 90 |
result [6] = now_local -> tm_sec; |
| 91 |
return result; |
| 92 |
} |
| 93 |
|
| 94 |
autoVEC date_utc_VEC () { |
| 95 |
time_t secondsSince1969 = time (nullptr); |
| 96 |
tm *now_utc = gmtime (& secondsSince1969); |
| 97 |
autoVEC result = zero_VEC (6); |
| 98 |
result [1] = now_utc -> tm_year + 1900; |
| 99 |
result [2] = now_utc -> tm_mon + 1; |
| 100 |
result [3] = now_utc -> tm_mday; |
| 101 |
result [4] = now_utc -> tm_hour; |
| 102 |
result [5] = now_utc -> tm_min; |
| 103 |
result [6] = now_utc -> tm_sec; |
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
autostring32 date_iso_STR () { |
| 108 |
time_t secondsSince1969 = time (nullptr); |
| 109 |
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_e (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_e (result8); |
| 126 |
} |
| 127 |
|
| 128 |
autostring32 date_utc_iso_STR () { |
| 129 |
time_t secondsSince1969 = time (nullptr); |
| 130 |
tm *now_utc = gmtime (& secondsSince1969); |
| 131 |
int year = now_utc -> tm_year + 1900; |
| 132 |
int month = now_utc -> tm_mon + 1; |
| 133 |
int day = now_utc -> tm_mday; |
| 134 |
int hour = now_utc -> tm_hour; |
| 135 |
int minute = now_utc -> tm_min; |
| 136 |
int second = now_utc -> tm_sec; |
| 137 |
return Melder_dup (Melder_cat ( |
| 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 */ |