Skip to content

esp32/mphalport: Fix mp_hal_time_ns offset. #6388

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

Merged
merged 1 commit into from
Sep 1, 2020

Conversation

dpgeorge
Copy link
Member

gettimeofday returns seconds since 2000/1/1 so needs to be adjusted to seconds since 1970/1/1 to give the correct return value of mp_hal_time_ns.

gettimeofday returns seconds since 2000/1/1 so needs to be adjusted to
seconds since 1970/1/1 to give the correct return value of mp_hal_time_ns.

Signed-off-by: Damien George <damien@micropython.org>
dpgeorge referenced this pull request Aug 30, 2020
This commit adds support for modification time of files on littlefs v2
filesystems, using file attributes.  For some background see issue #6114.

Features/properties of this implementation:
- Only supported on littlefs2 (not littlefs1).
- Uses littlefs2's general file attributes to store the timestamp.
- The timestamp is 64-bits and stores nanoseconds since 1970/1/1 (if the
  range to the year 2554 is not enough then additional bits can be added to
  this timestamp by adding another file attribute).
- mtime is enabled by default but can be disabled in the constructor, eg:
  uos.mount(uos.VfsLfs2(bdev, mtime=False), '/flash')
- It's fully backwards compatible, existing littlefs2 filesystems will work
  without reformatting and timestamps will be added transparently to
  existing files (once they are opened for writing).
- Files without timestamps will open correctly, and stat will just return 0
  for their timestamp.
- mtime can be disabled or enabled each mount time and timestamps will only
  be updated if mtime is enabled (otherwise they will be untouched).

Signed-off-by: Damien George <damien@micropython.org>
@tve
Copy link
Contributor

tve commented Aug 30, 2020

Are you sure this is the way to go: to expand the use of the odd 2000/1/1 epoch? That makes fixing things so they use the "standard" unix/CPython epoch even harder. E.g. #5973

@dpgeorge
Copy link
Member Author

There's not much choice here, the patch is just making littlefs timestamps the same as existing FAT timestamps, which then match the value returned from time.time().

Here's my test script:

import time, uos


class RAMBlockDevice:
    def __init__(self, block_size, num_blocks):
        self.block_size = block_size
        self.data = bytearray(num_blocks * self.block_size)

    def readblocks(self, block, buf, off=0):
        addr = block * self.block_size + off
        for i in range(len(buf)):
            buf[i] = self.data[addr + i]

    def writeblocks(self, block, buf, off=None):
        if off is None:
            # do erase, then write
            for i in range(len(buf) // self.block_size):
                self.ioctl(6, block + i)
            off = 0
        addr = block * self.block_size + off
        self.data[addr:addr + len(buf)] = buf

    def ioctl(self, op, arg):
        if op == 4:  # block count
            return len(self.data) // self.block_size
        if op == 5:  # block size
            return self.block_size
        if op == 6:  # erase block
            return 0

print(time.time())
print(time.localtime(time.time()))

bdev1 = RAMBlockDevice(512, 64)
uos.VfsFat.mkfs(bdev1)
uos.mount(uos.VfsFat(bdev1), "/fat")

bdev2 = RAMBlockDevice(256, 16)
uos.VfsLfs2.mkfs(bdev2)
uos.mount(uos.VfsLfs2(bdev2), "/lfs")

with open("/fat/test", "w") as f:
    f.write("hello")

with open("/lfs/test", "w") as f:
    f.write("hello")

s = uos.stat("/fat/test")
print(s)
print(time.localtime(s[-1]))

s = uos.stat("/lfs/test")
print(s)
print(time.localtime(s[-1]))

Output with this patch:

48
(2000, 1, 1, 0, 0, 48, 5, 1)
(32768, 0, 0, 0, 0, 0, 5, 48, 48, 48)
(2000, 1, 1, 0, 0, 48, 5, 1)
(32768, 0, 0, 0, 0, 0, 5, 48, 48, 48)
(2000, 1, 1, 0, 0, 48, 5, 1)

I agree eventually we want to change the Epoch to 1970/1/1.

@dpgeorge
Copy link
Member Author

A test is added for this as part of #6390

@dpgeorge dpgeorge merged commit 40153b8 into micropython:master Sep 1, 2020
@dpgeorge dpgeorge deleted the esp32-fix-time-ns-offset branch September 1, 2020 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants