-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEphemeralTypeBase.cs
More file actions
101 lines (89 loc) · 3.35 KB
/
EphemeralTypeBase.cs
File metadata and controls
101 lines (89 loc) · 3.35 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
using System;
namespace H.Necessaire
{
public abstract class EphemeralTypeBase : IEphemeralType
{
#region Construct
static readonly TimeSpan? defaultValidity = TimeSpan.FromHours(24);
TimeSpan? validFor;
DateTime? expiresAt;
DateTime validFrom;
DateTime createdAt;
DateTime asOf;
public EphemeralTypeBase()
{
DateTime utcNow = DateTime.UtcNow;
validFor = defaultValidity;
expiresAt = utcNow + defaultValidity;
validFrom = utcNow;
createdAt = utcNow;
asOf = utcNow;
}
#endregion
public DateTime CreatedAt { get => createdAt; set => createdAt = value.EnsureUtc(); }
public DateTime AsOf { get => asOf; set => asOf = value.EnsureUtc(); }
public long AsOfTicks { get => AsOf.Ticks; set => AsOf = new DateTime(value, DateTimeKind.Utc); }
public DateTime ValidFrom
{
get => validFrom;
set
{
validFrom = value.EnsureUtc();
ValidFor = ValidFor;
}
}
public long ValidFromTicks { get => ValidFrom.Ticks; set => ValidFrom = new DateTime(value, DateTimeKind.Utc); }
public TimeSpan? ValidFor
{
get
{
return validFor;
}
set
{
validFor = value;
if (validFor == null && ExpiresAtTicks != null)
ExpiresAtTicks = null;
else if (validFor != null && ValidFromTicks + ValidForTicks != ExpiresAtTicks)
ExpiresAtTicks = ValidFromTicks + ValidForTicks;
}
}
public long? ValidForTicks { get => ValidFor?.Ticks; set => ValidFor = value == null ? null : new TimeSpan?(new TimeSpan(value.Value)); }
public DateTime? ExpiresAt
{
get
{
return expiresAt;
}
set
{
expiresAt = value?.EnsureUtc();
if (expiresAt == null && ValidFor != null)
ValidForTicks = null;
else if (expiresAt != null && ExpiresAtTicks - ValidFromTicks != ValidForTicks)
ValidForTicks = ExpiresAtTicks - ValidFromTicks;
}
}
public long? ExpiresAtTicks { get => ExpiresAt?.Ticks; set => ExpiresAt = value == null ? null : new DateTime?(new DateTime(value.Value, DateTimeKind.Utc)); }
#region Operations
public TimeSpan GetAge(DateTime? asOf = null)
{
return (asOf?.EnsureUtc() ?? DateTime.UtcNow) - ValidFrom.EnsureUtc();
}
public bool IsExpired(DateTime? asOf = null)
{
if (ValidFor == null)
return false;
return GetAge(asOf).Ticks > ValidFor.Value.Ticks;
}
public bool IsActive(DateTime? asOf = null)
{
return ValidFrom.Ticks <= (asOf?.EnsureUtc().Ticks ?? DateTime.UtcNow.Ticks) && !IsExpired(asOf);
}
public void ActiveAsOf(DateTime asOf) => ValidFrom = asOf.EnsureUtc();
public void DoNotExpire() => ExpiresAt = null;
public void ExpireAt(DateTime at) => ExpiresAt = at.EnsureUtc();
public void ExpireIn(TimeSpan timeSpan) => ValidFor = timeSpan;
#endregion
}
}