-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeHandler.cs
41 lines (33 loc) · 1.27 KB
/
TimeHandler.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
using System;
using System.Collections.Generic;
namespace Hashcode2022
{
public static class TimeHandler
{
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static Dictionary<string, long> timers = new Dictionary<string, long>();
public static void StartTimer(string timerId)
{
if (timers.ContainsKey(timerId))
{
Console.WriteLine("Timer already started!");
return;
}
Console.WriteLine("Timer " + timerId + " started");
if (timers == null) timers = new Dictionary<string, long>();
timers.Add(timerId, GetTime());
}
public static long EndTimer(string timerId)
{
if (!timers.ContainsKey(timerId))
{
Console.WriteLine("Timer not started!");
return 0;
}
var milliseconds = GetTime() - timers[timerId];
Console.WriteLine("Timer " + timerId + " lasted " + milliseconds + " milliseconds");
return milliseconds;
}
private static long GetTime() => (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
}