Skip to content

Added utime() to the os library #6923

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 7 commits into from
Oct 13, 2022
Merged

Added utime() to the os library #6923

merged 7 commits into from
Oct 13, 2022

Conversation

isacben
Copy link

@isacben isacben commented Sep 19, 2022

Hi,

This PR is to add the utime() function to the os library, to solve issue #5978.

Description

This implementation changes the timestamp of a file. The function takes 2 positional arguments:

  • A string with the path to the file
  • An int for the timestamp expressed in seconds

Example:

import os
os.utime('file.log', 1663573240)

Limitations

The implementation takes advantage of the function f_utime() from the oofatfs library. Because of that, utime() sets the access time and the modified time of the file to the same timestamp. Due to this limitation, utime() only takes 1 integer for the timestamp expressed in seconds, compared to the CPython implementation, which takes a 2-tuple for the access time and the modified time express either in seconds or nanoseconds.

I tested with the Adafruit Father RP2040.

Thanks!

@isacben isacben changed the title Added utime() to the os librady Added utime() to the os library Sep 19, 2022
@microdev1 microdev1 linked an issue Sep 19, 2022 that may be closed by this pull request
@microdev1 microdev1 added enhancement cpython api modules from cpython labels Sep 19, 2022
@dhalbert
Copy link
Collaborator

dhalbert commented Sep 21, 2022

We are getting incredibly short on space on the non-Express SAMD21 builds again, so when #6247 is merged, this is probably going to make some builds too big. This is true even though I disabled the bulk of #6247 on the small builds. With #6247, before this addition, the arduino_nano_33_iot ru build has only 24 bytes free.

I am not saying not to add this, just that we will need to do some more squeezing.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! We want to make our API's be the same or a subset of what CPython provides. For that reason, could you make utime() take a 2-tuple instead of a plain int? Ignore one or the other value, I guess, or require that they are the same.

You can skip the ns argument support.

From https://docs.python.org/3/library/os.html#os.utime):

os.utime(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True)

Set the access and modified times of the file specified by path.

utime() takes two optional parameters, times and ns. These specify the times set on path and are used as follows:

  • If ns is specified, it must be a 2-tuple of the form (atime_ns, mtime_ns) where each member is an int expressing nanoseconds.
  • If times is not None, it must be a 2-tuple of the form (atime, mtime) where each member is an int or float expressing seconds.
  • If times is None and ns is unspecified, this is equivalent to specifying ns=(atime_ns, mtime_ns) where both times are the current time.

@isacben
Copy link
Author

isacben commented Sep 22, 2022

Hi @dhalbert ,

Thank you for your review and feedback! I have completed the changes to accept a Tuple[int, int], and ignore the second int. Please let me know if you have any comments.

As you anticipated, there are a few builds that get too big with this change. Please let me know if there is anything I can help with.

Thank you in advance!

@isacben isacben requested a review from dhalbert September 22, 2022 17:09
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! Impl looks good. I still need to figure out how to make this fit, so I will leave it open for now.

@isacben
Copy link
Author

isacben commented Sep 27, 2022

Thank you Dan!

@dhalbert
Copy link
Collaborator

dhalbert commented Oct 8, 2022

I squeezed some builds, but since the last time the jobs were run, there was other code added elsewhere, so I need to look further.

@isacben
Copy link
Author

isacben commented Oct 9, 2022

Hi Dan. I understand. I can see this can take some time and effort.
Thank you!

@dhalbert dhalbert mentioned this pull request Oct 10, 2022
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally it fits! @isacben thanks for your patience!

@dhalbert dhalbert merged commit 80429c2 into adafruit:main Oct 13, 2022
@isacben
Copy link
Author

isacben commented Oct 14, 2022

Wow @dhalbert that's amazing!
Thank you for making it work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpython api modules from cpython enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

os.utime()
3 participants