-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarHelper.cs
66 lines (53 loc) · 2.04 KB
/
CalendarHelper.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScreenTime
{
public class CalendarHelper
{
public long GetDayStart(string dateString)
{
DateTime dateTime;
// Parse the date string into a DateTime object
if (DateTime.TryParse(dateString, out dateTime))
{
// Get the local timezone
TimeZoneInfo localTimeZone = TimeZoneInfo.Local;
// Get the start of the day in the local timezone
DateTimeOffset startOfDay = new DateTimeOffset(dateTime.Date, localTimeZone.GetUtcOffset(dateTime.Date));
long startOfDayUnix = startOfDay.ToUnixTimeSeconds();
Properties.Settings.Default.UserTimeZone = localTimeZone.Id;
Properties.Settings.Default.Save();
Debug.WriteLine($"Timezone saved: {localTimeZone.Id}");
return startOfDayUnix;
}
else
{
Console.WriteLine("Invalid date format.");
return 0;
}
}
public long GetDayEnd(string dateString)
{
DateTime dateTime;
// Parse the date string into a DateTime object
if (DateTime.TryParse(dateString, out dateTime))
{
// Get the local timezone
TimeZoneInfo localTimeZone = TimeZoneInfo.Local;
// Get the end of the day (one second before midnight the next day) in the local timezone
DateTimeOffset endOfDay = new DateTimeOffset(dateTime.Date.AddDays(1).AddSeconds(-1), localTimeZone.GetUtcOffset(dateTime.Date.AddDays(1).AddSeconds(-1)));
long endOfDayUnix = endOfDay.ToUnixTimeSeconds();
return endOfDayUnix;
}
else
{
Console.WriteLine("Invalid date format.");
return 0;
}
}
}
}