esp32/mphalport: Fix mp_hal_time_ns offset.#6388
Merged
Merged
Conversation
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 <[email protected]>
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 <[email protected]>
Contributor
|
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 |
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 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: I agree eventually we want to change the Epoch to 1970/1/1. |
Member
Author
|
A test is added for this as part of #6390 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.