Beispiel #1
0
def initialize_date_from_format(interp, func_name, w_date, format_string,
                                time_string, w_datetimezone):

    if not w_datetimezone:
        w_date.timezone = interp.get_default_timezone(func_name)
    else:
        w_date.timezone = w_datetimezone

    w_date.timelib_time, error = timelib.timelib_time_from_format(
        format_string, time_string)

    now = timelib.timelib_time_ctor()
    now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
    now.c_tz_info = w_date.timezone.timelib_timezone
    zone_type = rffi.cast(lltype.Signed, w_date.timelib_time.c_zone_type)

    if zone_type == timelib.ZONETYPE_ID:
        pass
    elif zone_type == timelib.ZONETYPE_ABBR:
        pass
    elif zone_type == timelib.ZONETYPE_OFFSET:
        pass

    timelib.timelib_unixtime2local(now, int(time.time()))
    timelib.timelib_fill_holes(w_date.timelib_time, now,
                               timelib.TIMELIB_NO_CLONE)
    timelib.timelib_update_ts(w_date.timelib_time,
                              w_date.timezone.timelib_timezone)
    timelib.timelib_time_dtor(now)

    w_date.timelib_time.c_have_relative = rffi.cast(
        timelib.timelib_time.TO.c_have_relative, 1)

    return error
Beispiel #2
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
Beispiel #3
0
def date_sunrise_sunset(func_name, interp, num_args, timestamp, return_format,
                        latitude, longitude, zenith, gmt_offset):

    sunrise = False
    if func_name == "date_sunrise":
        sunrise = True

    if return_format not in [SUNFUNCS_RET_TIMESTAMP,
                             SUNFUNCS_RET_STRING,
                             SUNFUNCS_RET_DOUBLE]:
        interp.space.ec.warn(
            "%s(): Wrong return format given, pick one of "
            "SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE"
            % func_name)
        return interp.space.w_False

    altitude = 90 - zenith

    timelib_time = timelib.timelib_time_ctor()
    timelib_timezone = interp.get_default_timezone("date").timelib_timezone
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    c_h_rise = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_h_set = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_rise = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_set = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_transit = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')

    rs = timelib.timelib_astro_rise_set_altitude(
        timelib_time, longitude, latitude, altitude, 1,
        c_h_rise, c_h_set, c_rise, c_set, c_transit
    )

    if num_args <= 5:
        gmt_offset = float(timelib.timelib_get_current_offset(timelib_time) / 3600)

    timelib.timelib_time_dtor(timelib_time)

    if rs != 0:
        return interp.space.w_False

    if return_format == 0:
        return interp.space.wrap(c_rise[0] if sunrise else c_set[0])

    N = (c_h_rise[0] if sunrise else c_h_set[0]) + gmt_offset

    if N > 24 or N < 0:
        N -= math.floor(N / 24) * 24

    if return_format == 1:
        return interp.space.wrap("%s:%s" % (
            timelib.format_to(2, int(math.floor(N))),
            timelib.format_to(2, int(math.floor(60 * (N - int(N)))))
        ))

    elif return_format == 2:
        return interp.space.wrap(N)
Beispiel #4
0
def initialize_date_from_format(interp, func_name, w_date,
                                format_string, time_string, w_datetimezone):

    if not w_datetimezone:
        w_date.timezone = interp.get_default_timezone(func_name)
    else:
        w_date.timezone = w_datetimezone

    w_date.timelib_time, error = timelib.timelib_time_from_format(
        format_string, time_string
    )

    now = timelib.timelib_time_ctor()
    now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
    now.c_tz_info = w_date.timezone.timelib_timezone
    zone_type = rffi.cast(lltype.Signed, w_date.timelib_time.c_zone_type)

    if zone_type == timelib.ZONETYPE_ID:
        pass
    elif zone_type == timelib.ZONETYPE_ABBR:
        pass
    elif zone_type == timelib.ZONETYPE_OFFSET:
        pass

    timelib.timelib_unixtime2local(now, int(time.time()))
    timelib.timelib_fill_holes(w_date.timelib_time, now, timelib.TIMELIB_NO_CLONE)
    timelib.timelib_update_ts(w_date.timelib_time, w_date.timezone.timelib_timezone)
    timelib.timelib_time_dtor(now)

    w_date.timelib_time.c_have_relative = rffi.cast(
        timelib.timelib_time.TO.c_have_relative, 1
    )

    return error
