Updated utime() to take a 2-tuple instead of a plain int

This commit is contained in:
Isaac Benitez 2022-09-22 00:56:44 -07:00
parent 5a21c30ab2
commit 71d649613f
4 changed files with 15 additions and 10 deletions

View File

@ -415,18 +415,23 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t time_in) {
STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t times_in) {
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
const char *path = mp_obj_str_get_str(path_in);
const int time = mp_obj_get_int(time_in);
if (!mp_obj_is_tuple_compatible(times_in)) {
mp_raise_type_arg(&mp_type_TypeError, times_in);
}
mp_obj_t *times;
mp_obj_get_array_fixed_n(times_in, 2, &times);
const int atime = mp_obj_get_int(times[0]);
const int mtime = mp_obj_get_int(times[1]);
timeutils_struct_time_t tm;
timeutils_seconds_since_epoch_to_struct_time(time, &tm);
timeutils_seconds_since_epoch_to_struct_time(atime, &tm);
FILINFO fno;
fno.fdate = (WORD)(((tm.tm_year - 1980) * 512U) | tm.tm_mon * 32U | tm.tm_mday);
fno.ftime = (WORD)(tm.tm_hour * 2048U | tm.tm_min * 32U | tm.tm_sec / 2U);
FRESULT res = f_utime(&self->fatfs, path, &fno);
if (res != FR_OK) {
mp_raise_OSError_fresult(res);

View File

@ -247,13 +247,13 @@ STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
//| def utime(path: str, time: int) -> None:
//| def utime(path: str, times: Tuple[int, int]) -> None:
//| """Change the timestamp of a file."""
//| ...
//|
STATIC mp_obj_t os_utime(mp_obj_t path_in, mp_obj_t time_in) {
STATIC mp_obj_t os_utime(mp_obj_t path_in, mp_obj_t times_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_utime(path, time_in);
common_hal_os_utime(path, times_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(os_utime_obj, os_utime);

View File

@ -45,7 +45,7 @@ void common_hal_os_rename(const char *old_path, const char *new_path);
void common_hal_os_rmdir(const char *path);
mp_obj_t common_hal_os_stat(const char *path);
mp_obj_t common_hal_os_statvfs(const char *path);
void common_hal_os_utime(const char *path, mp_obj_t time);
void common_hal_os_utime(const char *path, mp_obj_t times);
// Returns true if data was correctly sourced from a true random number generator.
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length);

View File

@ -227,9 +227,9 @@ mp_obj_t common_hal_os_statvfs(const char *path) {
return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
}
void common_hal_os_utime(const char *path, mp_obj_t time) {
void common_hal_os_utime(const char *path, mp_obj_t times) {
mp_obj_t args[2];
mp_vfs_mount_t *vfs = lookup_path(path, &args[0]);
args[1] = time;
args[1] = times;
mp_vfs_proxy_call(vfs, MP_QSTR_utime, 2, args);
}