-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWeek.cs
139 lines (116 loc) · 5.05 KB
/
Week.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#region Using statements
using System;
using System.Globalization;
#endregion Using statements
namespace WeekNumber
{
internal class Week
{
#region Private variable that holds active week
private int _week;
#endregion Private variable that holds active week
#region Constructor that initiates active week
/// <summary>
/// Initiates the week to current
/// </summary>
public Week()
{
_week = Current();
}
#endregion Constructor that initiates active week
#region Internal static week strings
internal const string CalendarWeekRuleString = nameof(CalendarWeekRule);
internal const string FirstDay = nameof(CalendarWeekRule.FirstDay);
internal const string FirstFourDayWeek = nameof(CalendarWeekRule.FirstFourDayWeek);
internal const string FirstFullWeek = nameof(CalendarWeekRule.FirstFullWeek);
internal const string DayOfWeekString = nameof(DayOfWeek);
internal const string Monday = nameof(DayOfWeek.Monday);
internal const string Tuesday = nameof(DayOfWeek.Tuesday);
internal const string Wednesday = nameof(DayOfWeek.Wednesday);
internal const string Thursday = nameof(DayOfWeek.Thursday);
internal const string Friday = nameof(DayOfWeek.Friday);
internal const string Saturday = nameof(DayOfWeek.Saturday);
internal const string Sunday = nameof(DayOfWeek.Sunday);
#endregion Internal static week strings
#region Public function to check if week has changed
/// <summary>
/// Returns if week was changed since last check
/// </summary>
/// <returns>true|false</returns>
public bool WasChanged()
{
bool changed = _week != Current();
if (changed)
{
_week = Current();
}
Log.Info = $"Current week number is {_week} and this was {(changed ? "a change" : "same as last checked")}";
return changed;
}
#endregion Public function to check if week has changed
#region Public functions that returns week number based on calendar rules
/// <summary>
/// Get current week based on calendar rules in application settings
/// </summary>
/// <returns>Current week as int based on calendar rules in application settings</returns>
public static int Current()
{
DayOfWeek dayOfWeek;
CalendarWeekRule calendarWeekRule;
dayOfWeek = Enum.TryParse(Settings.GetSetting(DayOfWeekString), true, out dayOfWeek) ?
dayOfWeek : DayOfWeek.Monday;
calendarWeekRule = Enum.TryParse(Settings.GetSetting(CalendarWeekRuleString), true,
out calendarWeekRule) ? calendarWeekRule : CalendarWeekRule.FirstFourDayWeek;
int week = CultureInfo.CurrentCulture.Calendar.
GetWeekOfYear(DateTime.Now, calendarWeekRule, dayOfWeek);
if (week == 53 && (!YearHas53Weeks(DateTime.Now.Year)))
{
week = 1;
}
Log.Info = $"Current week is #{week}";
return week;
}
/// <summary>
/// Get week number based on calendar rules in application settings
/// </summary>
/// <param name="date">Date for which to get week number for</param>
/// <returns>Week number as for supplied date</returns>
public static int GetWeekNumber(DateTime date)
{
DayOfWeek dayOfWeek;
CalendarWeekRule calendarWeekRule;
dayOfWeek = Enum.TryParse(Settings.GetSetting(DayOfWeekString), true, out dayOfWeek) ?
dayOfWeek : DayOfWeek.Monday;
calendarWeekRule = Enum.TryParse(Settings.GetSetting(CalendarWeekRuleString), true,
out calendarWeekRule) ? calendarWeekRule : CalendarWeekRule.FirstFourDayWeek;
int week = CultureInfo.CurrentCulture.Calendar.
GetWeekOfYear(date, calendarWeekRule, dayOfWeek);
if (week == 53 && (!YearHas53Weeks(DateTime.Now.Year)))
{
week = 1;
}
Log.Info = $"Week number is {week} for date {date.Year}-{date.Month}-{date.Day}";
return week;
}
#endregion Public functions that returns week number based on calendar rules
#region Private helper functions to determine if it is week 1 or 53
private static bool YearHas53Weeks(int year)
{
return Weeks(year) == 53;
}
private static int Weeks(int year)
{
int w = 52;
if (P(year) == 4 || P(year - 1) == 3)
{
w++;
}
return w; // returns the number of weeks in that year
}
private static int P(int year)
{
return (int)(year + Math.Floor(year / 4f) - Math.Floor(year / 100f) + Math.Floor(year / 400f)) % 7;
}
#endregion Private helper functions to determine if it is week 1 or 53
}
}