forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimespan.cs
More file actions
118 lines (103 loc) · 6.38 KB
/
Timespan.cs
File metadata and controls
118 lines (103 loc) · 6.38 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace kOS
{
public class TimeSpan : SpecialValue
{
System.TimeSpan span;
public TimeSpan(double unixStyleTime)
{
span = System.TimeSpan.FromSeconds(unixStyleTime);
}
public int Year()
{
return (int)Math.Floor(span.Days / 365.0) + 1;
}
public int Day()
{
return (span.Days % 365) + 1;
}
public double ToUnixStyleTime()
{
return span.TotalSeconds;
}
public override object GetSuffix(string suffixName)
{
if (suffixName == "YEAR") return Year();
if (suffixName == "DAY") return Day();
if (suffixName == "HOUR") return span.Hours;
if (suffixName == "MINUTE") return span.Minutes;
if (suffixName == "SECOND") return span.Seconds;
if (suffixName == "SECONDS") return span.TotalSeconds;
if (suffixName == "CLOCK") return span.Hours + ":" + String.Format(span.Minutes.ToString("00") + ":" + String.Format(span.Seconds.ToString("00")));
if (suffixName == "CALENDAR") return "Year " + Year() + ", day " + Day();
return base.GetSuffix(suffixName);
}
public static TimeSpan operator +(TimeSpan a, TimeSpan b) { return new TimeSpan(a.ToUnixStyleTime() + b.ToUnixStyleTime()); }
public static TimeSpan operator -(TimeSpan a, TimeSpan b) { return new TimeSpan(a.ToUnixStyleTime() - b.ToUnixStyleTime()); }
public static TimeSpan operator +(TimeSpan a, double b) { return new TimeSpan(a.ToUnixStyleTime() + b); }
public static TimeSpan operator -(TimeSpan a, double b) { return new TimeSpan(a.ToUnixStyleTime() - b); }
public static TimeSpan operator *(TimeSpan a, double b) { return new TimeSpan(a.ToUnixStyleTime() * b); }
public static TimeSpan operator /(TimeSpan a, double b) { return new TimeSpan(a.ToUnixStyleTime() / b); }
public static TimeSpan operator -(double b, TimeSpan a) { return new TimeSpan(b - a.ToUnixStyleTime()); }
public static TimeSpan operator *(double b, TimeSpan a) { return new TimeSpan(b * a.ToUnixStyleTime()); }
public static TimeSpan operator /(double b, TimeSpan a) { return new TimeSpan(b / a.ToUnixStyleTime()); }
public static TimeSpan operator /(TimeSpan b, TimeSpan a) { return new TimeSpan(b.ToUnixStyleTime() / a.ToUnixStyleTime()); }
public static bool operator >(TimeSpan a, TimeSpan b) { return a.ToUnixStyleTime() > b.ToUnixStyleTime(); }
public static bool operator <(TimeSpan a, TimeSpan b) { return a.ToUnixStyleTime() < b.ToUnixStyleTime(); }
public static bool operator >=(TimeSpan a, TimeSpan b) { return a.ToUnixStyleTime() >= b.ToUnixStyleTime(); }
public static bool operator <=(TimeSpan a, TimeSpan b) { return a.ToUnixStyleTime() <= b.ToUnixStyleTime(); }
public static bool operator >(TimeSpan a, double b) { return a.ToUnixStyleTime() > b; }
public static bool operator <(TimeSpan a, double b) { return a.ToUnixStyleTime() < b; }
public static bool operator >=(TimeSpan a, double b) { return a.ToUnixStyleTime() >= b; }
public static bool operator <=(TimeSpan a, double b) { return a.ToUnixStyleTime() <= b; }
public static bool operator >(double a, TimeSpan b) { return a > b.ToUnixStyleTime(); }
public static bool operator <(double a, TimeSpan b) { return a < b.ToUnixStyleTime(); }
public static bool operator >=(double a, TimeSpan b) { return a >= b.ToUnixStyleTime(); }
public static bool operator <=(double a, TimeSpan b) { return a <= b.ToUnixStyleTime(); }
public override object TryOperation(string op, object other, bool reverseOrder)
{
// Order shouldn't matter here
if (other is TimeSpan && op == "+") return this + (TimeSpan)other;
if (other is double && op == "+") return this + (double)other;
if (other is double && op == "*") return this * (double)other; // Order would matter here if this were matrices
if (!reverseOrder)
{
if (other is TimeSpan && op == "-") return this - (TimeSpan)other;
if (other is TimeSpan && op == "/") return this / (TimeSpan)other;
if (other is double && op == "-") return this - (double)other;
if (other is double && op == "/") return this / (double)other;
if (other is TimeSpan && op == ">") return this > (TimeSpan)other;
if (other is TimeSpan && op == "<") return this < (TimeSpan)other;
if (other is TimeSpan && op == ">=") return this >= (TimeSpan)other;
if (other is TimeSpan && op == "<=") return this <= (TimeSpan)other;
if (other is double && op == ">") return this > (double)other;
if (other is double && op == "<") return this < (double)other;
if (other is double && op == ">=") return this >= (double)other;
if (other is double && op == "<=") return this <= (double)other;
}
else
{
if (other is TimeSpan && op == "-") return (TimeSpan)other - this;
if (other is TimeSpan && op == "/") return (TimeSpan)other / this;
if (other is double && op == "-") return (double)other - this;
if (other is double && op == "/") return (double)other / this; // Can't imagine why the heck you'd want to do this but here it is
if (other is TimeSpan && op == ">") return this < (TimeSpan)other;
if (other is TimeSpan && op == "<") return this > (TimeSpan)other;
if (other is TimeSpan && op == ">=") return (TimeSpan)other >= this;
if (other is TimeSpan && op == "<=") return (TimeSpan)other <= this;
if (other is double && op == ">") return (double)other > this;
if (other is double && op == "<") return (double)other < this;
if (other is double && op == ">=") return (double)other >= this;
if (other is double && op == "<=") return (double)other <= this;
}
return base.TryOperation(op, other, reverseOrder);
}
public override string ToString()
{
return Math.Floor(span.TotalSeconds).ToString("0");
}
}
}