Question about unix/utime.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
rguillon
Posts: 8
Joined: Thu Aug 04, 2016 2:19 pm

Question about unix/utime.

Post by rguillon » Thu Aug 25, 2016 7:20 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Question about unix/utime.

Post by dhylands » Thu Aug 25, 2016 8:50 pm

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.

rguillon
Posts: 8
Joined: Thu Aug 04, 2016 2:19 pm

Re: Question about unix/utime.

Post by rguillon » Thu Aug 25, 2016 9:31 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Question about unix/utime.

Post by dhylands » Thu Aug 25, 2016 9:58 pm

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.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Question about unix/utime.

Post by cefn » Wed Aug 02, 2017 11:11 am

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.

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)


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Question about unix/utime.

Post by dhylands » Wed Aug 02, 2017 8:36 pm

Buried in the documentation for mktime is the following:
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.
See: https://linux.die.net/man/3/mktime for the full documentation.

Post Reply