class_name Timespan
extends Node

var _millis: int

var _MillisPerSecond = 1000
var _MillisPerMinute = _MillisPerSecond * 60
var _MillisPerHour = _MillisPerMinute * 60
var _MillisPerDay = _MillisPerHour * 24


func _init(millis: int):
	_millis = millis


func Add(ts: Timespan) -> Timespan:
	return Timespan.new(self._millis + ts._millis)


func Subtract(ts: Timespan) -> Timespan:
	assert(self._millis >= ts._millis)
	return Timespan.new(self._millis - ts._millis)


func CompareTo(ts: Timespan) -> int:
	if self._millis > ts._millis: return 1
	if self._millis < ts._millis: return -1
	return 0


func Days() -> int:
	return _millis / _MillisPerDay


func Hours() -> int:
	return (_millis / _MillisPerHour) % 24


func Minutes() -> int:
	return (_millis / _MillisPerMinute) % 60


func Seconds() -> int:
	return (_millis / _MillisPerSecond) % 60


func Millis() -> int:
	return _millis % 1000


func TotalDays() -> float:
	return float(_millis) / _MillisPerDay


func TotalHours() -> float:
	return float(_millis) / _MillisPerHour


func TotalMinutes() -> float:
	return float(_millis) / _MillisPerMinute


func TotalSeconds() -> float:
	return float(_millis) / _MillisPerSecond


func TotalMillis() -> int:
	return _millis


static func FromTimestamps(t1: int, t2: int) -> Timespan:
	assert(t1 <= t2)
	return Timespan.new(t2 - t1)


static func Compare(ts1: Timespan, ts2: Timespan) -> int:
	if ts1._millis > ts2._millis: return 1
	if ts1._millis < ts2._millis: return -1
	return 0
