Ejemplo n.º 1
0
def gettimeofday(interp, as_float=False):

    space = interp.space

    void = lltype.nullptr(rffi.VOIDP.TO)
    timeval = lltype.malloc(timelib.timeval, flavor='raw')

    timelib.c_gettimeofday(timeval, void)

    if as_float:
        return space.wrap(
            intmask(timeval.c_tv_sec) +
            intmask(timeval.c_tv_usec) / 1000000.00)

    timelib_timezone = interp.get_default_timezone(
        "gettimeofday").timelib_timezone
    offset = timelib.timelib_get_time_zone_info(timeval.c_tv_sec,
                                                timelib_timezone)

    return space.new_array_from_pairs([
        (space.wrap('sec'), space.wrap(intmask(timeval.c_tv_sec))),
        (space.wrap('usec'), space.wrap(intmask(timeval.c_tv_usec))),
        (space.wrap('minuteswest'),
         space.wrap(-intmask(offset.c_offset) / 60)),
        (space.wrap('dsttime'), space.wrap(intmask(offset.c_is_dst))),
    ])
Ejemplo n.º 2
0
def get_offset(space, this, w_datetime):
    offset = timelib.timelib_get_time_zone_info(w_datetime.timelib_time.c_sse,
                                                this.timelib_timezone)
    res = offset.c_offset
    timelib.timelib_time_offset_dtor(offset)

    return space.wrap(rffi.cast(lltype.Signed, res))
Ejemplo n.º 3
0
def _mktime(interp, is_gmt, hour, minute, second, month, day, year, dst=-1):

    now = timelib.timelib_time_ctor()
    adjust_seconds = 0

    if is_gmt:
        timelib.timelib_unixtime2gmt(now, int(pytime.time()))
    else:
        now.c_tz_info = interp.get_default_timezone("mktime").timelib_timezone
        now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        timelib.timelib_unixtime2local(now, int(pytime.time()))

    if year >= 0 and year < 70:
        year += 2000
    elif year >= 70 and year <= 100:
        year += 1900

    now.c_y = rffi.cast(lltype.Signed, year)
    now.c_d = rffi.cast(lltype.Signed, day)
    now.c_m = rffi.cast(lltype.Signed, month)
    now.c_s = rffi.cast(lltype.Signed, second)
    now.c_i = rffi.cast(lltype.Signed, minute)
    now.c_h = rffi.cast(lltype.Signed, hour)

    if is_gmt:
        timelib.timelib_update_ts(now,
                                  lltype.nullptr(timelib.timelib_tzinfo.TO))
    else:
        timelib.timelib_update_ts(
            now,
            interp.get_default_timezone("mktime").timelib_timezone)

    if dst != -1:
        interp.space.ec.deprecated(
            "mktime(): The is_dst parameter is deprecated")
        if is_gmt:
            if dst == 1:
                adjust_seconds -= 3600
        else:
            tmp_offset = timelib.timelib_get_time_zone_info(
                now.c_sse,
                interp.get_default_timezone("mktime").timelib_timezone)
            if dst == 1 and intmask(tmp_offset.c_is_dst) == 0:
                adjust_seconds -= 3600
            if dst == 0 and intmask(tmp_offset.c_is_dst) == 1:
                adjust_seconds += 3600
            timelib.timelib_time_offset_dtor(tmp_offset)

    error = lltype.malloc(rffi.CArrayPtr(lltype.Signed).TO,
                          1,
                          flavor='raw',
                          zero=True)

    timestamp = timelib.timelib_date_to_int(now, error)
    timestamp += adjust_seconds

    timelib.timelib_time_dtor(now)
    lltype.free(error, flavor='raw')

    return timestamp
Ejemplo n.º 4
0
def timezone_offset_get(interp, w_datetimezone, w_datetime):
    offset = timelib.timelib_get_time_zone_info(
        w_datetime.timelib_time.c_sse,
        w_datetimezone.timezone_info.timelib_timezone)
    res = offset.c_offset
    timelib.timelib_time_offset_dtor(offset)

    return interp.space.wrap(rffi.cast(lltype.Signed, res))