Beispiel #5
0
def set_timezone(interp, this, w_datetimezone):
    this.w_datetimezone = w_datetimezone

    timelib.timelib_set_timezone(this.timelib_time, this.w_datetimezone.timezone_info.timelib_timezone)
    timelib.timelib_unixtime2local(this.timelib_time, this.timelib_time.c_sse)

    return this
Beispiel #6
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 #7
0
def set_timestamp(interp, this, timestamp):

    timelib.timelib_unixtime2local(this.timelib_time, timestamp)
    timelib.timelib_update_ts(this.timelib_time,
                              lltype.nullptr(timelib.timelib_tzinfo.TO))

    return this
Beispiel #8
0
def initialize_date(interp,
                    func_name,
                    this,
                    time_string=None,
                    w_datetimezone=None):
    from hippy.module.date.datetimezone_klass import W_DateTimeZone, k_DateTimeZone

    this.timelib_time, error = timelib.timelib_time_from_string(time_string)
    this.timelib_time.c_zone_type, this.timelib_time.c_tz_info
    zone_type = rffi.cast(lltype.Signed, this.timelib_time.c_zone_type)

    timezone_offset = None

    if w_datetimezone:
        timelib_timezone = w_datetimezone.timelib_timezone
    elif zone_type == timelib.ZONETYPE_ID:
        timelib_timezone = this.timelib_time.c_tz_info
    elif zone_type == timelib.ZONETYPE_ABBR:
        timelib_timezone = timelib.timelib_parse_tzfile(
            this.timelib_time.c_tz_abbr, timelib.timelib_builtin_db())

        if not timelib_timezone:
            timelib_timezone = interp.get_default_timezone(
                func_name).timelib_timezone

    elif zone_type == timelib.ZONETYPE_OFFSET:
        timelib_timezone = lltype.nullptr(timelib.timelib_tzinfo.TO)

        offset = timelib.timelib_get_current_offset(this.timelib_time) / 36
        mark = '+' if offset >= 0 else ''
        h, m = offset / 100, offset % 100
        "%s%s:%s" % (mark, timelib.format_to(2, h), timelib.format_to(2, m))

        timezone_offset = "%s%s:%s" % (mark, timelib.format_to(
            2, h), timelib.format_to(2, m))

    else:
        timelib_timezone = interp.get_default_timezone(
            func_name).timelib_timezone

    if timelib_timezone:
        now = timelib.timelib_time_ctor()
        now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        now.c_tz_info = timelib_timezone

        timelib.timelib_unixtime2local(now, int(time.time()))
        timelib.timelib_fill_holes(this.timelib_time, now,
                                   timelib.TIMELIB_NO_CLONE)
        timelib.timelib_update_ts(this.timelib_time, timelib_timezone)
        timelib.timelib_time_dtor(now)

        this.timelib_time.c_have_relative = rffi.cast(
            timelib.timelib_time.TO.c_have_relative, 1)

    this.w_datetimezone = W_DateTimeZone(k_DateTimeZone, [])

    this.w_datetimezone.timezone_info = TimeZoneWrapper(
        timelib_timezone, zone_type, timezone_offset)

    return error
