Beispiel #1
0
def localtime(interp, timestamp=int(pytime.time()), is_associative=False):
    space = interp.space

    timelib_timezone = interp.get_default_timezone("mktime").timelib_timezone

    timelib_time = timelib.timelib_time_ctor()
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    if is_associative:
        return_value = space.new_array_from_pairs([
            (space.wrap("tm_sec"), space.wrap(timelib_time.c_s)),
            (space.wrap("tm_min"), space.wrap(timelib_time.c_i)),
            (space.wrap("tm_hour"), space.wrap(timelib_time.c_h)),
            (space.wrap("tm_mday"), space.wrap(timelib_time.c_d)),
            (space.wrap("tm_mon"), space.wrap(timelib_time.c_m - 1)),
            (space.wrap("tm_year"), space.wrap(timelib_time.c_y - 1900)),
            (space.wrap("tm_wday"),
             space.wrap(
                 timelib.timelib_day_of_week(timelib_time.c_y,
                                             timelib_time.c_m,
                                             timelib_time.c_d))),
            (space.wrap("tm_yday"),
             space.wrap(
                 timelib.timelib_day_of_year(timelib_time.c_y,
                                             timelib_time.c_m,
                                             timelib_time.c_d))),
            (space.wrap("tm_isdst"), space.wrap(int(timelib_time.c_dst))),
        ])
    else:
        return_value = interp.space.new_array_from_list([
            space.wrap(timelib_time.c_s),
            space.wrap(timelib_time.c_i),
            space.wrap(timelib_time.c_h),
            space.wrap(timelib_time.c_d),
            space.wrap(timelib_time.c_m - 1),
            space.wrap(timelib_time.c_y - 1900),
            space.wrap(
                timelib.timelib_day_of_week(timelib_time.c_y, timelib_time.c_m,
                                            timelib_time.c_d)),
            space.wrap(
                timelib.timelib_day_of_year(timelib_time.c_y, timelib_time.c_m,
                                            timelib_time.c_d)),
            space.wrap(int(timelib_time.c_dst)),
        ])

    timelib.timelib_time_dtor(timelib_time)

    return return_value
Beispiel #2
0
def localtime(interp, timestamp=int(pytime.time()), is_associative=False):
    space = interp.space

    timelib_timezone = interp.get_default_timezone("mktime").timelib_timezone

    timelib_time = timelib.timelib_time_ctor()
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    if is_associative:
        return_value = space.new_array_from_pairs([
            (space.wrap("tm_sec"),   space.wrap(timelib_time.c_s)),
            (space.wrap("tm_min"),   space.wrap(timelib_time.c_i)),
            (space.wrap("tm_hour"),  space.wrap(timelib_time.c_h)),
            (space.wrap("tm_mday"),  space.wrap(timelib_time.c_d)),
            (space.wrap("tm_mon"),   space.wrap(timelib_time.c_m - 1)),
            (space.wrap("tm_year"),  space.wrap(timelib_time.c_y - 1900)),
            (space.wrap("tm_wday"),  space.wrap(timelib.timelib_day_of_week(
                timelib_time.c_y,
                timelib_time.c_m,
                timelib_time.c_d))),
            (space.wrap("tm_yday"),  space.wrap(timelib.timelib_day_of_year(
                timelib_time.c_y,
                timelib_time.c_m,
                timelib_time.c_d))),
            (space.wrap("tm_isdst"), space.wrap(int(timelib_time.c_dst))),
        ])
    else:
        return_value = interp.space.new_array_from_list([
            space.wrap(timelib_time.c_s),
            space.wrap(timelib_time.c_i),
            space.wrap(timelib_time.c_h),
            space.wrap(timelib_time.c_d),
            space.wrap(timelib_time.c_m - 1),
            space.wrap(timelib_time.c_y - 1900),
            space.wrap(timelib.timelib_day_of_week(
                timelib_time.c_y,
                timelib_time.c_m,
                timelib_time.c_d)),
            space.wrap(timelib.timelib_day_of_year(
                timelib_time.c_y,
                timelib_time.c_m,
                timelib_time.c_d)),
            space.wrap(int(timelib_time.c_dst)),
        ])

    timelib.timelib_time_dtor(timelib_time)

    return return_value
