-
Notifications
You must be signed in to change notification settings - Fork 3
/
timestamp.py
65 lines (49 loc) · 1.78 KB
/
timestamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import re
from enum import Enum
class Unit(Enum):
SECOND = "s"
MILLISECOND = "ms"
MICROSECOND = "us"
NANOSECOND = "ns"
PICOSECOND = "ps"
FEMTOSECOND = "fs"
POWERS = {
Unit.SECOND: 1000000000000000,
Unit.MILLISECOND: 1000000000000,
Unit.MICROSECOND: 1000000000,
Unit.NANOSECOND: 1000000,
Unit.PICOSECOND: 1000,
Unit.FEMTOSECOND: 1,
}
class Timestamp:
def __init__(self, value: int, unit: Unit):
self.value = value
self.unit = unit
@classmethod
def from_string(cls, string: str):
match = re.match(r'\s*(?P<value>\d+)(?P<unit>\w+)', string)
if match:
return cls(int(match.group('value')), Unit(match.group('unit')))
else:
raise ValueError("Incorrect timestamp format")
def convert_to(self, unit: Unit) -> float:
power_from = POWERS[self.unit]
power_to = POWERS[unit]
div = power_from / power_to
return self.value * div
def __eq__(self, other):
return self.convert_to(Unit.FEMTOSECOND) == other.convert_to(Unit.FEMTOSECOND)
def __lt__(self, other):
return self.convert_to(Unit.FEMTOSECOND) < other.convert_to(Unit.FEMTOSECOND)
def __le__(self, other):
return self.convert_to(Unit.FEMTOSECOND) <= other.convert_to(Unit.FEMTOSECOND)
def __add__(self, other):
value = self.convert_to(Unit.FEMTOSECOND) + other.convert_to(Unit.FEMTOSECOND)
return Timestamp(value, Unit.FEMTOSECOND)
def __sub__(self, other):
value = self.convert_to(Unit.FEMTOSECOND) - other.convert_to(Unit.FEMTOSECOND)
return Timestamp(value, Unit.FEMTOSECOND)
def __mul__(self, number: int):
value = self.value * number
return Timestamp(value, self.unit)
t_0 = Timestamp(0, Unit.SECOND)