Support seconds in vfs/rtc plugin and libc backend

Fixes #3886
This commit is contained in:
Christian Helmuth 2022-09-08 14:21:49 +02:00 committed by Norman Feske
parent 4689275845
commit 8186a1d7f8
5 changed files with 17 additions and 22 deletions

View File

@ -89,10 +89,9 @@ set config {
install_config $config install_config $config
set build_components { test/system_rtc test/libc_rtc } set build_components { test/system_rtc test/libc_rtc }
set boot_components { test-system_rtc test-libc_rtc }
build $build_components build $build_components
build_boot_image $boot_components build_boot_image [build_artifacts]
append qemu_args " -nographic " append qemu_args " -nographic "

View File

@ -47,14 +47,14 @@ struct Libc::Rtc : Vfs::Watch_response_handler
try { try {
File_content const content(_alloc, root_dir, _rtc_path.string(), File_content const content(_alloc, root_dir, _rtc_path.string(),
File_content::Limit{4096U}); File_content::Limit{4096U});
content.bytes([&] (char const *ptr, size_t size) { content.bytes([&] (char const *ptr, size_t size) {
char buf[32] { }; char buf[32] { };
::memcpy(buf, ptr, min(sizeof(buf) - 1, size)); ::memcpy(buf, ptr, min(sizeof(buf) - 1, size));
struct tm tm { }; 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); _rtc_value = mktime(&tm);
if (_rtc_value == (time_t)-1) if (_rtc_value == (time_t)-1)
_rtc_value = 0; _rtc_value = 0;

View File

@ -215,7 +215,8 @@ int main(int argc, char **argv)
ts.tv_sec = ts.tv_nsec = 0; ts.tv_sec = ts.tv_nsec = 0;
clock_gettime(CLOCK_REALTIME, &ts); 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; unsigned long long buf = 0;

View File

@ -21,23 +21,18 @@ int main()
unsigned idx = 1; unsigned idx = 1;
while (1) { while (1) {
struct timespec ts; struct timespec ts;
if (clock_gettime(0, &ts)) { if (clock_gettime(0, &ts))
return -1; return -1;
}
struct tm *tm = localtime((time_t*)&ts.tv_sec); struct tm *tm = localtime((time_t*)&ts.tv_sec);
if (!tm) { if (!tm)
return -1; return -1;
}
printf("Timestamp #%d: %d-%d-%d %d:%d %ds\n", char time_str[32];
idx++, if (!strftime(time_str, sizeof(time_str), "%F %T", tm))
1900 + tm->tm_year, return -1;
1 + tm->tm_mon,
tm->tm_mday, printf("Timestamp #%d: %s\n", idx++, time_str);
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
sleep(1); sleep(1);
} }

View File

@ -27,8 +27,8 @@ class Vfs::Rtc_file_system : public Single_file_system
{ {
private: private:
/* "1970-01-01 00:00\n" */ /* "1970-01-01 00:00:00\n" */
enum { TIMESTAMP_LEN = 17 }; enum { TIMESTAMP_LEN = 20 };
class Rtc_vfs_handle : public Single_vfs_handle 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 * Read the current time from the Rtc session
* *
* On each read the current time is queried and afterwards formated * 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, Read_result read(char *dst, file_size count,
file_size &out_count) override file_size &out_count) override
@ -63,8 +63,8 @@ class Vfs::Rtc_file_system : public Single_file_system
char buf[TIMESTAMP_LEN+1]; char buf[TIMESTAMP_LEN+1];
char *b = buf; char *b = buf;
Genode::size_t n = Genode::snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u\n", 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.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
n -= (size_t)seek(); n -= (size_t)seek();
b += seek(); b += seek();