コード例 #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
ファイル: interval.py プロジェクト: regnarock/pendulum
    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
ファイル: duration.py プロジェクト: pombredanne/pendulum
    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
ファイル: duration.py プロジェクト: pombredanne/pendulum
    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
ファイル: npdatetime.py プロジェクト: KristoforMaynard/Viscid
 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
ファイル: __init__.py プロジェクト: SirPrikol/djing
 def __new__(cls, tm):
     if isinstance(tm, timedelta):
         return timedelta.__new__(cls,
                                  days=tm.days,
                                  seconds=tm.seconds,
                                  microseconds=tm.microseconds)
コード例 #16
0
ファイル: i18n.py プロジェクト: yorishpopov/liberapay.com
 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
ファイル: i18n.py プロジェクト: devmiyax/liberapay.com
 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
ファイル: npdatetime.py プロジェクト: KristoforMaynard/Viscid
 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
ファイル: time_manager.py プロジェクト: psyinfra/HTCAnalyze
 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