forked from winsw/winsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeriodicRollingCalendar.cs
116 lines (101 loc) · 4.09 KB
/
PeriodicRollingCalendar.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
using System;
// ReSharper disable InconsistentNaming
namespace winsw
{
/**
* This is largely borrowed from the logback Rolling Calendar.
**/
public class PeriodicRollingCalendar
{
private readonly string _format;
private readonly long _period;
private DateTime _currentRoll;
private DateTime _nextRoll;
public PeriodicRollingCalendar(string format, long period)
{
_format = format;
_period = period;
_currentRoll = DateTime.Now;
}
public void init()
{
periodicityType = determinePeriodicityType();
_nextRoll = nextTriggeringTime(_currentRoll, _period);
}
public enum PeriodicityType
{
ERRONEOUS, TOP_OF_MILLISECOND, TOP_OF_SECOND, TOP_OF_MINUTE, TOP_OF_HOUR, TOP_OF_DAY
}
private static readonly PeriodicityType[] VALID_ORDERED_LIST = {
PeriodicityType.TOP_OF_MILLISECOND, PeriodicityType.TOP_OF_SECOND, PeriodicityType.TOP_OF_MINUTE, PeriodicityType.TOP_OF_HOUR, PeriodicityType.TOP_OF_DAY
};
private PeriodicityType determinePeriodicityType()
{
PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(_format, _period);
DateTime epoch = new DateTime(1970, 1, 1);
foreach (PeriodicityType i in VALID_ORDERED_LIST)
{
string r0 = epoch.ToString(_format);
periodicRollingCalendar.periodicityType = i;
DateTime next = periodicRollingCalendar.nextTriggeringTime(epoch, 1);
string r1 = next.ToString(_format);
if (r0 != null && r1 != null && !r0.Equals(r1))
{
return i;
}
}
return PeriodicityType.ERRONEOUS;
}
private DateTime nextTriggeringTime(DateTime input, long increment)
{
DateTime output;
switch (periodicityType)
{
case PeriodicityType.TOP_OF_MILLISECOND:
output = new DateTime(input.Year, input.Month, input.Day, input.Hour, input.Minute, input.Second, input.Millisecond);
output = output.AddMilliseconds(increment);
return output;
case PeriodicityType.TOP_OF_SECOND:
output = new DateTime(input.Year, input.Month, input.Day, input.Hour, input.Minute, input.Second);
output = output.AddSeconds(increment);
return output;
case PeriodicityType.TOP_OF_MINUTE:
output = new DateTime(input.Year, input.Month, input.Day, input.Hour, input.Minute, 0);
output = output.AddMinutes(increment);
return output;
case PeriodicityType.TOP_OF_HOUR:
output = new DateTime(input.Year, input.Month, input.Day, input.Hour, 0, 0);
output = output.AddHours(increment);
return output;
case PeriodicityType.TOP_OF_DAY:
output = new DateTime(input.Year, input.Month, input.Day);
output = output.AddDays(increment);
return output;
default:
throw new Exception("invalid periodicity type: " + periodicityType);
}
}
public PeriodicityType periodicityType { get; set; }
public Boolean shouldRoll
{
get
{
DateTime now = DateTime.Now;
if (now > _nextRoll)
{
_currentRoll = now;
_nextRoll = nextTriggeringTime(now, _period);
return true;
}
return false;
}
}
public string format
{
get
{
return _currentRoll.ToString(_format);
}
}
}
}