예제 #1
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)
예제 #2
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
예제 #3
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
예제 #4
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)
예제 #5
0
def format(interp, this, format):

    y = this.time_diff.c_y
    m = this.time_diff.c_m
    d = this.time_diff.c_d
    h = this.time_diff.c_h
    i = this.time_diff.c_i
    s = this.time_diff.c_s

    index = 0

    results = []

    while index < len(format):
        c = format[index]
        if c == '%':

            index += 1
            next_c = format[index]

            if next_c == 'Y':
                results.append(timelib.format_to(2, y))
            elif next_c == 'y':
                results.append("%d" % y)
            elif next_c == 'M':
                results.append(timelib.format_to(2, m))
            elif next_c == 'm':
                results.append("%d" % m)
            elif next_c == 'D':
                results.append(timelib.format_to(2, d))
            elif next_c == 'd':
                results.append("%d" % d)
            elif next_c == 'H':
                results.append(timelib.format_to(2, h))
            elif next_c == 'h':
                results.append("%d" % h)
            elif next_c == 'I':
                results.append(timelib.format_to(2, i))
            elif next_c == 'i':
                results.append("%d" % i)
            elif next_c == 'S':
                results.append(timelib.format_to(2, s))
            elif next_c == 's':
                results.append("%d" % s)
            elif next_c == 'a':
                if this.time_diff.c_d != -99999:
                    results.append("%d" % this.time_diff.c_days)
                else:
                    results.append("(unknown)")
            elif next_c == 'r':
                results.append("-" if int(this.time_diff.c_invert) else "")
            elif next_c == 'R':
                results.append("-" if int(this.time_diff.c_invert) else "+")
            elif next_c == '%':
                results.append('%')
            else:
                results.append("%%%s" % next_c)
        else:
             results.append(c)

        index += 1

    return interp.space.wrap("".join(results))