Ejemplo n.º 5
0
def get_offset(space, this, w_datetime):
    offset = timelib.timelib_get_time_zone_info(
        w_datetime.timelib_time.c_sse,
        this.timelib_timezone
    )
    res = offset.c_offset
    timelib.timelib_time_offset_dtor(offset)

    return space.wrap(rffi.cast(lltype.Signed, res))
Ejemplo n.º 6
0
def timezone_offset_get(interp, w_datetimezone, w_datetime):
    offset = timelib.timelib_get_time_zone_info(
        w_datetime.timelib_time.c_sse,
        w_datetimezone.timezone_info.timelib_timezone
    )
    res = offset.c_offset
    timelib.timelib_time_offset_dtor(offset)

    return interp.space.wrap(rffi.cast(lltype.Signed, res))
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def get_offset(interp, this):

    zone_type = rffi.cast(lltype.Signed, this.timelib_time.c_zone_type)
    if zone_type == timelib.ZONETYPE_ID:
        offset = timelib.timelib_get_time_zone_info(
            this.timelib_time.c_sse, this.timelib_time.c_tz_info)
        value = int(offset.c_offset)
        timelib.timelib_time_offset_dtor(offset)
        return interp.space.wrap(value)

    if zone_type == timelib.ZONETYPE_ABBR:
        return interp.space.wrap(int(this.timelib_time.c_z) * -60)

    if zone_type == timelib.ZONETYPE_OFFSET:
        return interp.space.wrap(
            int(this.timelib_time.c_z) -
            (60 * rffi.cast(lltype.Signed, this.timelib_time.c_dst)) * -60)

    return this
Ejemplo n.º 10
0
def get_offset(interp, this):

    zone_type = rffi.cast(lltype.Signed, this.timelib_time.c_zone_type)
    if zone_type == timelib.ZONETYPE_ID:
        offset = timelib.timelib_get_time_zone_info(
            this.timelib_time.c_sse, this.timelib_time.c_tz_info)
        value = int(offset.c_offset)
        timelib.timelib_time_offset_dtor(offset)
        return interp.space.wrap(value)

    if zone_type == timelib.ZONETYPE_ABBR:
        return interp.space.wrap(int(this.timelib_time.c_z) * -60)

    if zone_type == timelib.ZONETYPE_OFFSET:
        return interp.space.wrap(
            int(this.timelib_time.c_z) - (60 *
                rffi.cast(lltype.Signed, this.timelib_time.c_dst)) * -60)

    return this
Ejemplo n.º 11
0
def date_offset_get(interp, w_date):
    zone_type = rffi.cast(lltype.Signed, w_date.timelib_time.c_zone_type)

    if zone_type == timelib.ZONETYPE_ID:
        offset = timelib.timelib_get_time_zone_info(
            w_date.timelib_time.c_sse, w_date.timelib_time.c_tz_info)
        value = int(offset.c_offset)
        timelib.timelib_time_offset_dtor(offset)
        return interp.space.wrap(value)

    if zone_type == timelib.ZONETYPE_ABBR:
        dst = rffi.cast(lltype.Signed, w_date.timelib_time.c_dst) * 3600
        return interp.space.wrap(int(w_date.timelib_time.c_z) * -60 + dst)

    if zone_type == timelib.ZONETYPE_OFFSET:
        return interp.space.wrap(
            int(w_date.timelib_time.c_z) - (60 *
                rffi.cast(lltype.Signed, w_date.timelib_time.c_dst)) * -60)

    return w_date
Ejemplo n.º 12
0
def date_offset_get(interp, w_date):
    zone_type = rffi.cast(lltype.Signed, w_date.timelib_time.c_zone_type)

    if zone_type == timelib.ZONETYPE_ID:
        offset = timelib.timelib_get_time_zone_info(
            w_date.timelib_time.c_sse, w_date.timelib_time.c_tz_info)
        value = int(offset.c_offset)
        timelib.timelib_time_offset_dtor(offset)
        return interp.space.wrap(value)

    if zone_type == timelib.ZONETYPE_ABBR:
        dst = rffi.cast(lltype.Signed, w_date.timelib_time.c_dst) * 3600
        return interp.space.wrap(int(w_date.timelib_time.c_z) * -60 + dst)

    if zone_type == timelib.ZONETYPE_OFFSET:
        return interp.space.wrap(
            int(w_date.timelib_time.c_z) -
            (60 * rffi.cast(lltype.Signed, w_date.timelib_time.c_dst)) * -60)

    return w_date
