From b30b567228278f1fd7e73681869c31db60387c80 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 22 Sep 2019 18:14:10 +0200 Subject: [PATCH] fsdev: Convert time into proper POSIX UTC timestamps (FS reports local time instead) --- nx/source/runtime/devices/fs_dev.c | 36 +++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/nx/source/runtime/devices/fs_dev.c b/nx/source/runtime/devices/fs_dev.c index e02a8c0f..84d642df 100644 --- a/nx/source/runtime/devices/fs_dev.c +++ b/nx/source/runtime/devices/fs_dev.c @@ -7,10 +7,12 @@ #include #include #include +#include #include "runtime/devices/fs_dev.h" #include "runtime/util/utf.h" #include "services/fs.h" +#include "services/time.h" /*! @internal @@ -256,6 +258,28 @@ static ssize_t fsdev_convertfromfspath(uint8_t *out, uint8_t *in, size_t len) return strnlen((char*)out, len); } +static time_t fsdev_converttimetoutc(u64 timestamp) +{ + // Parse timestamp into y/m/d h:m:s + time_t posixtime = (time_t)timestamp; + struct tm *t = gmtime(&posixtime); + + // Convert time/date into an actual UTC POSIX timestamp using the system's timezone rules + TimeCalendarTime caltime; + caltime.year = 1900 + t->tm_year; + caltime.month = 1 + t->tm_mon; + caltime.day = t->tm_mday; + caltime.hour = t->tm_hour; + caltime.minute = t->tm_min; + caltime.second = t->tm_sec; + u64 new_timestamp; + Result rc = timeToPosixTimeWithMyRule(&caltime, &new_timestamp, 1, NULL); + if (R_SUCCEEDED(rc)) + posixtime = (time_t)new_timestamp; + + return posixtime; +} + extern int __system_argc; extern char** __system_argv; @@ -965,9 +989,9 @@ fsdev_fstat(struct _reent *r, if(file->timestamps.is_valid) { - st->st_ctime = file->timestamps.created; - st->st_mtime = file->timestamps.modified; - st->st_atime = file->timestamps.accessed; + st->st_ctime = fsdev_converttimetoutc(file->timestamps.created); + st->st_mtime = fsdev_converttimetoutc(file->timestamps.modified); + st->st_atime = fsdev_converttimetoutc(file->timestamps.accessed); } return 0; @@ -1030,9 +1054,9 @@ fsdev_stat(struct _reent *r, rc = fsFsGetFileTimeStampRaw(&device->fs, fs_path, ×tamps); if(R_SUCCEEDED(rc) && timestamps.is_valid) { - st->st_ctime = timestamps.created; - st->st_mtime = timestamps.modified; - st->st_atime = timestamps.accessed; + st->st_ctime = fsdev_converttimetoutc(timestamps.created); + st->st_mtime = fsdev_converttimetoutc(timestamps.modified); + st->st_atime = fsdev_converttimetoutc(timestamps.accessed); } }