88 lines
2.3 KiB
Python
88 lines
2.3 KiB
Python
import clr
|
|
|
|
clr.AddReference('System')
|
|
from System import DateTime, TimeSpan, Convert
|
|
|
|
|
|
class Duration:
|
|
time_span: TimeSpan
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
if len(args) == 1:
|
|
if args[0] is Duration:
|
|
self.time_span = args[0]
|
|
else:
|
|
raise ValueError("Invalid arguments for Duration initialization")
|
|
else:
|
|
raise ValueError("Invalid arguments for Duration initialization")
|
|
|
|
@staticmethod
|
|
def from_seconds(second: int):
|
|
return Duration(TimeSpan.FromSeconds(second))
|
|
|
|
@staticmethod
|
|
def from_minutes(minute: int):
|
|
return Duration(TimeSpan.FromMinutes(minute))
|
|
|
|
@staticmethod
|
|
def from_hours(hour: int):
|
|
return Duration(TimeSpan.FromHours(hour))
|
|
|
|
def __lt__(self, other):
|
|
return self.time_span < other.time_span
|
|
|
|
def __le__(self, other):
|
|
return self.time_span <= other.time_span
|
|
|
|
def __eq__(self, other):
|
|
return self.time_span == other.time_span
|
|
|
|
def __ne__(self, other):
|
|
return self.time_span != other.time_span
|
|
|
|
def __gt__(self, other):
|
|
return self.time_span > other.time_span
|
|
|
|
def __ge__(self, other):
|
|
return self.time_span >= other.time_span
|
|
|
|
|
|
class Time:
|
|
struct_time: DateTime
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
if len(args) == 0:
|
|
self.struct_time = DateTime.Now
|
|
elif len(args) == 1:
|
|
print(type(args[0]))
|
|
if args[0] is str:
|
|
self.struct_time = Convert.ToDateTime(args[0])
|
|
else:
|
|
self.struct_time = args[0]
|
|
else:
|
|
raise ValueError("Invalid arguments for Time initialization")
|
|
|
|
def __lt__(self, other):
|
|
return self.struct_time < other.struct_time
|
|
|
|
def __le__(self, other):
|
|
return self.struct_time <= other.struct_time
|
|
|
|
def __eq__(self, other):
|
|
return self.struct_time == other.struct_time
|
|
|
|
def __ne__(self, other):
|
|
return self.struct_time != other.struct_time
|
|
|
|
def __gt__(self, other):
|
|
return self.struct_time > other.struct_time
|
|
|
|
def __ge__(self, other):
|
|
return self.struct_time >= other.struct_time
|
|
|
|
def __add__(self, other: Duration):
|
|
return Time(self.struct_time + other.time_span)
|
|
|
|
def __sub__(self, other: 'Time'):
|
|
return TimeSpan(self.struct_time - other.struct_time)
|