-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
209 lines (173 loc) · 5.79 KB
/
Utils.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
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
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace MigrateData3to4
{
static partial class Utils
{
internal static void LogMessage(string message)
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message);
}
internal static char GetLogFileSeparator(string line, char defSep)
{
// we know the dayfile and monthly log files start with
// dd/MM/yy,NN,...
// dd/MM/yy,hh:mm,N.N,....
// so we just need to find the first separator after the date before a number
var reg = LogFileSeparatorRegex().Match(line);
if (reg.Success)
return reg.Groups[1].Value[0];
else
return defSep;
}
internal static char GetDayFileTimeSeparator(string highGustTime, char defSep)
{
// we know the dayfile starts with
// dd/MM/yy,NN,NN,hh:mm,...
// so we just need to find the first separator after the first three fields and before a number
var reg = DayFileTimeSeparatorRegex().Match(highGustTime);
if (reg.Success)
return reg.Groups[1].Value[0];
else
return defSep;
}
internal static char GetLogFileTimeSeparator(string line, char defSep)
{
// we know the monthly log files start with
// dd/MM/yy,hh:mm,N.N,....
// so we just need to find the first separator after the date and hour and before a number
var reg = LogFileTimeSeparatorRegex().Match(line);
if (reg.Success)
return reg.Groups[1].Value[0];
else
return defSep;
}
internal static bool TryDetectNewLine(string path, out string newLine)
{
using var fs = File.OpenRead(path);
char prevChar = '\0';
// read the first 1000 characters to try and find a newLine
for (var i = 0; i < 1000; i++)
{
int b;
if ((b = fs.ReadByte()) == -1)
break;
char curChar = (char)b;
if (curChar == '\n')
{
newLine = prevChar == '\r' ? "\r\n" : "\n";
return true;
}
prevChar = curChar;
}
// Returning false means could not determine linefeed convention
newLine = Environment.NewLine;
return false;
}
internal static string DdmmyyStrToStr(string d)
{
// Horrible hack, but we have localised separators, but UK sequence, so localised parsing may fail
// Determine separators from the strings, allow for multi-byte!
var datSep = DateOrTimeSeparatorRegex().Match(d).Value;
// Converts a date string in UK order to a string
string[] date = d.Split(new string[] { datSep }, StringSplitOptions.None);
return date[0] + "/" + date[1] + "/" + date[2];
}
internal static DateTime DdmmyyStrToDate(string d)
{
// Horrible hack, but we have localised separators, but UK sequence, so localised parsing may fail
// Determine separators from the strings, allow for multi-byte!
var datSep = DateOrTimeSeparatorRegex().Match(d).Value;
// Converts a date string in UK order to a DateTime
string[] date = d.Split(new string[] { datSep }, StringSplitOptions.None);
int D = Convert.ToInt32(date[0]);
int M = Convert.ToInt32(date[1]);
int Y = Convert.ToInt32(date[2]);
if (Y > 70)
{
Y += 1900;
}
else
{
Y += 2000;
}
return new DateTime(Y, M, D);
}
internal static DateTime DdmmyyhhmmStrToDate(string d, string t)
{
// Horrible hack, but we have localised separators, but UK sequence, so localised parsing may fail
// Determine separators from the strings, allow for multi-byte!
var datSep = DateOrTimeSeparatorRegex().Match(d).Value;
var timSep = DateOrTimeSeparatorRegex().Match(t).Value;
// Converts a date string in UK order to a DateTime
string[] date = d.Split(new string[] { datSep }, StringSplitOptions.None);
string[] time = t.Split(new string[] { timSep }, StringSplitOptions.None);
int D = Convert.ToInt32(date[0]);
int M = Convert.ToInt32(date[1]);
int Y = Convert.ToInt32(date[2]);
// Double check - just in case we get a four digit year!
if (Y < 1900)
{
Y += Y > 70 ? 1900 : 2000;
}
int h = Convert.ToInt32(time[0]);
int m = Convert.ToInt32(time[1]);
return new DateTime(Y, M, D, h, m, 0, DateTimeKind.Local);
}
internal static long ToUnixTime(DateTime dateTime)
{
var dateTimeOffset = new DateTimeOffset(dateTime);
return dateTimeOffset.ToUnixTimeSeconds();
}
internal static string GetLogFileName(DateTime thedate)
{
var datestring = thedate.ToString("yyyy-MM");
return datestring + "-log.txt";
}
internal static string GetExtraLogFileName(DateTime thedate)
{
var datestring = thedate.ToString("yyyy-MM");
return "Extra-" + datestring + "-log.txt";
}
internal static string GetAirLinkLogFileName(DateTime thedate)
{
var datestring = thedate.ToString("yyyy-MM");
return "AirLink-" + datestring + "-log.txt";
}
internal static DateTime ResolveAmbiguousTime(DateTime ambiguousTime, bool useDstTime)
{
// Time is not ambiguous
if (!TimeZoneInfo.Local.IsAmbiguousTime(ambiguousTime))
{
return ambiguousTime.ToUniversalTime();
}
// Time is ambiguous
else
{
var offsets = TimeZoneInfo.Local.GetAmbiguousTimeOffsets(ambiguousTime);
var offset = TimeZoneInfo.Local.BaseUtcOffset;
if (useDstTime)
{
for (var i = 0; i < offsets.Length; i++)
{
if (!offsets[i].Equals(TimeZoneInfo.Local.BaseUtcOffset))
{
offset = offsets[i];
break;
}
}
}
return DateTime.SpecifyKind(ambiguousTime - offset, DateTimeKind.Utc);
}
}
[GeneratedRegex(@"^\d{2}[^\d]+\d{2}[^\d]+\d{2}([^\d])")]
private static partial Regex LogFileSeparatorRegex();
[GeneratedRegex(@"^\d{2}([^\d])")]
private static partial Regex DayFileTimeSeparatorRegex();
[GeneratedRegex(@"^\d{2}[^\d]+\d{2}[^\d]+\d{2}[^\d]{1}\d{2}([^\d])")]
private static partial Regex LogFileTimeSeparatorRegex();
[GeneratedRegex(@"[^\d]+")]
private static partial Regex DateOrTimeSeparatorRegex();
}
}