-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeUtils.cs
71 lines (58 loc) · 2.34 KB
/
TimeUtils.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
using System.Numerics;
using System;
using System.Globalization;
namespace Bitmex{
internal class TimeUtils{
private string timeframe;
private CultureInfo provider = new CultureInfo("en-US");
private static readonly DateTime epochStart = DateTime.UnixEpoch.ToUniversalTime();
internal TimeUtils(string timeframe) {
this.timeframe = timeframe;
}
internal DateTime GetNextStartTime(string previousTimestamp) {
string dateFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
DateTime newStartTime = DateTime.ParseExact(previousTimestamp, dateFormat, provider)
.AddMinutes(GetTimeframeInMins());
return newStartTime;
}
internal decimal TimeDeltaInMins(DateTime startTime, DateTime endTime)
=> (decimal)endTime.Subtract(startTime).TotalMinutes;
internal DateTime USDateParse(string date) {
string[] dateFormat = {"MM/dd/yyyy", "MM/dd/yyyy HH:mm"};
return DateTime.ParseExact(
date,
dateFormat,
provider);
}
internal string TimeInBMexFormat (DateTime dateTime)
// Ha, I like these functional methods
=> dateTime.ToString("MM/dd/yyyy HH:mm:ss");
internal int GetTimeframeInMins() {
switch (timeframe) {
// Select the timeframe seconds
case "1m":
return (int)BINSIZES.MIN;
case "5m":
return (int)BINSIZES.MIN5;
case "1h":
return (int)BINSIZES.HR;
case "1d":
return (int)BINSIZES.DAY;
default:
return (int)BINSIZES.DAY;
}
}
internal static string UnixTimeToTickSeconds(string time) {
/// <summary>
/// Converts a string representation of a Unix timestamp
/// to seconds since Unix epoch start time.
/// </summary>
DateTimeOffset timeOffset = DateTimeOffset.Parse(time).UtcDateTime;
TimeSpan timeSpan = timeOffset.Subtract(epochStart);
return ((BigInteger) timeSpan.TotalSeconds).ToString();
}
internal enum BINSIZES {
MIN = 1, MIN5 = 5, HR = 60, DAY = 1440
}
}
}