Beispiel #3
0
def getdate(interp, timestamp=int(pytime.time())):

    space = interp.space

    timelib_timezone = interp.get_default_timezone("getdate").timelib_timezone

    timelib_time = timelib.timelib_time_ctor()
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    return_value = space.new_array_from_pairs([
        (space.wrap("seconds"),   space.wrap(timelib_time.c_s)),
        (space.wrap("minutes"),   space.wrap(timelib_time.c_i)),
        (space.wrap("hours"),  space.wrap(timelib_time.c_h)),
        (space.wrap("mday"),  space.wrap(timelib_time.c_d)),
        (space.wrap("wday"),  space.wrap(timelib.timelib_day_of_week(
            timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("mon"),   space.wrap(timelib_time.c_m)),
        (space.wrap("year"),  space.wrap(timelib_time.c_y)),
        (space.wrap("yday"),  space.wrap(timelib.timelib_day_of_year(
            timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("weekday"),  space.wrap(
            timelib.full_day_name(timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("month"),  space.wrap(timelib.full_month_names[timelib_time.c_m - 1])),

        (space.wrap("0"), space.wrap(timestamp)),
    ])

    timelib.timelib_time_dtor(timelib_time)

    return return_value
Beispiel #4
0
def getdate(interp, timestamp=int(pytime.time())):

    space = interp.space

    timelib_timezone = interp.get_default_timezone("getdate").timelib_timezone

    timelib_time = timelib.timelib_time_ctor()
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    return_value = space.new_array_from_pairs([
        (space.wrap("seconds"),   space.wrap(timelib_time.c_s)),
        (space.wrap("minutes"),   space.wrap(timelib_time.c_i)),
        (space.wrap("hours"),  space.wrap(timelib_time.c_h)),
        (space.wrap("mday"),  space.wrap(timelib_time.c_d)),
        (space.wrap("wday"),  space.wrap(timelib.timelib_day_of_week(
            timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("mon"),   space.wrap(timelib_time.c_m)),
        (space.wrap("year"),  space.wrap(timelib_time.c_y)),
        (space.wrap("yday"),  space.wrap(timelib.timelib_day_of_year(
            timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("weekday"),  space.wrap(
            timelib.full_day_name(timelib_time.c_y, timelib_time.c_m, timelib_time.c_d))),
        (space.wrap("month"),  space.wrap(timelib.full_month_names[timelib_time.c_m - 1])),

        (space.wrap("0"), space.wrap(timestamp)),
    ])

    timelib.timelib_time_dtor(timelib_time)

    return return_value
Beispiel #5
0
def _strftime(interp, is_gmt, format_string, timestamp):
    offset = lltype.nullptr(timelib.timelib_time_offset.TO)
    ta = lltype.malloc(timelib.tm, flavor='raw', zero=True)

    timelib_time = timelib.timelib_time_ctor()
    if is_gmt:
        timelib_timezone = lltype.nullptr(timelib.timelib_tzinfo.TO)
        timelib.timelib_unixtime2gmt(timelib_time, timestamp)
    else:
        timelib_timezone = interp.get_default_timezone(
            "getdate").timelib_timezone
        timelib_time.c_tz_info = timelib_timezone
        timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        timelib.timelib_unixtime2local(timelib_time, timestamp)

    ta.c_tm_sec = rffi.cast(rffi.INT, timelib_time.c_s)
    ta.c_tm_min = rffi.cast(rffi.INT, timelib_time.c_i)
    ta.c_tm_hour = rffi.cast(rffi.INT, timelib_time.c_h)
    ta.c_tm_mday = rffi.cast(rffi.INT, timelib_time.c_d)
    ta.c_tm_mon = rffi.cast(rffi.INT, timelib_time.c_m - 1)
    ta.c_tm_year = rffi.cast(rffi.INT, timelib_time.c_y - 1900)
    ta.c_tm_wday = rffi.cast(
        rffi.INT,
        timelib.timelib_day_of_week(timelib_time.c_y, timelib_time.c_m,
                                    timelib_time.c_d))
    ta.c_tm_yday = rffi.cast(
        rffi.INT,
        timelib.timelib_day_of_year(timelib_time.c_y, timelib_time.c_m,
                                    timelib_time.c_d))

    if is_gmt:
        ta.c_tm_isdst = rffi.cast(rffi.INT, 0)
        ta.c_tm_gmtoff = rffi.cast(lltype.Signed, 0)
        ta.c_tm_zone = rffi.str2charp("GMT")
    else:
        offset = timelib.timelib_get_time_zone_info(timestamp,
                                                    timelib_timezone)
        ta.c_tm_isdst = rffi.cast(rffi.INT, offset.c_is_dst)
        ta.c_tm_gmtoff = rffi.cast(lltype.Signed, offset.c_offset)
        ta.c_tm_zone = offset.c_abbr

    # stolen from PyPy
    i = 1024
    while True:
        outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw')
        try:
            buflen = timelib.c_strftime(outbuf, i, format_string, ta)
            if buflen > 0 or i >= 256 * len(format_string):
                return rffi.charp2strn(outbuf, intmask(buflen))
        finally:
            timelib.timelib_time_dtor(timelib_time)
            lltype.free(outbuf, flavor='raw')
            if offset:
                timelib.timelib_time_offset_dtor(offset)
        i += i
Beispiel #6
0
def _strftime(interp, is_gmt, format_string, timestamp):
    offset = lltype.nullptr(timelib.timelib_time_offset.TO)
    ta = lltype.malloc(timelib.tm, flavor='raw', zero=True)

    timelib_time = timelib.timelib_time_ctor()
    if is_gmt:
        timelib_timezone = lltype.nullptr(timelib.timelib_tzinfo.TO)
        timelib.timelib_unixtime2gmt(timelib_time, timestamp)
    else:
        timelib_timezone = interp.get_default_timezone("getdate").timelib_timezone
        timelib_time.c_tz_info = timelib_timezone
        timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        timelib.timelib_unixtime2local(timelib_time, timestamp)

    ta.c_tm_sec   = rffi.cast(rffi.INT, timelib_time.c_s)
    ta.c_tm_min   = rffi.cast(rffi.INT, timelib_time.c_i)
    ta.c_tm_hour  = rffi.cast(rffi.INT, timelib_time.c_h)
    ta.c_tm_mday  = rffi.cast(rffi.INT, timelib_time.c_d)
    ta.c_tm_mon   = rffi.cast(rffi.INT, timelib_time.c_m - 1)
    ta.c_tm_year  = rffi.cast(rffi.INT, timelib_time.c_y - 1900)
    ta.c_tm_wday  = rffi.cast(rffi.INT, timelib.timelib_day_of_week(
        timelib_time.c_y,
        timelib_time.c_m,
        timelib_time.c_d
    ))
    ta.c_tm_yday  = rffi.cast(rffi.INT, timelib.timelib_day_of_year(
        timelib_time.c_y,
        timelib_time.c_m,
        timelib_time.c_d
    ))

    if is_gmt:
        ta.c_tm_isdst = rffi.cast(rffi.INT, 0)
        ta.c_tm_gmtoff = rffi.cast(lltype.Signed, 0)
        ta.c_tm_zone = rffi.str2charp("GMT")
    else:
        offset = timelib.timelib_get_time_zone_info(timestamp, timelib_timezone)
        ta.c_tm_isdst = rffi.cast(rffi.INT, offset.c_is_dst)
        ta.c_tm_gmtoff = rffi.cast(lltype.Signed, offset.c_offset)
        ta.c_tm_zone = offset.c_abbr

    # stolen from PyPy
    i = 1024
    while True:
        outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw')
        try:
            buflen = timelib.c_strftime(outbuf, i, format_string, ta)
            if buflen > 0 or i >= 256 * len(format_string):
                return rffi.charp2strn(outbuf, intmask(buflen))
        finally:
            timelib.timelib_time_dtor(timelib_time)
            lltype.free(outbuf, flavor='raw')
            if offset:
                timelib.timelib_time_offset_dtor(offset)
        i += i
Beispiel #7
0
def idate(interp, date_format, timestamp=int(pytime.time())):
    space = interp.space

    if len(date_format) != 1:
        interp.space.ec.warn("idate(): idate format is one char")
        return interp.space.w_False

    timelib_timezone = interp.get_default_timezone("idate").timelib_timezone
    timelib_time = timelib.timelib_time_from_timestamp(timestamp,
                                                       timelib_timezone)

    if date_format == 'B':
        retval = (((timelib_time.c_sse) -
                   ((timelib_time.c_sse) -
                    (((timelib_time.c_sse) % 86400) + 3600))) * 10) / 864

        while retval < 0:
            retval += 1000

        return space.wrap(retval % 1000)

    elif date_format == 'd':
        return space.wrap(timelib_time.c_d)

    elif date_format == 'h':
        if timelib_time.c_h % 12:
            return space.wrap(timelib_time.c_h % 12)
        else:
            return space.wrap(12)

    elif date_format == 'H':
        return space.wrap(timelib_time.c_h)

    elif date_format == 'i':
        return space.wrap(timelib_time.c_i)

    elif date_format == 'I':
        raise NotImplementedError()

    elif date_format == 'L':
        return space.wrap(int(timelib.is_leap(timelib_time.c_y)))

    elif date_format == 'm':
        return space.wrap(timelib_time.c_m)

    elif date_format == 's':
        return space.wrap(timelib_time.c_s)

    elif date_format == 't':
        return space.wrap(
            timelib.timelib_days_in_month(timelib_time.c_y, timelib_time.c_m))

    elif date_format == 'U':
        return space.wrap(timelib_time.c_sse)

    elif date_format == 'w':
        return space.wrap(
            timelib.timelib_day_of_week(timelib_time.c_y, timelib_time.c_m,
                                        timelib_time.c_d))

    elif date_format == 'W':
        return space.wrap(
            timelib.isoweek_from_date(timelib_time.c_y, timelib_time.c_m,
                                      timelib_time.c_d))

    elif date_format == 'y':
        return space.wrap(timelib_time.c_y)

    elif date_format == 'Y':
        return space.wrap(timelib_time.c_y)

    elif date_format == 'z':
        return space.wrap(
            timelib.timelib_day_of_year(timelib_time.c_y, timelib_time.c_m,
                                        timelib_time.c_d))

    elif date_format == 'Z':
        raise NotImplementedError()

    else:
        interp.space.ec.warn("idate(): Unrecognized date format token.")
        return interp.space.w_False
Beispiel #8
0
def idate(interp, date_format, timestamp=int(pytime.time())):
    space = interp.space

    if len(date_format) != 1:
        interp.space.ec.warn("idate(): idate format is one char")
        return interp.space.w_False

    timelib_timezone = interp.get_default_timezone("idate").timelib_timezone
    timelib_time = timelib.timelib_time_from_timestamp(timestamp, timelib_timezone)

    if date_format == 'B':
        retval = (
            ((timelib_time.c_sse)-(
                (timelib_time.c_sse) - (((timelib_time.c_sse) % 86400) + 3600))) * 10
        ) / 864


        while retval < 0:
            retval += 1000

        return space.wrap(retval % 1000)

    elif date_format == 'd':
        return space.wrap(timelib_time.c_d)

    elif date_format == 'h':
        if timelib_time.c_h % 12:
            return space.wrap(timelib_time.c_h % 12)
        else:
            return space.wrap(12)

    elif date_format == 'H':
        return space.wrap(timelib_time.c_h)

    elif date_format == 'i':
        return space.wrap(timelib_time.c_i)

    elif date_format == 'I':
        raise NotImplementedError()

    elif date_format == 'L':
        return space.wrap(int(timelib.is_leap(timelib_time.c_y)))

    elif date_format == 'm':
        return space.wrap(timelib_time.c_m)

    elif date_format == 's':
        return space.wrap(timelib_time.c_s)

    elif date_format == 't':
        return space.wrap(
            timelib.timelib_days_in_month(timelib_time.c_y, timelib_time.c_m)
        )

    elif date_format == 'U':
        return space.wrap(timelib_time.c_sse)

    elif date_format == 'w':
        return space.wrap(
            timelib.timelib_day_of_week(
                timelib_time.c_y, timelib_time.c_m, timelib_time.c_d
            ))

    elif date_format == 'W':
        return space.wrap(
            timelib.isoweek_from_date(
                timelib_time.c_y, timelib_time.c_m, timelib_time.c_d)
        )

    elif date_format == 'y':
        return space.wrap(timelib_time.c_y)

    elif date_format == 'Y':
        return space.wrap(timelib_time.c_y)

    elif date_format == 'z':
        return space.wrap(
            timelib.timelib_day_of_year(
                timelib_time.c_y, timelib_time.c_m, timelib_time.c_d
            ))

    elif date_format == 'Z':
        raise NotImplementedError()

    else:
        interp.space.ec.warn("idate(): Unrecognized date format token.")
        return interp.space.w_False