|
1 | | -# from redshift_connector.config import max_int4, max_int8, min_int4, min_int8 |
2 | | -# |
3 | | -# |
4 | | -# class Interval: |
5 | | -# """An Interval represents a measurement of time. In PostgreSQL, an |
6 | | -# interval is defined in the measure of months, days, and microseconds; as |
7 | | -# such, the interval type represents the same information. |
8 | | -# |
9 | | -# Note that values of the :attr:`microseconds`, :attr:`days` and |
10 | | -# :attr:`months` properties are independently measured and cannot be |
11 | | -# converted to each other. A month may be 28, 29, 30, or 31 days, and a day |
12 | | -# may occasionally be lengthened slightly by a leap second. |
13 | | -# |
14 | | -# .. attribute:: microseconds |
15 | | -# |
16 | | -# Measure of microseconds in the interval. |
17 | | -# |
18 | | -# The microseconds value is constrained to fit into a signed 64-bit |
19 | | -# integer. Any attempt to set a value too large or too small will result |
20 | | -# in an OverflowError being raised. |
21 | | -# |
22 | | -# .. attribute:: days |
23 | | -# |
24 | | -# Measure of days in the interval. |
25 | | -# |
26 | | -# The days value is constrained to fit into a signed 32-bit integer. |
27 | | -# Any attempt to set a value too large or too small will result in an |
28 | | -# OverflowError being raised. |
29 | | -# |
30 | | -# .. attribute:: months |
31 | | -# |
32 | | -# Measure of months in the interval. |
33 | | -# |
34 | | -# The months value is constrained to fit into a signed 32-bit integer. |
35 | | -# Any attempt to set a value too large or too small will result in an |
36 | | -# OverflowError being raised. |
37 | | -# """ |
38 | | -# |
39 | | -# def __init__(self: 'Interval', microseconds: int = 0, days: int = 0, months: int = 0) -> None: |
40 | | -# self.microseconds = microseconds |
41 | | -# self.days = days |
42 | | -# self.months = months |
43 | | -# |
44 | | -# def _setMicroseconds(self: 'Interval', value: int) -> None: |
45 | | -# if not isinstance(value, int): |
46 | | -# raise TypeError("microseconds must be an integer type") |
47 | | -# elif not (min_int8 < value < max_int8): |
48 | | -# raise OverflowError( |
49 | | -# "microseconds must be representable as a 64-bit integer") |
50 | | -# else: |
51 | | -# self._microseconds = value |
52 | | -# |
53 | | -# def _setDays(self: 'Interval', value: int) -> None: |
54 | | -# if not isinstance(value, int): |
55 | | -# raise TypeError("days must be an integer type") |
56 | | -# elif not (min_int4 < value < max_int4): |
57 | | -# raise OverflowError( |
58 | | -# "days must be representable as a 32-bit integer") |
59 | | -# else: |
60 | | -# self._days = value |
61 | | -# |
62 | | -# def _setMonths(self: 'Interval', value: int) -> None: |
63 | | -# if not isinstance(value, int): |
64 | | -# raise TypeError("months must be an integer type") |
65 | | -# elif not (min_int4 < value < max_int4): |
66 | | -# raise OverflowError( |
67 | | -# "months must be representable as a 32-bit integer") |
68 | | -# else: |
69 | | -# self._months = value |
70 | | -# |
71 | | -# microseconds = property(lambda self: self._microseconds, _setMicroseconds) |
72 | | -# days = property(lambda self: self._days, _setDays) |
73 | | -# months = property(lambda self: self._months, _setMonths) |
74 | | -# |
75 | | -# def __repr__(self: 'Interval') -> str: |
76 | | -# return "<Interval %s months %s days %s microseconds>" % ( |
77 | | -# self.months, self.days, self.microseconds) |
78 | | -# |
79 | | -# def __eq__(self: 'Interval', other: object) -> bool: |
80 | | -# return other is not None and isinstance(other, Interval) and \ |
81 | | -# self.months == other.months and self.days == other.days and \ |
82 | | -# self.microseconds == other.microseconds |
83 | | -# |
84 | | -# def __neq__(self: 'Interval', other: 'Interval') -> bool: |
85 | | -# return not self.__eq__(other) |
| 1 | +from redshift_connector.config import max_int4, max_int8, min_int4, min_int8 |
| 2 | + |
| 3 | + |
| 4 | +class Interval: |
| 5 | + """An Interval represents a measurement of time. In PostgreSQL, an |
| 6 | + interval is defined in the measure of months, days, and microseconds; as |
| 7 | + such, the interval type represents the same information. |
| 8 | +
|
| 9 | + Note that values of the :attr:`microseconds`, :attr:`days` and |
| 10 | + :attr:`months` properties are independently measured and cannot be |
| 11 | + converted to each other. A month may be 28, 29, 30, or 31 days, and a day |
| 12 | + may occasionally be lengthened slightly by a leap second. |
| 13 | +
|
| 14 | + .. attribute:: microseconds |
| 15 | +
|
| 16 | + Measure of microseconds in the interval. |
| 17 | +
|
| 18 | + The microseconds value is constrained to fit into a signed 64-bit |
| 19 | + integer. Any attempt to set a value too large or too small will result |
| 20 | + in an OverflowError being raised. |
| 21 | +
|
| 22 | + .. attribute:: days |
| 23 | +
|
| 24 | + Measure of days in the interval. |
| 25 | +
|
| 26 | + The days value is constrained to fit into a signed 32-bit integer. |
| 27 | + Any attempt to set a value too large or too small will result in an |
| 28 | + OverflowError being raised. |
| 29 | +
|
| 30 | + .. attribute:: months |
| 31 | +
|
| 32 | + Measure of months in the interval. |
| 33 | +
|
| 34 | + The months value is constrained to fit into a signed 32-bit integer. |
| 35 | + Any attempt to set a value too large or too small will result in an |
| 36 | + OverflowError being raised. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self: "Interval", microseconds: int = 0, days: int = 0, months: int = 0) -> None: |
| 40 | + self.microseconds = microseconds |
| 41 | + self.days = days |
| 42 | + self.months = months |
| 43 | + |
| 44 | + def _setMicroseconds(self: "Interval", value: int) -> None: |
| 45 | + if not isinstance(value, int): |
| 46 | + raise TypeError("microseconds must be an integer type") |
| 47 | + elif not (min_int8 < value < max_int8): |
| 48 | + raise OverflowError("microseconds must be representable as a 64-bit integer") |
| 49 | + else: |
| 50 | + self._microseconds = value |
| 51 | + |
| 52 | + def _setDays(self: "Interval", value: int) -> None: |
| 53 | + if not isinstance(value, int): |
| 54 | + raise TypeError("days must be an integer type") |
| 55 | + elif not (min_int4 < value < max_int4): |
| 56 | + raise OverflowError("days must be representable as a 32-bit integer") |
| 57 | + else: |
| 58 | + self._days = value |
| 59 | + |
| 60 | + def _setMonths(self: "Interval", value: int) -> None: |
| 61 | + if not isinstance(value, int): |
| 62 | + raise TypeError("months must be an integer type") |
| 63 | + elif not (min_int4 < value < max_int4): |
| 64 | + raise OverflowError("months must be representable as a 32-bit integer") |
| 65 | + else: |
| 66 | + self._months = value |
| 67 | + |
| 68 | + microseconds = property(lambda self: self._microseconds, _setMicroseconds) |
| 69 | + days = property(lambda self: self._days, _setDays) |
| 70 | + months = property(lambda self: self._months, _setMonths) |
| 71 | + |
| 72 | + def __repr__(self: "Interval") -> str: |
| 73 | + return "<Interval %s months %s days %s microseconds>" % (self.months, self.days, self.microseconds) |
| 74 | + |
| 75 | + def __eq__(self: "Interval", other: object) -> bool: |
| 76 | + return ( |
| 77 | + other is not None |
| 78 | + and isinstance(other, Interval) |
| 79 | + and self.months == other.months |
| 80 | + and self.days == other.days |
| 81 | + and self.microseconds == other.microseconds |
| 82 | + ) |
| 83 | + |
| 84 | + def __neq__(self: "Interval", other: "Interval") -> bool: |
| 85 | + return not self.__eq__(other) |
| 86 | + |
| 87 | + def total_seconds(self: "Interval") -> float: |
| 88 | + """Total seconds in the Interval, excluding month field.""" |
| 89 | + return ((self.days * 86400) * 10 ** 6 + self.microseconds) / 10 ** 6 |
0 commit comments