Skip to content

Commit 80429c2

Browse files
authored
Merge pull request #6923 from isacben/add-os-utime-function
Added utime() to the os library
2 parents 1569c7e + de7a9c3 commit 80429c2

File tree

9 files changed

+60
-2
lines changed

9 files changed

+60
-2
lines changed

extmod/vfs_fat.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,35 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
400400
}
401401
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
402402

403+
STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t times_in) {
404+
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
405+
const char *path = mp_obj_str_get_str(path_in);
406+
if (!mp_obj_is_tuple_compatible(times_in)) {
407+
mp_raise_type_arg(&mp_type_TypeError, times_in);
408+
}
409+
410+
mp_obj_t *otimes;
411+
mp_obj_get_array_fixed_n(times_in, 2, &otimes);
412+
413+
// Validate that both elements of the tuple are int and discard the second one
414+
int time[2];
415+
time[0] = mp_obj_get_int(otimes[0]);
416+
time[1] = mp_obj_get_int(otimes[1]);
417+
timeutils_struct_time_t tm;
418+
timeutils_seconds_since_epoch_to_struct_time(time[0], &tm);
419+
420+
FILINFO fno;
421+
fno.fdate = (WORD)(((tm.tm_year - 1980) * 512U) | tm.tm_mon * 32U | tm.tm_mday);
422+
fno.ftime = (WORD)(tm.tm_hour * 2048U | tm.tm_min * 32U | tm.tm_sec / 2U);
423+
FRESULT res = f_utime(&self->fatfs, path, &fno);
424+
if (res != FR_OK) {
425+
mp_raise_OSError_fresult(res);
426+
}
427+
428+
return mp_const_none;
429+
}
430+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);
431+
403432
#if MICROPY_FATFS_USE_LABEL
404433
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
405434
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
@@ -451,6 +480,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
451480
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
452481
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
453482
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
483+
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&fat_vfs_utime_obj) },
454484
#if MICROPY_FATFS_USE_LABEL
455485
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
456486
#endif

ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ CIRCUITPY_BUILD_EXTENSIONS = bin,uf2
1111
INTERNAL_FLASH_FILESYSTEM = 1
1212
LONGINT_IMPL = NONE
1313
CIRCUITPY_FULL_BUILD = 0
14+
15+
CIRCUITPY_RAINBOWIO = 0

ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ CIRCUITPY_BUILD_EXTENSIONS = bin,uf2
1111
INTERNAL_FLASH_FILESYSTEM = 1
1212
LONGINT_IMPL = NONE
1313
CIRCUITPY_FULL_BUILD = 0
14+
1415
CIRCUITPY_RAINBOWIO = 0

ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ CIRCUITPY_FULL_BUILD = 0
1515
# A number of modules are removed for RFM69 to make room for frozen libraries.
1616
# Many I/O functions are not available.
1717
CIRCUITPY_ANALOGIO = 1
18+
CIRCUITPY_BUSDEVICE = 1
19+
CIRCUITPY_RAINBOWIO = 0
1820
CIRCUITPY_ROTARYIO = 0
1921
CIRCUITPY_RTC = 0
22+
CIRCUITPY_TOUCHIO = 0
2023
CIRCUITPY_USB_MIDI = 0
2124
CIRCUITPY_USB_HID = 0
22-
CIRCUITPY_TOUCHIO = 0
23-
CIRCUITPY_BUSDEVICE = 1
2425

2526
# Include these Python libraries in firmware.
2627
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69

ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ CIRCUITPY_BITBANG_APA102 = 0
2020
CIRCUITPY_FREQUENCYIO = 0
2121
CIRCUITPY_I2CTARGET = 0
2222
CIRCUITPY_NEOPIXEL_WRITE = 0
23+
CIRCUITPY_ONEWIREIO = 0
2324
CIRCUITPY_PARALLELDISPLAY = 0
2425
CIRCUITPY_PIXELBUF = 0
2526
CIRCUITPY_PS2IO = 0
2627
CIRCUITPY_PULSEIO = 0
2728
CIRCUITPY_PWMIO = 0
29+
CIRCUITPY_RAINBOWIO = 0
2830
CIRCUITPY_ROTARYIO = 0
2931
CIRCUITPY_RTC = 0
3032
CIRCUITPY_SAMD = 0

ports/atmel-samd/boards/sparkfun_redboard_turbo/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
99
SPI_FLASH_FILESYSTEM = 1
1010
EXTERNAL_FLASH_DEVICES = "W25Q32FV"
1111
LONGINT_IMPL = MPZ
12+
13+
CIRCUITPY_RAINBOWIO = 0

shared-bindings/os/__init__.c

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

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

@@ -264,6 +275,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
264275
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
265276
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
266277
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&os_remove_obj) }, // unlink aliases to remove
278+
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&os_utime_obj) },
267279

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

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 times);
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 times) {
231+
mp_obj_t args[2];
232+
mp_vfs_mount_t *vfs = lookup_path(path, &args[0]);
233+
args[1] = times;
234+
mp_vfs_proxy_call(vfs, MP_QSTR_utime, 2, args);
235+
}

0 commit comments

Comments
 (0)