Namespace System


Class TimeSpan
	m_Time As Int64
Public
	Static MaxValue = 3162240000000000000 As Int64
	Static MinValue = -3162240000000000000 As Int64
	Static TicksPerDay = 864000000000 As Int64
	Static TicksPerHour = 36000000000 As Int64
	Static TicksPerMinute = 600000000 As Int64
	Static TicksPerSecond = 10000000 As Int64
	Static TicksPerMillisecond = 10000 As Int64
	Static Zero = 0 As Int64

	Sub TimeSpan(ticks As Int64)
		If (ticks > MaxValue) Or (ticks < MinValue) Then
			'ArgumentOutOfRangeException
			debug
			Exit Sub
		End If
		m_Time = ticks
	End Sub

	Sub TimeSpan(ts As TimeSpan)
		m_Time = ts.m_Time
	End Sub

	Sub ~TimeSpan()
	End Sub

	Function Operator+ (ts As TimeSpan) As TimeSpan
		Return FromTicks(m_Time + ts.m_Time)
	End Function

	Function Operator- (ts As TimeSpan) As TimeSpan
		Return FromTicks(m_Time - ts.m_Time)
	End Function

	Function Operator* (value As Long) As TimeSpan
		Return FromTicks(m_Time * value)
	End Function

	Function Operator/ (value As Long) As TimeSpan
		Return FromTicks(m_Time \ value)
	End Function

	Function Operator== (ts As TimeSpan) As Boolean
		Return Equals(ts)
	End Function

	Function Operator<> (ts As TimeSpan) As Boolean
		Return Not Equals(ts)
	End Function

	Function Operator> (ts As TimeSpan) As Boolean
		If CompareTo(ts) > 0 Then
			Return True
		Else
			Return False
		End If
	End Function

	Function Operator< (ts As TimeSpan) As Boolean
		If CompareTo(ts) < 0 Then
			Return True
		Else
			Return False
		End If
	End Function

	Function Operator>= (ts As TimeSpan) As Boolean
		If CompareTo(ts) => 0 Then
			Return True
		Else
			Return False
		End If
	End Function

	Function Operator<= (ts As TimeSpan) As Boolean
		If CompareTo(ts) <= 0 Then
			Return True
		Else
			Return False
		End If
	End Function
		
	'Public Properties
	Function Ticks() As Int64
		Return m_Time
	End Function

	Function Milliseconds() As Long
		Return (m_Time \ TicksPerMillisecond Mod 1000) As Long
	End Function

	Function Seconds() As Long
		Return (m_Time \ TicksPerSecond Mod 60) As Long
	End Function

	Function Minutes() As Long
		Return (m_Time \ TicksPerMinute Mod 60) As Long
	End Function

	Function Hours() As Long
		Return (m_Time \ TicksPerHour Mod 24) As Long
	End Function

	Function Days() As Long
		Return (m_Time \ TicksPerDay) As Long
	End Function

	Function TotalMilliseconds() As Double
		Return m_Time / TicksPerMillisecond
	End Function

	Function TotalSeconds() As Double
		Return m_Time / TicksPerSecond
	End Function

	Function TotalMinute() As Double
		Return m_Time / TicksPerMinute
	End Function

	Function TotalHours() As Double
		Return m_Time / TicksPerHour
	End Function

	Function TotalDays() As Double
		Return m_Time / TicksPerDay
	End Function

	Function Add(ts As TimeSpan) As TimeSpan
		Return FromTicks(m_Time + ts.m_Time)
	End Function

	Static Function Compare(ts1 As TimeSpan, ts2 As TimeSpan) As Long
		If ts1.m_Time < ts2.m_Time Then
			Return -1
		ElseIf ts1.m_Time = ts2.m_Time Then
			Return 0
		ElseIf ts1.m_Time > ts2.m_Time Then
			Return 1
		End If
	End Function

	Function CompareTo(ts As TimeSpan) As Long
		Return (m_Time - ts.m_Time) As Long
	End Function

	Function Duration() As TimeSpan
		Return FromTicks(System.Math.Abs(m_Time))
	End Function

	Function Equals(ts As TimeSpan) As Boolean
		Return Equals(This, ts)
	End Function

	Static Function Equals(ts1 As TimeSpan, ts2 As TimeSpan) As Boolean
		If ts1.m_Time = ts2.m_Time Then
			Return True
		Else
			Return False
		End If
	End Function
	
	Static Function FromTicks(ticks As Int64) As TimeSpan
		If (ticks > MaxValue) Or (ticks < MinValue) Then
			'ArgumentOutOfRangeException
			debug
			Exit Function
		End If
		Return New TimeSpan( ticks )
	End Function

	Static Function FromMilliseconds(value As Double) As TimeSpan
		Return FromTicks((value * TicksPerMillisecond) As Int64)
	End Function

	Static Function FromSeconds(value As Double) As TimeSpan
		Return FromTicks((value * TicksPerSecond)As Int64)
	End Function

	Static Function FromMinutes(value As Double) As TimeSpan
		Return FromTicks((value * TicksPerMinute) As Int64)
	End Function

	Static Function FromHours(value As Double) As TimeSpan
		Return FromTicks((value * TicksPerHour) As Int64)
	End Function

	Static Function FromDays(value As Double) As TimeSpan
		Return FromTicks((value * TicksPerDay) As Int64)
	End Function

	Function Negate() As TimeSpan
		Return FromTicks(m_Time * (-1))
	End Function

	Function Subtract(ts As TimeSpan) As TimeSpan
		Return FromTicks(m_Time - ts.m_Time)
	End Function
End Class


End Namespace
