示例#1
0
 def __new__(cls, *args, **kwargs):
     if len(args) == 1 and not kwargs and isinstance(args[0], timedelta):
         td = args[0]
         self = timedelta.__new__(cls, td.days, td.seconds, td.microseconds)
     else:
         self = timedelta.__new__(cls, *args, **kwargs)
     return self
示例#2
0
 def __new__(cls, *a, **kw):
     format_args = kw.pop('format_args', {})
     if len(a) == 1 and not kw and isinstance(a[0], timedelta):
         r = timedelta.__new__(cls, a[0].days, a[0].seconds, a[0].microseconds)
     else:
         r = timedelta.__new__(cls, *a, **kw)
     r.format_args = format_args
     return r
示例#3
0
    def __new__(cls,
                days=0,
                seconds=0,
                microseconds=0,
                milliseconds=0,
                minutes=0,
                hours=0,
                weeks=0):
        self = timedelta.__new__(cls, days, seconds, microseconds,
                                 milliseconds, minutes, hours, weeks)

        # We need to compute the total_seconds() value
        # on a native timedelta object
        delta = timedelta(days, seconds, microseconds, milliseconds, minutes,
                          hours, weeks)

        # Intuitive normalization
        self._total = delta.total_seconds()
        total = abs(self._total)

        self._microseconds = round(total % 1 * 1e6)
        self._seconds = int(total) % SECONDS_PER_DAY
        self._days = int(total) // SECONDS_PER_DAY

        return self
示例#4
0
    def __new__(cls, days=0, seconds=0, microseconds=0,
                milliseconds=0, minutes=0, hours=0, weeks=0, years=0, months=0):
        if not isinstance(years, int) or not isinstance(months, int):
            raise ValueError('Float year and months are not supported')

        self = timedelta.__new__(
            cls, days, seconds, microseconds,
            milliseconds, minutes, hours, weeks
        )

        # Intuitive normalization
        total = self.total_seconds()

        m = 1
        if total < 0:
            m = -1

        self._microseconds = round(total % m * 1e6)
        self._seconds = abs(int(total)) % SECONDS_PER_DAY * m

        _days = abs(int(total)) // SECONDS_PER_DAY * m
        self._days = _days + (years * 365 + months * 30)
        self._remaining_days = abs(_days) % 7 * m
        self._weeks = abs(_days) // 7 * m
        self._months = months
        self._years = years

        return self
示例#5
0
    def __new__(cls, desc):
        """Since we derive from timedelta which is special, we must use 
        __new__.
        """
        if isinstance(desc, timedelta):
            kwargs = { 
                'days': desc.days
                , 'seconds': desc.seconds
                , 'microseconds': desc.microseconds
            }
        elif desc == 'now':
            kwargs = {}
        else:
            parts = desc.split(' ')
            ago = False
            if parts[-1] == 'ago':
                parts = parts[:-1]
                ago = True
            kwargs = {}
            for i in range(0, len(parts), 2):
                qty = float(parts[i])
                if ago:
                    qty = -qty
                unit = parts[i + 1]
                if unit[-1] == 's':
                    unit = unit[:-1]
                unit = unit + "s"
                kwargs[unit] = qty

        instance = timedelta.__new__(TimeInterval, **kwargs)
        return instance
示例#6
0
    def __new__(cls, days=0, seconds=0, microseconds=0,
                milliseconds=0, minutes=0, hours=0,
                weeks=0, years=0, months=0):
        if not isinstance(years, int) or not isinstance(months, int):
            raise ValueError('Float year and months are not supported')

        self = timedelta.__new__(
            cls, days, seconds, microseconds,
            milliseconds, minutes, hours, weeks
        )

        # We need to compute the total_seconds() value
        # on a native timedelta object
        delta = timedelta(
            days, seconds, microseconds,
            milliseconds, minutes, hours, weeks
        )

        # Intuitive normalization
        self._total = delta.total_seconds()
        total = abs(self._total)

        self._microseconds = round(total % 1 * 1e6)
        self._seconds = int(total) % SECONDS_PER_DAY

        days = int(total) // SECONDS_PER_DAY
        self._days = abs(days + years * 365 + months * 30)
        self._remaining_days = days % 7
        self._weeks = days // 7
        self._months = abs(months)
        self._years = abs(years)

        return self
示例#7
0
文件: __init__.py 项目: bashmak/djing
 def __new__(cls, tm):
     if isinstance(tm, timedelta):
         return timedelta.__new__(
             cls,
             days=tm.days,
             seconds=tm.seconds,
             microseconds=tm.microseconds
         )
示例#8
0
 def new_object(totaled_days):
     td_cls = cls
     if td_cls is None:
         td_cls = timedelta
     return timedelta.__new__(td_cls,
                              days=totaled_days,
                              seconds=seconds,
                              microseconds=microseconds,
                              milliseconds=milliseconds,
                              minutes=minutes,
                              hours=hours,
                              weeks=weeks)
示例#9
0
 def __new__(cls, *args, **kwargs):
     """ A convenience wrapper for datetime.timedelta that handles months and years.
     """
     # Time.years
     # Time.months
     # Time.days
     # Time.seconds
     # Time.microseconds
     y = kwargs.pop("years", 0)
     m = kwargs.pop("months", 0)
     t = timedelta.__new__(cls, *args, **kwargs)
     setattr(t, "years", y)
     setattr(t, "months", m)
     return t
