Skip to content

Commit 9bc8bbc

Browse files
committed
Impove the datetime extension
1 parent 1202f95 commit 9bc8bbc

3 files changed

Lines changed: 18 additions & 25 deletions

File tree

API.Test/MessagesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public void GetReceptionPeople()
6767
{
6868
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
6969

70-
Task<KeyValuePair<string, MessagePerson[]>[]> people = Client.GetMessagePeopleAsync();
70+
Task<Dictionary<string, MessagePerson[]>> people = Client.GetMessagePeopleAsync();
7171
people.Wait();
72-
if (people.Result.Length > 0)
72+
if (people.Result.Count > 0)
7373
Assert.Pass();
7474
else
7575
Assert.Fail();

WebUntisAPI.Client/Extensions/DateTimeExtensions.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,25 @@ public static void ToWebUntisTimeFormat(this DateTime dateTime, out string dateS
3939
/// The time format is HHMM (Hour Minute).
4040
/// </para>
4141
/// </remarks>
42-
/// <param name="dateTime">Instance</param>
42+
/// <param name="_">Instance</param>
4343
/// <param name="dateString">Date string</param>
4444
/// <param name="timeString">Time string</param>
4545
/// <returns>The new instance that contains the given time. When not to throw on exception and an exception happened is the return value <see langword="null"/></returns>
4646
/// <exception cref="FormatException">Thrown when one of the given strings isn't in the right format</exception>
47-
public static DateTime FromWebUntisTimeFormat(this DateTime dateTime, string dateString, string timeString)
47+
public static DateTime FromWebUntisTimeFormat(this DateTime _, string dateString, string timeString)
4848
{
49-
Regex dateRegex = new Regex(@"^\d{4}(?:0\d|1[0-2])(?:0[1-9]|[1-2]\d|3[0-1])$"); // Regex for the WebUntis date format
50-
Regex timeRegex = new Regex(@"^(?:\d|1\d|2[0-3])[0-5]\d$"); // Regex for the WebUntis time format
51-
52-
// Check if the date- and time strings are valid
53-
bool isDateValid = dateRegex.IsMatch(dateString);
54-
bool isTimeValid = timeRegex.IsMatch(timeString);
55-
56-
if (!isDateValid || !isTimeValid)
57-
throw new FormatException($"The string {(isDateValid ? timeString : dateString)} isn't in the valid format!");
49+
Match dateMatch = Regex.Match(dateString, @"^(\d{4})(0\d|1[0-2])(0[1-9]|[1-2]\d|3[0-1])$");
50+
Match timeMatch = Regex.Match(timeString, @"^(\d|1\d|2[0-3])([0-5]\d)$");
5851

52+
if (!dateMatch.Success || !timeMatch.Success)
53+
throw new FormatException("A valid format was expected");
5954

60-
// Parse the numbers in the string to value
61-
int year = int.Parse(dateString.Substring(0, 4));
62-
int month = int.Parse(dateString.Substring(4, 2));
63-
int day = int.Parse(dateString.Substring(6, 2));
55+
int year = int.Parse(dateMatch.Groups[1].Value);
56+
int month = int.Parse(dateMatch.Groups[2].Value);
57+
int day = int.Parse(dateMatch.Groups[3].Value);
6458

65-
bool is4Letters = timeString.Length == 4;
66-
int hour = int.Parse(is4Letters ? timeString.Substring(0, 2) : timeString[0].ToString());
67-
int minute = int.Parse(is4Letters ? timeString.Substring(2, 2) : timeString.Substring(1, 2));
59+
int hour = int.Parse(timeMatch.Groups[1].Value);
60+
int minute = int.Parse(timeMatch.Groups[2].Value);
6861

6962
// date and time string to DateTime instance
7063
return new DateTime(year, month, day, hour, minute, 0);

WebUntisAPI.Client/WebUntisClient.Messages.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public async Task<MessagePermissions> GetMessagePermissionsAsync(CancellationTok
5252
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
5353
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
5454
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
55-
public async Task<KeyValuePair<string, MessagePerson[]>[]> GetMessagePeopleAsync(CancellationToken ct = default)
55+
public async Task<Dictionary<string, MessagePerson[]>> GetMessagePeopleAsync(CancellationToken ct = default)
5656
{
5757
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/rest/view/v1/messages/recipients/static/persons", ct);
5858

59-
List<KeyValuePair<string, MessagePerson[]>> personTypes = new List<KeyValuePair<string, MessagePerson[]>>();
59+
Dictionary<string, MessagePerson[]> personTypes = new Dictionary<string, MessagePerson[]>();
6060
JArray types = JArray.Parse(responseString);
6161
foreach (JObject personType in types.Cast<JObject>())
62-
personTypes.Add(new KeyValuePair<string, MessagePerson[]>(personType.Value<string>("type"),
63-
new JsonSerializer().Deserialize<List<MessagePerson>>(personType.Value<JArray>("persons").CreateReader()).ToArray()));
64-
return personTypes.ToArray();
62+
personTypes.Add(personType.Value<string>("type"),
63+
new JsonSerializer().Deserialize<List<MessagePerson>>(personType.Value<JArray>("persons").CreateReader()).ToArray());
64+
return personTypes;
6565
}
6666

6767
/// <summary>

0 commit comments

Comments
 (0)