summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorBrian Gianforcaro <b.gianfo@gmail.com>2021-04-19 22:20:13 -0700
committerLinus Groh <mail@linusgroh.de>2021-04-21 08:04:52 +0200
commit440b81deba0ea9365608635687195082ca5abf80 (patch)
tree3eec020dd9d98bdf1a99e180f3a9c91bf9958eea /Userland
parent8124719c3d2744edfc475155f70a286cb45e72ef (diff)
downloadserenity-440b81deba0ea9365608635687195082ca5abf80.zip
LibC: Add ctime_r() and asctime_r() implementations
Need this for a port of FIO (Flexible IO Tester) https://fio.readthedocs.io/
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibC/time.cpp15
-rw-r--r--Userland/Libraries/LibC/time.h2
2 files changed, 16 insertions, 1 deletions
diff --git a/Userland/Libraries/LibC/time.cpp b/Userland/Libraries/LibC/time.cpp
index 9331bbde13..966521d994 100644
--- a/Userland/Libraries/LibC/time.cpp
+++ b/Userland/Libraries/LibC/time.cpp
@@ -84,6 +84,12 @@ char* ctime(const time_t* t)
return asctime(localtime(t));
}
+char* ctime_r(const time_t* t, char* buf)
+{
+ struct tm tm_buf;
+ return asctime_r(localtime_r(t, &tm_buf), buf);
+}
+
static const int __seconds_per_day = 60 * 60 * 24;
static void time_to_tm(struct tm* tm, time_t t)
@@ -180,7 +186,14 @@ struct tm* gmtime_r(const time_t* t, struct tm* tm)
char* asctime(const struct tm* tm)
{
static char buffer[69];
- strftime(buffer, sizeof buffer, "%a %b %e %T %Y", tm);
+ return asctime_r(tm, buffer);
+}
+
+char* asctime_r(const struct tm* tm, char* buffer)
+{
+ // Spec states buffer must be at least 26 bytes.
+ constexpr size_t assumed_len = 26;
+ strftime(buffer, assumed_len, "%a %b %e %T %Y", tm);
return buffer;
}
diff --git a/Userland/Libraries/LibC/time.h b/Userland/Libraries/LibC/time.h
index 74cf7f8de6..558f28be3b 100644
--- a/Userland/Libraries/LibC/time.h
+++ b/Userland/Libraries/LibC/time.h
@@ -57,8 +57,10 @@ time_t mktime(struct tm*);
time_t timegm(struct tm*);
time_t time(time_t*);
char* ctime(const time_t*);
+char* ctime_r(const time_t* tm, char* buf);
void tzset();
char* asctime(const struct tm*);
+char* asctime_r(const struct tm*, char* buf);
#define CLOCKS_PER_SEC 1000
clock_t clock();