示例#10
0
    def __new__(cls,
                days=0,
                seconds=0,
                microseconds=0,
                milliseconds=0,
                minutes=0,
                hours=0,
                weeks=0):
        self = timedelta.__new__(cls, days, seconds, microseconds,
                                 milliseconds, minutes, hours, weeks)

        # Intuitive normalization
        total = abs(self.total_seconds())

        self._microseconds = round(total % 1 * 1e6)
        self._seconds = int(total) % SECONDS_PER_DAY
        self._days = int(total) // SECONDS_PER_DAY

        return self
示例#11
0
    def __new__(cls, days=0, seconds=0, microseconds=0,
                milliseconds=0, minutes=0, hours=0, weeks=0):
        self = timedelta.__new__(
            cls, days, seconds, microseconds,
            milliseconds, minutes, hours, weeks
        )

        # Intuitive normalization
        total = self.total_seconds()

        m = 1
        if total < 0:
            m = -1

        self._microseconds = abs(round(total % 1 * 1e6)) * m
        self._seconds = abs(int(total)) % SECONDS_PER_DAY * m
        self._days = abs(int(total)) // SECONDS_PER_DAY * m

        return self
示例#12
0
    def __new__(
        cls,
        days=0,
        seconds=0,
        microseconds=0,
        milliseconds=0,
        minutes=0,
        hours=0,
        weeks=0,
        years=0,
        months=0,
    ):
        if not isinstance(years, int) or not isinstance(months, int):
            raise ValueError("Float year and months are not supported")

        self = timedelta.__new__(
            cls, days, seconds, microseconds, milliseconds, minutes, hours, weeks
        )

        # We need to compute the total_seconds() value
        # on a native timedelta object
        delta = timedelta(
            days, seconds, microseconds, milliseconds, minutes, hours, weeks
        )

        # Intuitive normalization
        self._total = delta.total_seconds()
        total = abs(self._total)

        self._microseconds = round(total % 1 * 1e6)
        self._seconds = int(total) % SECONDS_PER_DAY

        days = int(total) // SECONDS_PER_DAY
        self._days = abs(days + years * 365 + months * 30)
        self._remaining_days = days % 7
        self._weeks = days // 7
        self._months = abs(months)
        self._years = abs(years)

        return self
示例#13
0
    def __new__(
        cls,
        days=0,
        seconds=0,
        microseconds=0,
        milliseconds=0,
        minutes=0,
        hours=0,
        weeks=0,
        years=0,
        months=0,
    ):
        if not isinstance(years, int) or not isinstance(months, int):
            raise ValueError("Float year and months are not supported")

        self = timedelta.__new__(
            cls, days, seconds, microseconds, milliseconds, minutes, hours, weeks
        )

        # Intuitive normalization
        total = self.total_seconds()

        m = 1
        if total < 0:
            m = -1

        self._microseconds = round(total % m * 1e6)
        self._seconds = abs(int(total)) % SECONDS_PER_DAY * m

        _days = abs(int(total)) // SECONDS_PER_DAY * m
        self._days = _days + (years * 365 + months * 30)
        self._remaining_days = abs(_days) % 7 * m
        self._weeks = abs(_days) // 7 * m
        self._months = months
        self._years = years

        return self
示例#14
0
 def __new__(*args, **kwargs):  # pylint: disable=no-method-argument
     if len(args) == 2 and isinstance(args[1], (timedelta, np.timedelta64)):
         return timedelta.__new__(args[0], args[1].days, args[1].seconds,
                                  args[1].microseconds)
     else:
         return timedelta.__new__(*args, **kwargs)
示例#15
0
 def __new__(cls, tm):
     if isinstance(tm, timedelta):
         return timedelta.__new__(cls,
                                  days=tm.days,
                                  seconds=tm.seconds,
                                  microseconds=tm.microseconds)
示例#16
0
 def __new__(cls, *a, **kw):
     if len(a) == 1 and not kw and isinstance(a[0], timedelta):
         return timedelta.__new__(cls, a[0].days, a[0].seconds,
                                  a[0].microseconds)
     return timedelta.__new__(cls, *a, **kw)
示例#17
0
 def __new__(cls, *a, **kw):
     if len(a) == 1 and not kw and isinstance(a[0], timedelta):
         return timedelta.__new__(cls, a[0].days, a[0].seconds, a[0].microseconds)
     return timedelta.__new__(cls, *a, **kw)
示例#18
0
文件: parser.py 项目: danae/iffgraph
 def __new__(cls, hours, minutes):
     return timedelta.__new__(cls, hours=hours, minutes=minutes)
示例#19
0
 def __new__(*args, **kwargs):  # pylint: disable=no-method-argument
     if len(args) == 2 and isinstance(args[1], (timedelta, np.timedelta64)):
         return timedelta.__new__(args[0], args[1].days, args[1].seconds,
                                  args[1].microseconds)
     else:
         return timedelta.__new__(*args, **kwargs)
示例#20
0
 def __new__(cls, time_delta: timedelta, *_, **__):
     if time_delta:
         return timedelta.__new__(cls,
                                  days=time_delta.days,
                                  seconds=time_delta.seconds)
     return timedelta.__new__(cls)
示例#21
0
 def __new__(cls, minutes=0, seconds=0):
     seconds = minutes * 60 + seconds
     seconds = min(90 * 60, seconds)
     seconds = max(0, seconds)
     self = timedelta.__new__(cls, seconds=seconds)
     return self