Beispiel #9
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 #10
0
def initialize_date(interp, func_name, this, time_string=None, w_datetimezone=None):
    from hippy.module.date.datetimezone_klass import W_DateTimeZone, k_DateTimeZone

    this.timelib_time, error = timelib.timelib_time_from_string(time_string)
    this.timelib_time.c_zone_type, this.timelib_time.c_tz_info
    zone_type = rffi.cast(lltype.Signed, this.timelib_time.c_zone_type)

    timezone_offset = None

    if w_datetimezone:
        timelib_timezone = w_datetimezone.timelib_timezone
    elif zone_type == timelib.ZONETYPE_ID:
        timelib_timezone = this.timelib_time.c_tz_info
    elif zone_type == timelib.ZONETYPE_ABBR:
        timelib_timezone = timelib.timelib_parse_tzfile(
            this.timelib_time.c_tz_abbr,
            timelib.timelib_builtin_db()
        )

        if not timelib_timezone:
            timelib_timezone = interp.get_default_timezone(func_name).timelib_timezone

    elif zone_type == timelib.ZONETYPE_OFFSET:
        timelib_timezone = lltype.nullptr(timelib.timelib_tzinfo.TO)

        offset = timelib.timelib_get_current_offset(this.timelib_time) / 36
        mark = '+' if offset >= 0 else ''
        h, m = offset / 100, offset % 100
        "%s%s:%s" % (mark, timelib.format_to(2, h), timelib.format_to(2, m))

        timezone_offset = "%s%s:%s" % (
            mark, timelib.format_to(2, h),
            timelib.format_to(2, m)
        )

    else:
        timelib_timezone = interp.get_default_timezone(func_name).timelib_timezone

    if timelib_timezone:
        now = timelib.timelib_time_ctor()
        now.c_zone_type = timelib.TIMELIB_ZONETYPE_ID
        now.c_tz_info = timelib_timezone

        timelib.timelib_unixtime2local(now, int(time.time()))
        timelib.timelib_fill_holes(this.timelib_time, now, timelib.TIMELIB_NO_CLONE)
        timelib.timelib_update_ts(this.timelib_time, timelib_timezone)
        timelib.timelib_time_dtor(now)

        this.timelib_time.c_have_relative = rffi.cast(
            timelib.timelib_time.TO.c_have_relative, 1
        )

    this.w_datetimezone = W_DateTimeZone(k_DateTimeZone, [])

    this.w_datetimezone.timezone_info = TimeZoneWrapper(
        timelib_timezone, zone_type, timezone_offset
    )

    return error
Beispiel #11
0
def set_timezone(interp, this, w_datetimezone):
    this.w_datetimezone = w_datetimezone

    timelib.timelib_set_timezone(
        this.timelib_time, this.w_datetimezone.timezone_info.timelib_timezone)
    timelib.timelib_unixtime2local(this.timelib_time, this.timelib_time.c_sse)

    return this
Beispiel #12
0
def date_timezone_set(interp, w_date, w_datetimezone):
    w_date.timezone = w_datetimezone

    timelib.timelib_set_timezone(w_date.timelib_time,
                                 w_date.timezone.timelib_timezone)
    timelib.timelib_unixtime2local(w_date.timelib_time,
                                   w_date.timelib_time.c_sse)

    return w_date
Beispiel #13
0
def date_timezone_set(interp, w_date, w_datetimezone):
    w_date.timezone = w_datetimezone

    timelib.timelib_set_timezone(
        w_date.timelib_time, w_date.timezone.timelib_timezone)
    timelib.timelib_unixtime2local(
        w_date.timelib_time, w_date.timelib_time.c_sse)

    return w_date
