-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalkmanLibTimeConvert.cs
More file actions
282 lines (240 loc) · 11.7 KB
/
WalkmanLibTimeConvert.cs
File metadata and controls
282 lines (240 loc) · 11.7 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using static System.Math;
public partial class WalkmanLib {
public struct TimeInfo {
public ulong Seconds;
public ulong Minutes;
public ulong Hours;
public ulong Days;
public ulong Weeks;
}
public class TimeConvert {
/// <summary>Amount of Seconds in a Minute</summary>
private const ulong _SecondsInMinute = 60;
/// <summary>Amount of Minutes in an Hour</summary>
private const ulong _MinutesInHour = 60;
/// <summary>Amount of Hours in a Day</summary>
private const ulong _HoursInDay = 24;
/// <summary>Amount of Days in a Week</summary>
private const ulong _DaysInWeek = 7;
/// <summary>Amount of Seconds in an Hour</summary>
private const ulong _SecondsInHour = _SecondsInMinute * _MinutesInHour;
/// <summary>Amount of Seconds in a Day</summary>
private const ulong _SecondsInDay = _SecondsInHour * _HoursInDay;
/// <summary>Amount of Seconds in a Week</summary>
private const ulong _SecondsInWeek = _SecondsInDay * _DaysInWeek;
/// <summary>Amount of Minutes in a Day</summary>
private const ulong _MinutesInDay = _MinutesInHour * _HoursInDay;
/// <summary>Amount of Minutes in a Week</summary>
private const ulong _MinutesInWeek = _MinutesInDay * _DaysInWeek;
/// <summary>Amount of Hours in a Week</summary>
private const ulong _HoursInWeek = _HoursInDay * _DaysInWeek;
#region Single value to multiple
/// <summary>Truncates and casts a <see cref="double"/> to <see cref="ulong"/></summary>
/// <param name="value"><see cref="double"/> value to truncate</param>
private static ulong Tr(double value) {
value = Truncate(value);
return (ulong)value;
}
/// <summary>Removes all the digits before the decimal point, using <see cref="decimal"/> to do the calculation so no precision is lost</summary>
/// <param name="value"><see cref="Double"/> value to get decimals</param>
private static decimal Digits(double value) {
return (decimal)value % 1;
}
/// <summary>Converts seconds to a <see cref="TimeInfo"/> struct. Any amount that can convert will be put into higher units</summary>
/// <param name="seconds">Seconds to convert</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvSeconds(ulong seconds) {
var rtn = new TimeInfo();
if (seconds >= _SecondsInWeek) {
rtn.Weeks = seconds / _SecondsInWeek;
seconds %= _SecondsInWeek;
}
if (seconds >= _SecondsInDay) {
rtn.Days = seconds / _SecondsInDay;
seconds %= _SecondsInDay;
}
if (seconds >= _SecondsInHour) {
rtn.Hours = seconds / _SecondsInHour;
seconds %= _SecondsInHour;
}
if (seconds >= _SecondsInMinute) {
rtn.Minutes = seconds / _SecondsInMinute;
seconds %= _SecondsInMinute;
}
rtn.Seconds = seconds;
return rtn;
}
/// <summary>Converts minutes to a <see cref="TimeInfo"/> struct, without any lower units. Any amount that can convert will be put into higher units</summary>
/// <param name="minutes">Minutes to convert</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvMinutes(ulong minutes) {
var rtn = new TimeInfo();
if (minutes >= _MinutesInWeek) {
rtn.Weeks = minutes / _MinutesInWeek;
minutes %= _MinutesInWeek;
}
if (minutes >= _MinutesInDay) {
rtn.Days = minutes / _MinutesInDay;
minutes %= _MinutesInDay;
}
if (minutes >= _MinutesInHour) {
rtn.Hours = minutes / _MinutesInHour;
minutes %= _MinutesInHour;
}
rtn.Minutes = minutes;
return rtn;
}
/// <summary>Converts minutes to a <see cref="TimeInfo"/> struct, with decimal places converted to seconds. Any amount that can convert will be put into higher units</summary>
/// <param name="minutes">Minutes to convert. This is Abs()'d, so negativity doesn't make a difference</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvMinutesD(double minutes) {
minutes = Abs(minutes);
ulong minutesUL = Tr(minutes);
TimeInfo rtn = ConvMinutes(minutesUL);
minutes = (double)Digits(minutes);
minutes *= _SecondsInMinute;
rtn.Seconds = Tr(minutes);
return rtn;
}
/// <summary>Converts hours to a <see cref="TimeInfo"/> struct, without any lower units. Any amount that can convert will be put into higher units</summary>
/// <param name="hours">Hours to convert</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvHours(ulong hours) {
var rtn = new TimeInfo();
if (hours >= _HoursInWeek) {
rtn.Weeks = hours / _HoursInWeek;
hours %= _HoursInWeek;
}
if (hours >= _HoursInDay) {
rtn.Days = hours / _HoursInDay;
hours %= _HoursInDay;
}
rtn.Hours = hours;
return rtn;
}
/// <summary>Converts hours to a <see cref="TimeInfo"/> struct, with decimal places converted to lower units. Any amount that can convert will be put into higher units</summary>
/// <param name="hours">Hours to convert. This is Abs()'d, so negativity doesn't make a difference</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvHoursD(double hours) {
hours = Abs(hours);
ulong hoursUL = Tr(hours);
TimeInfo rtn = ConvHours(hoursUL);
hours = (double)Digits(hours);
hours *= _MinutesInHour;
rtn.Minutes = Tr(hours);
hours = (double)Digits(hours);
hours *= _SecondsInMinute;
rtn.Seconds = Tr(hours);
return rtn;
}
/// <summary>Converts days to a <see cref="TimeInfo"/> struct, without any lower units. Any amount that can convert will be put into higher units (Weeks only)</summary>
/// <param name="days">Days to convert</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvDays(ulong days) {
var rtn = new TimeInfo();
if (days >= _DaysInWeek) {
rtn.Weeks = days / _DaysInWeek;
days %= _DaysInWeek;
}
rtn.Days = days;
return rtn;
}
/// <summary>Converts days to a <see cref="TimeInfo"/> struct, with decimal places converted to lower units. Any amount that can convert will be put into higher units (Weeks only)</summary>
/// <param name="days">Days to convert. This is Abs()'d, so negativity doesn't make a difference</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvDaysD(double days) {
days = Abs(days);
ulong daysUL = Tr(days);
TimeInfo rtn = ConvDays(daysUL);
days = (double)Digits(days);
days *= _HoursInDay;
rtn.Hours = Tr(days);
days = (double)Digits(days);
days *= _MinutesInHour;
rtn.Minutes = Tr(days);
days = (double)Digits(days);
days *= _SecondsInMinute;
rtn.Seconds = Tr(days);
return rtn;
}
/// <summary>Converts Weeks to a <see cref="TimeInfo"/> struct, with decimal places converted to lower units</summary>
/// <param name="weeks">Weeks to convert. This is Abs()'d, so negativity doesn't make a difference</param>
/// <returns><see cref="TimeInfo"/> struct with values filled in</returns>
public static TimeInfo ConvWeeksD(double weeks) {
weeks = Abs(weeks);
TimeInfo rtn;
rtn.Weeks = Tr(weeks);
weeks = (double)Digits(weeks);
weeks *= _DaysInWeek;
rtn.Days = Tr(weeks);
weeks = (double)Digits(weeks);
weeks *= _HoursInDay;
rtn.Hours = Tr(weeks);
weeks = (double)Digits(weeks);
weeks *= _MinutesInHour;
rtn.Minutes = Tr(weeks);
weeks = (double)Digits(weeks);
weeks *= _SecondsInMinute;
rtn.Seconds = Tr(weeks);
return rtn;
}
#endregion
#region Multiple to single value
/// <summary>Converts a <see cref="TimeInfo"/> struct to seconds</summary>
/// <param name="ti">The <see cref="TimeInfo"/> to convert</param>
/// <returns>Result in seconds</returns>
public static ulong GetSeconds(TimeInfo ti) {
ulong rtn = ti.Seconds;
rtn += ti.Minutes * _SecondsInMinute;
rtn += ti.Hours * _SecondsInHour;
rtn += ti.Days * _SecondsInDay;
rtn += ti.Weeks * _SecondsInWeek;
return rtn;
}
/// <summary>Converts a <see cref="TimeInfo"/> struct to minutes, with seconds as decimal places</summary>
/// <param name="ti">The <see cref="TimeInfo"/> to convert</param>
/// <returns>Result in minutes</returns>
public static double GetMinutes(TimeInfo ti) {
double rtn = ti.Minutes;
rtn += ti.Seconds / (double)_SecondsInMinute;
rtn += ti.Hours * _MinutesInHour;
rtn += ti.Days * _MinutesInDay;
rtn += ti.Weeks * _MinutesInWeek;
return rtn;
}
/// <summary>Converts a <see cref="TimeInfo"/> struct to hours, with smaller units as decimal places</summary>
/// <param name="ti">The <see cref="TimeInfo"/> to convert</param>
/// <returns>Result in hours</returns>
public static double GetHours(TimeInfo ti) {
double rtn = ti.Hours;
rtn += ti.Seconds / (double)_SecondsInHour;
rtn += ti.Minutes / (double)_MinutesInHour;
rtn += ti.Days * _HoursInDay;
rtn += ti.Weeks * _HoursInWeek;
return rtn;
}
/// <summary>Converts a <see cref="TimeInfo"/> struct to days, with smaller units as decimal places</summary>
/// <param name="ti">The <see cref="TimeInfo"/> to convert</param>
/// <returns>Result in days</returns>
public static double GetDays(TimeInfo ti) {
double rtn = ti.Days;
rtn += ti.Seconds / (double)_SecondsInDay;
rtn += ti.Minutes / (double)_MinutesInDay;
rtn += ti.Hours / (double)_HoursInDay;
rtn += ti.Weeks * _DaysInWeek;
return rtn;
}
/// <summary>Converts a <see cref="TimeInfo"/> struct to weeks, with smaller units as decimal places</summary>
/// <param name="ti">The <see cref="TimeInfo"/> to convert</param>
/// <returns>Result in weeks</returns>
public static double GetWeeks(TimeInfo ti) {
double rtn = ti.Weeks;
rtn += ti.Seconds / (double)_SecondsInWeek;
rtn += ti.Minutes / (double)_MinutesInWeek;
rtn += ti.Hours / (double)_HoursInWeek;
rtn += ti.Days / (double)_DaysInWeek;
return rtn;
}
#endregion
}
}