Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os file system time not synchronized #9921

Open
TinkerPhu opened this issue Dec 28, 2024 · 1 comment
Open

os file system time not synchronized #9921

TinkerPhu opened this issue Dec 28, 2024 · 1 comment
Labels
Milestone

Comments

@TinkerPhu
Copy link

CircuitPython version

Adafruit CircuitPython 9.2.1 on 2024-11-20; Adafruit QT Py ESP32S2 with ESP32S2

Code/REPL

#===========boot.py:
import storage
storage.remount("/", readonly=False, disable_concurrent_write_protection=True)

#===========code.py:
import adafruit_connection_manager
import adafruit_ntp
import os
import time
import wifi
import rtc

wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
wifi.radio.connect(wifi_ssid, wifi_password)

pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=1, cache_seconds=3600)
path = "/sd/hello.txt"

print(time.localtime())
print(ntp.datetime)
rtc.set_time_source(ntp)
print(time.localtime())

while True:
    print(time.localtime())
    
    with open(path, "w") as f:
        f.write("hello")

    stats = os.stat(path)
    file_size = stats[6]
    file_date = stats[9]
    print("file_date:",time.localtime(file_date))
    time.sleep(5)

Behavior

file date is not synchronized with the system time.

code.py output:
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=14, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=21, tm_min=56, tm_sec=10, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=21, tm_min=56, tm_sec=10, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=21, tm_min=56, tm_sec=15, tm_wday=5, tm_yday=363, tm_isdst=-1)
file_date: struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=18, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=21, tm_min=56, tm_sec=20, tm_wday=5, tm_yday=363, tm_isdst=-1)
file_date: struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=24, tm_wday=5, tm_yday=1, tm_isdst=-1)

Description

No response

Additional information

This bug does not happen on XIAO ESP32-C3 with the same code. On XIAO ESP32-C3 there seams to be an background worker that syncronizes with ntp as soon as connection to internet is made. It does not need a manual ntp request.

@TinkerPhu TinkerPhu added the bug label Dec 28, 2024
@Neradoc
Copy link

Neradoc commented Dec 28, 2024

Hi, I see the same behavior on both an S2 and C3 QT PY.

It seems that the file time does not follow time.localtime() (set by the time source) but the internal RTC time.
I don't know enough about that to say if it's a bug or an (undocumented ?) expected behavior.
(The C code for file access might not want to rely on calling a python time source).

But file times are correct for me when not setting the time source and instead use:

rtc.RTC().datetime = ntp.datetime

Which might mean that you need to manually call that from time to time to keep it in sync.
Also note that the RTC time should be kept when the code is reloaded until the board is hard reset.

I see no difference between the QT PY C3 and the QT PY S2 with the same code (in both your code, and my modified version the boards behave identical to each other). Maybe you set the RTC on the C3 in a previous test or some other code ?

First run after changing the code as mentioned above:

struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=24, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=19, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=19, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=19, tm_wday=5, tm_yday=363, tm_isdst=-1)
file_date: struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=18, tm_wday=5, tm_yday=363, tm_isdst=-1)

Second run (time kept after soft reload)

struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=30, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=31, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=31, tm_wday=5, tm_yday=363, tm_isdst=-1)
struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=31, tm_wday=5, tm_yday=363, tm_isdst=-1)
file_date: struct_time(tm_year=2024, tm_mon=12, tm_mday=28, tm_hour=23, tm_min=11, tm_sec=30, tm_wday=5, tm_yday=363, tm_isdst=-1)

@dhalbert dhalbert added this to the 9.x.x milestone Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants