forked from Nenkai/PDTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDIDATETIME.cs
More file actions
217 lines (177 loc) · 6.42 KB
/
Copy pathPDIDATETIME.cs
File metadata and controls
217 lines (177 loc) · 6.42 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
using System;
using System.Collections.Generic;
using System.Text;
namespace PDTools.Structures;
/// <summary>
/// 64 bits julian date + time
/// </summary>
public class PDIDATETIME
{
public static readonly int[] DaysInYear = [0, 365, 730, 1095, 1461];
public static int[][] Days_To_Month =
[
// non-leap year
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365],
// leap year
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366],
];
const int HalfSecondsInDay = 43200; // 0xA8C0
const int SecondsInDay = 86400; // 0x15180
const int SecondsInHour = 3600;
const int SecondsInMinute = 60;
const int DaysPer400Years = 146097;
const int DaysPer100Years = 36524;
const int DaysPer4Years = 1461;
const int Jan11JulianDay = 1721426; // 0x1a4452
///////////////////////////////////////////////////////////
/////////////////// PDIDATETIME ///////////////////////////
///////////////////////////////////////////////////////////
// Handles Sec/Min/Hr/Day/Month/Year/ <-> 64 bit integer translation.
/// <summary>
/// Converts a packed PDIDATETIME julian value into a DateTime.
/// </summary>
/// <param name="julianTime"></param>
/// <returns></returns>
public static DateTime JulianToDateTime_64(ulong julianTime)
{
int dmyValue = (int)((ulong)(julianTime + HalfSecondsInDay) / SecondsInDay);
var dmy = DayToDate(dmyValue);
var smh = GetSecondMinuteHours((int)(julianTime + HalfSecondsInDay) + dmyValue * -SecondsInDay);
return new DateTime(dmy.Year, dmy.Month, dmy.Day, smh.Hours, smh.Minutes, smh.Seconds);
}
/// <summary>
/// Converts a datetime into a packed PDIDATETIME julian value.
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static double DateTimeToJulian_64(DateTime dateTime)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = dateTime.ToUniversalTime() - origin;
double unixTime = Math.Floor(diff.TotalSeconds);
double julianDate = ((unixTime / 86400) + 2440587.5) * 86400;
return julianDate;
}
private static (byte Seconds, byte Minutes, byte Hours) GetSecondMinuteHours(int smh)
{
byte hours = Convert.ToByte(smh / SecondsInHour);
byte minutes = Convert.ToByte((smh % SecondsInHour) / SecondsInMinute);
byte seconds = Convert.ToByte((smh % SecondsInHour) + minutes * -SecondsInMinute);
return (seconds, minutes, hours);
}
///////////////////////////////////////////////////////
/////////////////// PDIDATE ///////////////////////////
///////////////////////////////////////////////////////
// Handles Day/Month/Year <-> 32 bit integer translation only.
#if NET6_0_OR_GREATER
public static DateOnly DayToDateOnly(int value)
{
(int Year, int Month, int Day) = DayToDate(value);
return new DateOnly(Year, Month, Day);
}
#endif
/// <summary>
/// Converts a 32 bit packed integer into a date time (d/m/y).
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime DayToDateTime(int value)
{
(int Year, int Month, int Day) = DayToDate(value);
if (Year < 1)
Year = 1;
if (Day > 31)
Day = 31;
return new DateTime(Year, Month, Day);
}
/// <summary>
/// Converts a packed 32 bit integer into a PDIDATE (d/m/y). Original Impl
/// </summary>
public static (int Year, int Month, int Day) DayToDate(int value)
{
int n400 = (value - (Jan11JulianDay - 1)) / DaysPer400Years;
int dayOfYear = (value - (Jan11JulianDay - 1)) % DaysPer400Years;
int n100 = dayOfYear / DaysPer100Years;
dayOfYear %= DaysPer100Years;
if (dayOfYear / DaysPer100Years == 4)
{
n100 = 3;
dayOfYear %= DaysPer100Years;
}
int n4 = dayOfYear / DaysPer4Years;
dayOfYear %= DaysPer4Years;
int n1 = search_day(dayOfYear, DaysInYear, 5);
bool isLeapYear = n1 == 3 && n100 == 3;
if (n4 != 24)
isLeapYear = n100 == 3;
var day = (dayOfYear - DaysInYear[n1]);
var year = n1 + 4 * (n4 + 25 * (n100 + 4 * n400)) + 1;
var month = search_day(day, Days_To_Month[isLeapYear ? 1 : 0], 13);
int actualDay = (byte)(day - Days_To_Month[isLeapYear ? 1 : 0][month] + 1);
int actualMonth = (byte)(month + 1);
return ((short)year, actualMonth, actualDay);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Converts a date only into a 32 bit packed integer (d/m/y).
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int DayToDateOnly(DateOnly date)
{
return DateToDay(date.Year, date.Month, date.Day);
}
#endif
/// <summary>
/// Converts a date time into a 32 bit packed integer (d/m/y).
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int DateTimeToDay(DateTime date)
{
return DateToDay(date.Year, date.Month, date.Day);
}
/// <summary>
/// Converts a date only (d/m/y) into a 32 bit packed integer. Original Impl
/// </summary>
/// <param name="year"></param>
/// <param name="months"></param>
/// <param name="day"></param>
/// <returns></returns>
public static int DateToDay(int year, int months, int day)
{
int monthIndex = months - 1;
int dayIndex = day - 1;
int month = monthIndex % 12;
int yr = year + (monthIndex / 12);
if (month < 0)
{
month += 12;
--yr;
}
bool isLeapYear = (yr % 4 == 0 && (yr % 100 != 0 || yr % 400 == 0));
int yrIndex = yr - 1;
return 365 * yrIndex
+ (yrIndex - 1) / 4
- yrIndex / 100
+ yrIndex / 100 / 4
+ Days_To_Month[isLeapYear ? 1 : 0][month]
+ dayIndex
+ (Jan11JulianDay - 1);
}
static int search_day(int target, int[] values, int max)
{
int mid; // $v0
int min = 0;
for (mid = max; ; mid = min + max)
{
if (target < values[mid / 2] )
max = mid / 2;
else
min = mid / 2;
if ((int)min + 1 >= max)
break;
}
return min;
}
}