feat(libc): implement ctime

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adrià Franch 2026-10-11 02:58:30 +02:00
parent 06da117642
commit ef2d56eb9c
2 changed files with 18 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include <cstdint>
#include <cstddef>
#include <cerrno>
#include <ctime>
#include <cstring>
#include <limits>
@ -213,6 +214,14 @@ char* APS5_VABI asctime_nid_postfix(const GuestTm* timeptr) {
return std::asctime(&host);
}
char* APS5_VABI ctime_nid_postfix(const int64_t* timer) {
if (const GuestTm* local = localtime_nid_postfix(timer)) return asctime_nid_postfix(local);
static char unknown[26];
std::memcpy(unknown, "??? ??? ?? ??:??:?? ????\n", sizeof(unknown));
errno = EINVAL;
return unknown;
}
size_t APS5_VABI strftime_nid_postfix(char* str, size_t count, const char* format, const GuestTm* timeptr) {
return libc_strftime_nid_postfix(str, count, format, timeptr);
}

View File

@ -21,6 +21,8 @@ GuestTm* APS5_VABI localtime_s_nid_postfix(const std::int64_t*, GuestTm*);
std::int64_t APS5_VABI mktime_nid_postfix(GuestTm*);
std::size_t APS5_VABI strftime_nid_postfix(char*, std::size_t, const char*, const GuestTm*);
void APS5_VABI tzset_nid_postfix(void);
char* APS5_VABI ctime_nid_postfix(const std::int64_t*);
int* APS5_VABI __error_nid_postfix();
}
using Converter = GuestTm* (APS5_VABI *)(const std::int64_t*);
@ -128,6 +130,13 @@ int main() {
Require(roundTrip.tm_wday == 4 && roundTrip.tm_yday == 0, "mktime did not normalize the calendar fields");
Require(roundTrip.tm_gmtoff == 7200 && roundTrip.tm_zone != nullptr, "mktime did not set the zone fields");
Require(std::strcmp(ctime_nid_postfix(&epoch), "Thu Jan 1 02:00:00 1970\n") == 0, "ctime did not format local time");
const std::int64_t leap = 951782400 + 13 * 3600 + 4 * 60 + 5;
Require(std::strcmp(ctime_nid_postfix(&leap), "Tue Feb 29 15:04:05 2000\n") == 0, "ctime formatted the wrong date");
*__error_nid_postfix() = 0;
Require(std::strcmp(ctime_nid_postfix(&underflow), "??? ??? ?? ??:??:?? ????\n") == 0 && *__error_nid_postfix() == 22,
"ctime of an unrepresentable time did not report it");
GuestTm utc{};
Require(gmtime_s_nid_postfix(&epoch, &utc) == &utc, "UTC epoch conversion failed");
char formatted[64]{};