Ejemplo n.º 13
0
def gettimeofday(interp, as_float=False):

    space = interp.space

    void = lltype.nullptr(rffi.VOIDP.TO)
    timeval = lltype.malloc(timelib.timeval, flavor='raw')

    timelib.c_gettimeofday(timeval, void)

    if as_float:
        return space.wrap(intmask(timeval.c_tv_sec) + intmask(timeval.c_tv_usec) / 1000000.00)

    timelib_timezone = interp.get_default_timezone("gettimeofday").timelib_timezone
    offset = timelib.timelib_get_time_zone_info(timeval.c_tv_sec, timelib_timezone)

    return space.new_array_from_pairs([
            (space.wrap('sec'), space.wrap(intmask(timeval.c_tv_sec))),
            (space.wrap('usec'), space.wrap(intmask(timeval.c_tv_usec))),
            (space.wrap('minuteswest'), space.wrap(-intmask(offset.c_offset) / 60)),
            (space.wrap('dsttime'), space.wrap(intmask(offset.c_is_dst))),
        ])
Ejemplo n.º 14
0
 def abbr(self, timestamp):
     offset = timelib.timelib_get_time_zone_info(timestamp, self.local_tzi)
     res = rffi.charp2str(offset.c_abbr)
     timelib.timelib_time_offset_dtor(offset)
     return res
Ejemplo n.º 15
0
 def is_dst(self, timestamp):
     offset = timelib.timelib_get_time_zone_info(timestamp, self.local_tzi)
     res = offset.c_is_dst
     timelib.timelib_time_offset_dtor(offset)
     return rffi.cast(lltype.Signed, res)
Ejemplo n.º 16
0
def _mktime(interp, is_gmt, hour, minute, second,
            month, day, year, dst=-1):

    now = timelib.timelib_time_ctor()
    adjust_seconds = 0

    if is_gmt:
        timelib.timelib_unixtime2gmt(now, int(pytime.time()))
    else:
        now.c_tz_info = interp.get_default_timezone("mktime").timelib_timezone
        now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        timelib.timelib_unixtime2local(now, int(pytime.time()))

    if year >= 0 and year < 70:
        year += 2000
    elif year >= 70 and year <= 100:
        year += 1900

    now.c_y = rffi.cast(lltype.Signed, year)
    now.c_d = rffi.cast(lltype.Signed, day)
    now.c_m = rffi.cast(lltype.Signed, month)
    now.c_s = rffi.cast(lltype.Signed, second)
    now.c_i = rffi.cast(lltype.Signed, minute)
    now.c_h = rffi.cast(lltype.Signed, hour)

    if is_gmt:
        timelib.timelib_update_ts(
            now,
            lltype.nullptr(timelib.timelib_tzinfo.TO))
    else:
        timelib.timelib_update_ts(
            now,
            interp.get_default_timezone("mktime").timelib_timezone)

    if dst != -1:
        interp.space.ec.deprecated(
            "mktime(): The is_dst parameter is deprecated"
        )
        if is_gmt:
            if dst == 1:
                adjust_seconds -= 3600
        else:
            tmp_offset = timelib.timelib_get_time_zone_info(
                now.c_sse, interp.get_default_timezone("mktime").timelib_timezone
            )
            if dst == 1 and intmask(tmp_offset.c_is_dst) == 0:
                adjust_seconds -= 3600
            if dst == 0 and intmask(tmp_offset.c_is_dst) == 1:
                adjust_seconds += 3600
            timelib.timelib_time_offset_dtor(tmp_offset)

    error = lltype.malloc(
        rffi.CArrayPtr(lltype.Signed).TO, 1, flavor='raw', zero=True
    )

    timestamp = timelib.timelib_date_to_int(now, error)
    timestamp += adjust_seconds

    timelib.timelib_time_dtor(now)
    lltype.free(error, flavor='raw')

    return timestamp