← index #16500Issue #9074
Related · high · value 2.227
QUERY · ISSUE

docs: time.mktime requires one positional argument

openby maxi07opened 2024-12-29updated 2025-10-03
docs

Documentation URL

No response

Description

when running the function time.mktime(), an exception occurs as the function does require one positional argument. But this is not documented, although it states "t’s argument is a full 8-tuple" inside the description.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

broken machine.RTC.datetime() on RP2040

closedby br0kenpixelopened 2022-08-19updated 2022-08-19
bug

I noticed that the machine.RTC.datetime() is behaving stragely.

MicroPython v1.19.1-292-g59e3348c1-dirty on 2022-08-19; Arduino Nano RP2040 Connect with RP2040
>>> import machine
>>> machine.RTC.datetime()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function missing 1 required positional arguments

I'm assuming that this function is supposed to return the currently stored date/time information in the RTC if no arguments are given and save date/time information in the RTC if an argument is given. It seems like it always requires an argument.

>>> machine.RTC.datetime((0, 0, 0, 0, 0, 0, 0, 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument has wrong type

If I give it some random date/time info, it just raises a TypeError. (same for lists)
Looking at the source code though , it seems strange: (machine_rtc.c)
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
It seems like this function requires one or two arguments.

STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
    if (n_args == 1) { //shouldn’t this be n_args == 0 ?
        bool ret;
        datetime_t t;
        ...

I changed the entire function definition to:

STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
    if (n_args == 0) {
        bool ret;
        datetime_t t;

        ret = rtc_get_datetime(&t);
        if (!ret) {
            mp_raise_OSError(MP_EIO);
        }

        mp_obj_t tuple[8] = {
            mp_obj_new_int(t.year),
            mp_obj_new_int(t.month),
            mp_obj_new_int(t.day),
            mp_obj_new_int(t.dotw),
            mp_obj_new_int(t.hour),
            mp_obj_new_int(t.min),
            mp_obj_new_int(t.sec),
            mp_obj_new_int(0)
        };

        return mp_obj_new_tuple(8, tuple);
    } else {
        mp_obj_t *items;

        mp_obj_get_array_fixed_n(args[0], 8, &items); //args[0] - first argument (list/tuple)

        datetime_t t = {
            .year = mp_obj_get_int(items[0]),
            .month = mp_obj_get_int(items[1]),
            .day = mp_obj_get_int(items[2]),
            .hour = mp_obj_get_int(items[4]),
            .min = mp_obj_get_int(items[5]),
            .sec = mp_obj_get_int(items[6]),
        };
        // Deliberately ignore the weekday argument and compute the proper value
        t.dotw = timeutils_calc_weekday(t.year, t.month, t.day);

        if (!rtc_set_datetime(&t)) {
            mp_raise_OSError(MP_EINVAL);
        }

    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 0, 1, machine_rtc_datetime);

This fixes the first problem. Calling machine.RTC.datetime() will return a tuple with some default date/time info. However, calling it with an 8-tuple will raise the same TypeError as above.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied