I need the functions mktime and localtime but they are missing on the unix port.
Are they left out on purpose or is it something I could be working on?
Question about unix/utime.
Re: Question about unix/utime.
time.localtime is available in micropython-lib:
https://github.com/micropython/micropyt ... ime.py#L13
Presumably mktime could be made available using FFI as well.
https://github.com/micropython/micropyt ... ime.py#L13
Presumably mktime could be made available using FFI as well.
Re: Question about unix/utime.
Sorry if I misunderstand something but it seems to me that this only exposes the C function. Would it not need some wrapping to make it usable like the python localtime?
Re: Question about unix/utime.
Yes - you're right. It needs a small python wrapper which you'd call localtime to call localtime_ and unpack the structure and put it in a tuple.
Re: Question about unix/utime.
I just figured out that you can grab libc C function definitions via FFI, thanks, @dhylands
For others hitting the same issue, the below demonstration of dynamically binding to mktime through libc seems to work superficially, though it's minimally tested.
In particular I don't know what to do about the weekday, yearday and 9th argument to libc mktime, I just set them to 0 to get things to work in a basic way.
For others hitting the same issue, the below demonstration of dynamically binding to mktime through libc seems to work superficially, though it's minimally tested.
In particular I don't know what to do about the weekday, yearday and 9th argument to libc mktime, I just set them to 0 to get things to work in a basic way.
Code: Select all
import ure as re
import ustruct
import ffi
import ffilib
libc = ffilib.libc()
_mktime = libc.func("i", "mktime", "P")
def _tuple_to_c_tm(t):
return ustruct.pack("@iiiiiiiii", t[5], t[4], t[3], t[2], t[1] - 1, t[0] - 1900, (t[6] + 1) % 7, t[7] - 1, t[8])
def mktime(tt):
return _mktime(_tuple_to_c_tm(tt))
result = mktime((2017, 7, 4, 21, 30, 30, 0, 0, 0))
print(result)
Re: Question about unix/utime.
Buried in the documentation for mktime is the following:
See: https://linux.die.net/man/3/mktime for the full documentation.The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. The function ignores the values supplied by the caller in the tm_wday and tm_yday fields. The value specified in the tm_isdst field informs mktime() whether or not daylight saving time (DST) is in effect for the time supplied in the tm structure: a positive value means DST is in effect; zero means that DST is not in effect; and a negative value means that mktime() should (use timezone information and system databases to) attempt to determine whether DST is in effect at the specified time.