Skip to content

Commit 5a21c30

Browse files
committed
Added utime() to the os librady
1 parent d2f28eb commit 5a21c30

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

extmod/vfs_fat.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,27 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
415415
}
416416
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
417417

418+
STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t time_in) {
419+
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
420+
const char *path = mp_obj_str_get_str(path_in);
421+
const int time = mp_obj_get_int(time_in);
422+
423+
timeutils_struct_time_t tm;
424+
timeutils_seconds_since_epoch_to_struct_time(time, &tm);
425+
426+
FILINFO fno;
427+
fno.fdate = (WORD)(((tm.tm_year - 1980) * 512U) | tm.tm_mon * 32U | tm.tm_mday);
428+
fno.ftime = (WORD)(tm.tm_hour * 2048U | tm.tm_min * 32U | tm.tm_sec / 2U);
429+
430+
FRESULT res = f_utime(&self->fatfs, path, &fno);
431+
if (res != FR_OK) {
432+
mp_raise_OSError_fresult(res);
433+
}
434+
435+
return mp_const_none;
436+
}
437+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);
438+
418439
#if MICROPY_FATFS_USE_LABEL
419440
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
420441
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
@@ -466,6 +487,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
466487
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
467488
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
468489
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
490+
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&fat_vfs_utime_obj) },
469491
#if MICROPY_FATFS_USE_LABEL
470492
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
471493
#endif

shared-bindings/os/__init__.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
247247
}
248248
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
249249

250+
//| def utime(path: str, time: int) -> None:
251+
//| """Change the timestamp of a file."""
252+
//| ...
253+
//|
254+
STATIC mp_obj_t os_utime(mp_obj_t path_in, mp_obj_t time_in) {
255+
const char *path = mp_obj_str_get_str(path_in);
256+
common_hal_os_utime(path, time_in);
257+
return mp_const_none;
258+
}
259+
MP_DEFINE_CONST_FUN_OBJ_2(os_utime_obj, os_utime);
260+
250261
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
251262
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
252263

@@ -263,6 +274,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
263274
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
264275
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
265276
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&os_remove_obj) }, // unlink aliases to remove
277+
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&os_utime_obj) },
266278

267279
{ MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&os_sync_obj) },
268280

shared-bindings/os/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void common_hal_os_rename(const char *old_path, const char *new_path);
4545
void common_hal_os_rmdir(const char *path);
4646
mp_obj_t common_hal_os_stat(const char *path);
4747
mp_obj_t common_hal_os_statvfs(const char *path);
48+
void common_hal_os_utime(const char *path, mp_obj_t time);
4849

4950
// Returns true if data was correctly sourced from a true random number generator.
5051
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length);

shared-module/os/__init__.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,10 @@ mp_obj_t common_hal_os_statvfs(const char *path) {
226226
}
227227
return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
228228
}
229+
230+
void common_hal_os_utime(const char *path, mp_obj_t time) {
231+
mp_obj_t args[2];
232+
mp_vfs_mount_t *vfs = lookup_path(path, &args[0]);
233+
args[1] = time;
234+
mp_vfs_proxy_call(vfs, MP_QSTR_utime, 2, args);
235+
}

0 commit comments

Comments
 (0)