blob: 8d33888768bb7e58e8ad9076c8125a37c0e077be [file] [log] [blame]
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <time.h>
30
Elliott Hughesb891dda2025-08-27 07:19:07 -070031#include <errno.h>
32#include <stdio.h>
33#include <string.h>
34
35char* asctime(const tm* tm) {
36 static char buf[128];
37 return asctime_r(tm, buf);
38}
39
40char* asctime_r(const tm* tm, char* buf) {
41 if (tm == nullptr) {
42 errno = EINVAL;
43 return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
44 }
45
46 auto pick = [](unsigned n, unsigned max, const char* s) {
47 return (n < max) ? s + 3*n : "???";
48 };
49 const char* day = pick(tm->tm_wday, 7, "SunMonTueWedThuFriSat");
50 const char* mon = pick(tm->tm_mon, 12, "JanFebMarAprMayJunJulAugSepOctNovDec");
51
Elliott Hughese0ef29b2025-08-28 07:15:08 -070052 // This format string is specified by ISO C.
Elliott Hughesb891dda2025-08-27 07:19:07 -070053 char tmp_buf[26];
54 int n = snprintf(tmp_buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", day, mon,
55 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, 1900 + tm->tm_year);
56 if (n > 25) {
57 errno = EOVERFLOW;
58 return nullptr;
59 }
60 strcpy(buf, tmp_buf);
61 return buf;
62}
63
64char* ctime(const time_t* tp) {
65 static char buf[128];
66 return ctime_r(tp, buf);
67}
68
69char* ctime_r(const time_t* tp, char* buf) {
70 struct tm tm;
71 if (localtime_r(tp, &tm) == nullptr) return nullptr;
72 return asctime_r(&tm, buf);
73}
74
75double difftime(time_t t1, time_t t0) {
76 return t1 - t0;
77}
78
Elliott Hughesf98d87b2018-07-17 13:21:05 -070079int timespec_get(timespec* ts, int base) {
Elliott Hughes7db0a6c2023-05-03 15:37:46 -070080 return (clock_gettime(base - 1, ts) != -1) ? base : 0;
Elliott Hughes52541ee2023-04-24 17:04:49 -070081}
82
83int timespec_getres(timespec* ts, int base) {
Elliott Hughes7db0a6c2023-05-03 15:37:46 -070084 return (clock_getres(base - 1, ts) != -1) ? base : 0;
Elliott Hughesf98d87b2018-07-17 13:21:05 -070085}