From 8186a1d7f866812c6a3d2729d78dd11af211cf6a Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Thu, 8 Sep 2022 14:21:49 +0200 Subject: [PATCH] Support seconds in vfs/rtc plugin and libc backend Fixes #3886 --- repos/libports/run/system_rtc.run | 3 +-- repos/libports/src/lib/libc/internal/rtc.h | 4 ++-- repos/libports/src/test/libc/main.cc | 3 ++- repos/libports/src/test/libc_rtc/main.cc | 19 +++++++------------ repos/os/src/lib/vfs/rtc_file_system.h | 10 +++++----- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/repos/libports/run/system_rtc.run b/repos/libports/run/system_rtc.run index e817b9b566..15e1029987 100644 --- a/repos/libports/run/system_rtc.run +++ b/repos/libports/run/system_rtc.run @@ -89,10 +89,9 @@ set config { install_config $config set build_components { test/system_rtc test/libc_rtc } -set boot_components { test-system_rtc test-libc_rtc } build $build_components -build_boot_image $boot_components +build_boot_image [build_artifacts] append qemu_args " -nographic " diff --git a/repos/libports/src/lib/libc/internal/rtc.h b/repos/libports/src/lib/libc/internal/rtc.h index b38b25d12c..cb10fd1335 100644 --- a/repos/libports/src/lib/libc/internal/rtc.h +++ b/repos/libports/src/lib/libc/internal/rtc.h @@ -47,14 +47,14 @@ struct Libc::Rtc : Vfs::Watch_response_handler try { File_content const content(_alloc, root_dir, _rtc_path.string(), File_content::Limit{4096U}); - content.bytes([&] (char const *ptr, size_t size) { char buf[32] { }; ::memcpy(buf, ptr, min(sizeof(buf) - 1, size)); struct tm tm { }; - if (strptime(buf, "%Y-%m-%d %R", &tm)) { + if (strptime(buf, "%Y-%m-%d %H:%M:%S", &tm) + || strptime(buf, "%Y-%m-%d %H:%M", &tm)) { _rtc_value = mktime(&tm); if (_rtc_value == (time_t)-1) _rtc_value = 0; diff --git a/repos/libports/src/test/libc/main.cc b/repos/libports/src/test/libc/main.cc index 85032dc131..f74357b268 100644 --- a/repos/libports/src/test/libc/main.cc +++ b/repos/libports/src/test/libc/main.cc @@ -215,7 +215,8 @@ int main(int argc, char **argv) ts.tv_sec = ts.tv_nsec = 0; clock_gettime(CLOCK_REALTIME, &ts); - printf("sleep/gettime(CLOCK_REALTIME): %.09f\n", ts.tv_sec + ts.tv_nsec / 1000000000.0); + printf("sleep/gettime(CLOCK_REALTIME): %.09f %s\n", + ts.tv_sec + ts.tv_nsec / 1000000000.0, asctime(localtime(&ts.tv_sec))); { unsigned long long buf = 0; diff --git a/repos/libports/src/test/libc_rtc/main.cc b/repos/libports/src/test/libc_rtc/main.cc index 3b88a9987c..8aeba3a3f8 100644 --- a/repos/libports/src/test/libc_rtc/main.cc +++ b/repos/libports/src/test/libc_rtc/main.cc @@ -21,23 +21,18 @@ int main() unsigned idx = 1; while (1) { struct timespec ts; - if (clock_gettime(0, &ts)) { + if (clock_gettime(0, &ts)) return -1; - } struct tm *tm = localtime((time_t*)&ts.tv_sec); - if (!tm) { + if (!tm) return -1; - } - printf("Timestamp #%d: %d-%d-%d %d:%d %ds\n", - idx++, - 1900 + tm->tm_year, - 1 + tm->tm_mon, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec); + char time_str[32]; + if (!strftime(time_str, sizeof(time_str), "%F %T", tm)) + return -1; + + printf("Timestamp #%d: %s\n", idx++, time_str); sleep(1); } diff --git a/repos/os/src/lib/vfs/rtc_file_system.h b/repos/os/src/lib/vfs/rtc_file_system.h index 3d2449edbc..866e9fb180 100644 --- a/repos/os/src/lib/vfs/rtc_file_system.h +++ b/repos/os/src/lib/vfs/rtc_file_system.h @@ -27,8 +27,8 @@ class Vfs::Rtc_file_system : public Single_file_system { private: - /* "1970-01-01 00:00\n" */ - enum { TIMESTAMP_LEN = 17 }; + /* "1970-01-01 00:00:00\n" */ + enum { TIMESTAMP_LEN = 20 }; class Rtc_vfs_handle : public Single_vfs_handle { @@ -49,7 +49,7 @@ class Vfs::Rtc_file_system : public Single_file_system * Read the current time from the Rtc session * * On each read the current time is queried and afterwards formated - * as '%Y-%m-%d %H:%M\n'. + * as '%Y-%m-%d %H:%M:%S\n' resp. '%F %T\n'. */ Read_result read(char *dst, file_size count, file_size &out_count) override @@ -63,8 +63,8 @@ class Vfs::Rtc_file_system : public Single_file_system char buf[TIMESTAMP_LEN+1]; char *b = buf; - Genode::size_t n = Genode::snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u\n", - ts.year, ts.month, ts.day, ts.hour, ts.minute); + Genode::size_t n = Genode::snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u:%02u\n", + ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); n -= (size_t)seek(); b += seek();