-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.cs
executable file
·82 lines (72 loc) · 1.89 KB
/
Timer.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
using System;
namespace Wizcorp.MageSDK.Time
{
public class Timer
{
public double OffsetMSec { get; private set; }
public double AccelerationFactor { get; private set; }
public double StartAt { get; private set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public static double DateNowMSec() {
return Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
}
/// <summary>
///
/// </summary>
public Timer() {
OffsetMSec = 0;
AccelerationFactor = 1;
StartAt = DateNowMSec();
}
/// <summary>
///
/// </summary>
/// <param name="offsetMSec"></param>
/// <param name="accelerationFactor"></param>
/// <param name="startAt"></param>
public void Configure(double offsetMSec = 0, double accelerationFactor = 1, double? startAt = null) {
OffsetMSec = offsetMSec;
AccelerationFactor = accelerationFactor;
StartAt = (startAt != null) ? (double)startAt : DateNowMSec();
}
/// <summary>
///
/// </summary>
/// <param name="timestamp"></param>
/// <param name="msecOut"></param>
/// <returns></returns>
public double Translate(double timestamp, bool msecOut = false) {
double now = timestamp + OffsetMSec;
if (AccelerationFactor != 1 && timestamp >= StartAt) {
double msecPassed = (timestamp - StartAt) * AccelerationFactor;
now = StartAt + OffsetMSec + msecPassed;
}
return msecOut ? now : Math.Floor(now / 1000);
}
/// <summary>
///
/// </summary>
/// <param name="msecOut"></param>
/// <returns></returns>
public double Now(bool msecOut = false) {
return Translate(DateNowMSec(), msecOut);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public double Sec() {
return Translate(DateNowMSec());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public double MSec() {
return Translate(DateNowMSec(), true);
}
}
}