-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueTerms.cs
152 lines (130 loc) · 4.46 KB
/
ValueTerms.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
namespace biscuit_net.Datalog;
public sealed record Set(List<Term> Values) : Constant
{
public bool Equals(Set? other)
{
if(other == null)
{
return false;
}
return Values.SequenceEqual(other.Values);
}
public override int GetHashCode()
{
var hashCode = new HashCode();
foreach(var v in Values)
{
hashCode.Add(v);
}
return hashCode.ToHashCode();
}
public override string ToString() => $"[{string.Join(", ", Values)}]";
}
public sealed record Bytes(byte[] Value) : Constant
{
public bool Equals(Bytes? other)
{
if(other == null)
{
return false;
}
return Value.SequenceEqual(other.Value);
}
public override int GetHashCode() => Value.GetHashCode();
}
public sealed record String(string Value) : Constant
{
public override string ToString() => Value;
}
public sealed record Boolean(bool Value) : Constant
{
public override string ToString() => Value.ToString();
public static bool operator &(Boolean one, Boolean two) => one.Value && two.Value;
public static bool operator |(Boolean one, Boolean two) => one.Value || two.Value;
public static bool operator !(Boolean value) => !value.Value;
public static bool operator true(Boolean value) => value.Value;
public static bool operator false(Boolean value) => value.Value;
}
public sealed record Integer(long Value) : Constant
{
public override string ToString() => Value.ToString();
public static bool operator <(Integer one, Integer two) => one.Value < two.Value;
public static bool operator >(Integer one, Integer two) => one.Value > two.Value;
public static bool operator <=(Integer one, Integer two) => one.Value <= two.Value;
public static bool operator >=(Integer one, Integer two) => one.Value >= two.Value;
}
public sealed record Date(ulong Value) : Constant
{
public override string ToString() => DateTime.ToLongDateString();
public DateTime DateTime => FromTAI64(Value);
public static bool operator <(Date one, Date two) => one.Value < two.Value;
public static bool operator >(Date one, Date two) => one.Value > two.Value;
public static bool operator <=(Date one, Date two) => one.Value <= two.Value;
public static bool operator >=(Date one, Date two) => one.Value >= two.Value;
public static DateTime FromTAI64(ulong timestamp)
{
//TODE make more robust
//taken from https://github.com/paulhammond/tai64/blob/main/tai64n.go
var unixTimeStamp = EpochTime(timestamp - (2^62));
// Unix timestamp is seconds past epoch
DateTime dateTime = new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
return dateTime;
}
public static ulong ToTAI64(DateTime dateTime)
{
var universal = dateTime.ToUniversalTime();
var unixTimeStamp = (ulong)universal.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
return unixTimeStamp + (2^62);
}
// EpochTime returns the time.Time at secs seconds and nsec nanoseconds since
// the beginning of January 1, 1970 TAI.
public static ulong EpochTime(ulong secs)
{
var offset = leapSeconds.Length + 10;
foreach(var leap in leapSeconds)
{
offset--;
if(secs > leap)
break;
}
return secs- (ulong)offset;
}
// This is a list of all leap seconds added since 1972, in TAI seconds since
// the unix epoch. It is derived from
// http://www.ietf.org/timezones/data/leap-seconds.list
// http://hpiers.obspm.fr/eop-pc/earthor/utc/UTC.html
// http://maia.usno.navy.mil/leapsec.html
static readonly ulong[] leapSeconds = new ulong[] {
// subtract 2208988800 to convert from NTP datetime to unix seconds
// then add number of previous leap seconds to get TAI-since-unix-epoch
1483228836,
1435708835,
1341100834,
1230768033,
1136073632,
915148831,
867715230,
820454429,
773020828,
741484827,
709948826,
662688025,
631152024,
567993623,
489024022,
425865621,
394329620,
362793619,
315532818,
283996817,
252460816,
220924815,
189302414,
157766413,
126230412,
94694411,
78796810,
63072009,
};
}