Beispiel #14
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 #15
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 #16
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 #17
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 #18
0
def date_sun_info(interp, time, latitude, longitude):

    space = interp.space

    timelib_time = timelib.timelib_time_ctor()
    timelib_timezone = interp.get_default_timezone("date").timelib_timezone

    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, time)

    timelib_time_2 = timelib.timelib_time_ctor()

    dummy = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    ddummy = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_rise = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_set = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_transit = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO,
                              1,
                              flavor='raw')

    ret_val = []

    # Get sun up/down and transit
    rs = timelib.timelib_astro_rise_set_altitude(timelib_time, longitude,
                                                 latitude, -35.0 / 60, 1,
                                                 ddummy, ddummy, c_rise, c_set,
                                                 c_transit)

    if rs == -1:
        ret_val.append((space.wrap("sunrise"), space.w_False))
        ret_val.append((space.wrap("sunset"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("sunrise"), space.w_True))
        ret_val.append((space.wrap("sunset"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append(
            (space.wrap("sunrise"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append(
            (space.wrap("sunset"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))

    timelib_time_2.c_sse = c_transit[0]
    ret_val.append(
        (space.wrap("transit"),
         space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))

    # Get civil twilight
    rs = timelib.timelib_astro_rise_set_altitude(timelib_time, longitude,
                                                 latitude, -6.0, 0, ddummy,
                                                 ddummy, c_rise, c_set,
                                                 c_transit)

    if rs == -1:
        ret_val.append((space.wrap("civil_twilight_begin"), space.w_False))
        ret_val.append((space.wrap("civil_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("civil_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("civil_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append(
            (space.wrap("civil_twilight_begin"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append(
            (space.wrap("civil_twilight_end"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))

    # Get nautical twilight
    rs = timelib.timelib_astro_rise_set_altitude(timelib_time, longitude,
                                                 latitude, -12.0, 0, ddummy,
                                                 ddummy, c_rise, c_set,
                                                 c_transit)

    if rs == -1:
        ret_val.append((space.wrap("nautical_twilight_begin"), space.w_False))
        ret_val.append((space.wrap("nautical_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("nautical_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("nautical_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append(
            (space.wrap("nautical_twilight_begin"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append(
            (space.wrap("nautical_twilight_end"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))

    # Get astronomical twilight
    rs = timelib.timelib_astro_rise_set_altitude(timelib_time, longitude,
                                                 latitude, -18.0, 0, ddummy,
                                                 ddummy, c_rise, c_set,
                                                 c_transit)

    if rs == -1:
        ret_val.append(
            (space.wrap("astronomical_twilight_begin"), space.w_False))
        ret_val.append(
            (space.wrap("astronomical_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append(
            (space.wrap("astronomical_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("astronomical_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append(
            (space.wrap("astronomical_twilight_begin"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append(
            (space.wrap("astronomical_twilight_end"),
             space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))))

    timelib.timelib_time_dtor(timelib_time)
    timelib.timelib_time_dtor(timelib_time_2)

    return space.new_array_from_pairs(ret_val)
Beispiel #19
0
def set_timestamp(interp, this, timestamp):

    timelib.timelib_unixtime2local(this.timelib_time, timestamp)
    timelib.timelib_update_ts(this.timelib_time, lltype.nullptr(timelib.timelib_tzinfo.TO))

    return this
Beispiel #20
0
def date_sun_info(interp, time, latitude, longitude):

    space = interp.space

    timelib_time = timelib.timelib_time_ctor()
    timelib_timezone = interp.get_default_timezone("date").timelib_timezone

    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, time)

    timelib_time_2 = timelib.timelib_time_ctor()

    dummy = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    ddummy = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_rise = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_set = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_transit = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')

    ret_val = []

    # Get sun up/down and transit
    rs = timelib.timelib_astro_rise_set_altitude(
        timelib_time,
        longitude, latitude,
        -35.0/60, 1,
        ddummy, ddummy, c_rise, c_set, c_transit
    )

    if rs == -1:
        ret_val.append((space.wrap("sunrise"), space.w_False))
        ret_val.append((space.wrap("sunset"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("sunrise"), space.w_True))
        ret_val.append((space.wrap("sunset"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append((
            space.wrap("sunrise"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append((
            space.wrap("sunset"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))

    timelib_time_2.c_sse = c_transit[0]
    ret_val.append((
            space.wrap("transit"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))

    # Get civil twilight
    rs = timelib.timelib_astro_rise_set_altitude(
        timelib_time,
        longitude, latitude,
        -6.0, 0,
        ddummy, ddummy, c_rise, c_set, c_transit
    )

    if rs == -1:
        ret_val.append((space.wrap("civil_twilight_begin"), space.w_False))
        ret_val.append((space.wrap("civil_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("civil_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("civil_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append((
            space.wrap("civil_twilight_begin"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append((
            space.wrap("civil_twilight_end"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))


    # Get nautical twilight
    rs = timelib.timelib_astro_rise_set_altitude(
        timelib_time,
        longitude, latitude,
        -12.0, 0,
        ddummy, ddummy, c_rise, c_set, c_transit
    )

    if rs == -1:
        ret_val.append((space.wrap("nautical_twilight_begin"), space.w_False))
        ret_val.append((space.wrap("nautical_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("nautical_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("nautical_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append((
            space.wrap("nautical_twilight_begin"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append((
            space.wrap("nautical_twilight_end"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))

    # Get astronomical twilight
    rs = timelib.timelib_astro_rise_set_altitude(
        timelib_time,
        longitude, latitude,
        -18.0, 0,
        ddummy, ddummy, c_rise, c_set, c_transit
    )

    if rs == -1:
        ret_val.append((space.wrap("astronomical_twilight_begin"), space.w_False))
        ret_val.append((space.wrap("astronomical_twilight_end"), space.w_False))
    elif rs == 1:
        ret_val.append((space.wrap("astronomical_twilight_begin"), space.w_True))
        ret_val.append((space.wrap("astronomical_twilight_end"), space.w_True))
    else:
        timelib_time_2.c_sse = c_rise[0]
        ret_val.append((
            space.wrap("astronomical_twilight_begin"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))
        timelib_time_2.c_sse = c_set[0]
        ret_val.append((
            space.wrap("astronomical_twilight_end"),
            space.wrap(timelib.timelib_date_to_int(timelib_time_2, dummy))
        ))

    timelib.timelib_time_dtor(timelib_time)
    timelib.timelib_time_dtor(timelib_time_2)

    return space.new_array_from_pairs(ret_val)
Beispiel #21
0
def date_sunrise_sunset(func_name, interp, num_args, timestamp, return_format,
                        latitude, longitude, zenith, gmt_offset):

    sunrise = False
    if func_name == "date_sunrise":
        sunrise = True

    if return_format not in [
            SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, SUNFUNCS_RET_DOUBLE
    ]:
        interp.space.ec.warn(
            "%s(): Wrong return format given, pick one of "
            "SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE"
            % func_name)
        return interp.space.w_False

    altitude = 90 - zenith

    timelib_time = timelib.timelib_time_ctor()
    timelib_timezone = interp.get_default_timezone("date").timelib_timezone
    timelib_time.c_tz_info = timelib_timezone
    timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID

    timelib.timelib_unixtime2local(timelib_time, timestamp)

    c_h_rise = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_h_set = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw')
    c_rise = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_set = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw')
    c_transit = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO,
                              1,
                              flavor='raw')

    rs = timelib.timelib_astro_rise_set_altitude(timelib_time, longitude,
                                                 latitude, altitude, 1,
                                                 c_h_rise, c_h_set, c_rise,
                                                 c_set, c_transit)

    if num_args <= 5:
        gmt_offset = float(
            timelib.timelib_get_current_offset(timelib_time) / 3600)

    timelib.timelib_time_dtor(timelib_time)

    if rs != 0:
        return interp.space.w_False

    if return_format == 0:
        return interp.space.wrap(c_rise[0] if sunrise else c_set[0])

    N = (c_h_rise[0] if sunrise else c_h_set[0]) + gmt_offset

    if N > 24 or N < 0:
        N -= math.floor(N / 24) * 24

    if return_format == 1:
        return interp.space.wrap(
            "%s:%s" % (timelib.format_to(2, int(math.floor(N))),
                       timelib.format_to(2, int(math.floor(60 *
                                                           (N - int(N)))))))

    elif return_format == 2:
        return interp.space.wrap(N)
Beispiel #22
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