def method_sub(self, space, w_other): if isinstance(w_other, W_TimeObject): return space.newfloat(self.epoch_seconds - Coerce.float(space, w_other)) else: w_time = space.send(space.getclassfor(W_TimeObject), "allocate") w_time._set_epoch_seconds(self.epoch_seconds - Coerce.float(space, w_other)) w_time._set_offset(self.offset) return w_time
def method_sub(self, space, w_other): if isinstance(w_other, W_TimeObject): return space.newfloat( self.epoch_seconds - Coerce.float(space, w_other)) else: w_time = space.send(space.getclassfor(W_TimeObject), "allocate") w_time._set_epoch_seconds( self.epoch_seconds - Coerce.float(space, w_other)) w_time._set_offset(self.offset) return w_time
def method_at(self, space, w_time, w_microtime=None): if not (w_time.is_kind_of(space, space.w_numeric) or w_time.is_kind_of(space, space.getclassfor(W_TimeObject))): raise space.error(space.w_TypeError) if w_microtime is not None: microtime = Coerce.float(space, w_microtime) * 0.000001 else: microtime = 0.0 timestamp = Coerce.float(space, w_time) w_time = space.send(self, "new") w_time._set_epoch_seconds(timestamp + microtime) return w_time
def method_gm(self, space, args_w): if len(args_w) == 0: raise space.error( space.w_ArgumentError, "wrong number of arguments (given 0, expected 1..8)" ) elif len(args_w) == 10: # sec, min, hour, day, month, year, dummy, dummy, dummy, dummy sec = Coerce.int(space, args_w[0]) minute = Coerce.int(space, args_w[1]) hour = Coerce.int(space, args_w[2]) day = Coerce.int(space, args_w[3]) month = W_TimeObject.month_arg_to_month(space, args_w[4]) year = Coerce.int(space, args_w[5]) usecfrac = 0.0 else: month = day = 1 hour = minute = sec = 0 usecfrac = 0.0 year = Coerce.int(space, args_w[0]) if len(args_w) > 1: month = W_TimeObject.month_arg_to_month(space, args_w[1]) if len(args_w) > 2: day = Coerce.int(space, args_w[2]) if len(args_w) > 3: hour = Coerce.int(space, args_w[3]) if len(args_w) > 4: minute = Coerce.int(space, args_w[4]) if len(args_w) > 6: sec = Coerce.int(space, args_w[5]) usecfrac = Coerce.float(space, args_w[6]) / 1000000 if len(args_w) > 5: fsec = Coerce.float(space, args_w[5]) sec = int(math.floor(fsec)) usecfrac = fsec - sec if not (1 <= month < 12): raise space.error(space.w_ArgumentError, "mon out of range") if not (1 <= day < 31): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= hour < 24): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= minute < 60): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= sec < 60): raise space.error(space.w_ArgumentError, "argument out of range") w_time = space.send(space.getclassfor(W_TimeObject), "new") w_time._set_epoch_seconds( mktime(year, month, day, hour, minute, sec) + usecfrac) return w_time
def method_gm(self, space, args_w): if len(args_w) == 0: raise space.error( space.w_ArgumentError, "wrong number of arguments (given 0, expected 1..8)") elif len(args_w) == 10: # sec, min, hour, day, month, year, dummy, dummy, dummy, dummy sec = Coerce.int(space, args_w[0]) minute = Coerce.int(space, args_w[1]) hour = Coerce.int(space, args_w[2]) day = Coerce.int(space, args_w[3]) month = W_TimeObject.month_arg_to_month(space, args_w[4]) year = Coerce.int(space, args_w[5]) usecfrac = 0.0 else: month = day = 1 hour = minute = sec = 0 usecfrac = 0.0 year = Coerce.int(space, args_w[0]) if len(args_w) > 1: month = W_TimeObject.month_arg_to_month(space, args_w[1]) if len(args_w) > 2: day = Coerce.int(space, args_w[2]) if len(args_w) > 3: hour = Coerce.int(space, args_w[3]) if len(args_w) > 4: minute = Coerce.int(space, args_w[4]) if len(args_w) > 6: sec = Coerce.int(space, args_w[5]) usecfrac = Coerce.float(space, args_w[6]) / 1000000 if len(args_w) > 5: fsec = Coerce.float(space, args_w[5]) sec = int(math.floor(fsec)) usecfrac = fsec - sec if not (1 <= month < 12): raise space.error(space.w_ArgumentError, "mon out of range") if not (1 <= day < 31): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= hour < 24): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= minute < 60): raise space.error(space.w_ArgumentError, "argument out of range") if not (0 <= sec < 60): raise space.error(space.w_ArgumentError, "argument out of range") w_time = space.send(space.getclassfor(W_TimeObject), "new") w_time._set_epoch_seconds( mktime(year, month, day, hour, minute, sec) + usecfrac) return w_time
def method_plus(self, space, w_other): if isinstance(w_other, W_TimeObject): raise space.error(space.w_TypeError, "time + time?") w_time = space.send(space.getclassfor(W_TimeObject), "allocate") w_time._set_epoch_seconds(self.epoch_seconds + Coerce.float(space, w_other)) w_time._set_offset(self.offset) return w_time
def method_sleep(self, space, w_duration=None): if w_duration is None: raise space.error(space.w_NotImplementedError) elif space.is_kind_of(w_duration, space.w_string): raise space.error(space.w_TypeError, "can't convert String into time interval") start = time.time() time.sleep(Coerce.float(space, w_duration)) return space.newint(int(round_double(time.time() - start, 0)))
def method_at(self, space, w_time): if not (w_time.is_kind_of(space, space.w_numeric) or w_time.is_kind_of(space, space.getclassfor(W_TimeObject))): raise space.error(space.w_TypeError) timestamp = Coerce.float(space, w_time) w_time = space.send(self, "new") w_time._set_epoch_seconds(timestamp) return w_time
def wrapper(self, space, args_w): if len(args_w) == 2: w_value1, w_value2 = args_w else: # delegate and hope that the gateway will raise an # ArgumentError args = [Coerce.float(space, w_arg) for w_arg in args_w] return func(self, space, args) if space.is_kind_of(w_value1, space.w_numeric): if space.is_kind_of(w_value2, space.w_numeric): value1 = Coerce.float(space, w_value1) value2 = Coerce.int(space, w_value2) return func(self, space, value1, value2) else: raise_type_error(space, w_value2) else: raise_type_error(space, w_value1)
def method_plus(self, space, w_other): if isinstance(w_other, W_TimeObject): raise space.error(space.w_TypeError, "time + time?") w_time = space.send(space.getclassfor(W_TimeObject), "allocate") w_time._set_epoch_seconds( self.epoch_seconds + Coerce.float(space, w_other)) w_time._set_offset(self.offset) return w_time
def wrapper(self, space, args_w): f_args = [] for w_arg in args_w: if space.is_kind_of(w_arg, space.w_numeric): f_arg = Coerce.float(space, w_arg) f_args.append(f_arg) else: raise_type_error(space, w_arg) return func(self, space, *f_args)
def method_div(self, space, w_other): if space.is_kind_of(w_other, space.w_float): if space.float_w(w_other) == 0.0: self.raise_zero_division_error(space) else: w_float = space.send(space.newfloat(space.float_w(self)), "/", [w_other]) w_float = space.newfloat(math.floor(Coerce.float(space, w_float))) return space.send(w_float, "to_i") else: return self.divide(space, w_other)
def method_div(self, space, w_other): if space.is_kind_of(w_other, space.w_float): if space.float_w(w_other) == 0.0: self.raise_zero_division_error(space) else: w_float = space.send(space.newfloat(space.float_w(self)), "/", [w_other]) w_float = space.newfloat( math.floor(Coerce.float(space, w_float))) return space.send(w_float, "to_i") else: return self.divide(space, w_other)
def method_ceil(self, space): return space.newint(int(math.ceil(Coerce.float(space, self))))
def _rand_float(self, space, float): random = self.random.random() max = Coerce.float(space, float) if max <= 0: raise space.error(space.w_ArgumentError, "invalid argument") return space.newfloat(random * max)
def method_floor(self, space): return space.newint(int(math.floor(Coerce.float(space, self))))
def _rand_bignum(self, space, bignum): random = self.random.random() max = Coerce.float(space, bignum) if max <= 0: raise space.error(space.w_ArgumentError, "invalid argument") return space.newbigint_fromfloat(random * max)
def fmt_f(self, space, width): num = Coerce.float(space, self._next_item()) return self._fmt_num(space, formatd(num, "f", 6), width)
def fmt_f(self, space, width, precision, format_char): num = Coerce.float(space, self._next_item()) return self._fmt_num(space, formatd(num, "f", precision), width)
def method_round(self, space): return space.newint(int(round_away(Coerce.float(space, self))))