Fixed current day of week calculation

This commit is contained in:
Simon Lübeß
2024-04-05 13:54:38 +02:00
parent 81cbff9cab
commit b42ba4a9b1
13 changed files with 1981 additions and 187 deletions

View File

@ -1,4 +1,5 @@
using UnityEngine;
using UnityEditor;
using UnityEngine;
namespace Utility
{
@ -23,7 +24,15 @@ namespace Utility
else if (_instance != value)
{
Debug.LogError("Instance already exists. Deleting duplicate.");
Destroy(value.gameObject);
if (Application.isEditor)
{
DestroyImmediate(value.gameObject);
}
else
{
Destroy(value.gameObject);
}
}
}
}
@ -41,8 +50,11 @@ namespace Utility
public void OnAfterDeserialize()
{
// The value of Instance is lost after deserialization, so we need to set it again.
Instance = (T)this;
if (!Application.isEditor)
{
// The value of Instance is lost after deserialization, so we need to set it again.
Instance = (T)this;
}
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using UnityEngine;
namespace Utility
{
[Serializable]
public struct SimpleTime
{
[SerializeField, Range(0, 23)]
private byte _hour;
[SerializeField, Range(0, 59)]
private byte _minutes;
public int Hour
{
get => _hour;
set => _hour = (byte)value;
}
public int Minute
{
get => _minutes;
set => _minutes = (byte)value;
}
public TimeSpan ToTimeSpan() => new(_hour, _minutes, 0);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 012fa3bc180f42f396fca3a7a6c4fb5b
timeCreated: 1712316058

View File

@ -0,0 +1,17 @@
using System;
namespace Utility
{
public static class TimeSpanExtension
{
public static bool IsBetween(this TimeSpan value, TimeSpan start, TimeSpan end)
{
if (start > end)
{
throw new ArgumentException(nameof(start), "start must be before end.");
}
return value >= start && value <= end;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d76aeb99bb254637b57aa6f86d764c01
timeCreated: 1712316955

View File

@ -1,4 +1,6 @@
namespace Utility
using System;
namespace Utility
{
public enum Weekday
{
@ -41,4 +43,35 @@
_ => "Unbekannt"
};
}
public static class DayOfWeekExtension
{
/// <summary>
/// Gibt den Wochentag zurück, der auf den gegebenen Wochentag folgt.
/// </summary>
public static DayOfWeek GetNext(this DayOfWeek current) => GetFutureDay(current, 1);
/// <summary>
/// Gibt den Wochentag zurück, der in der gegebenen Anzahl an Tagen auf den gegebenen Wochentag folgt.
/// </summary>
/// <param name="current">Der aktuelle Wochentag.</param>
/// <param name="days">Die Anzahl an Tagen, die vergehen sollen.</param>
public static DayOfWeek GetFutureDay(this DayOfWeek current, int days) =>
(DayOfWeek)(((int)current + days) % 7);
/// <summary>
/// Gibt den deutschen Namen des Wochentags zurück.
/// </summary>
public static string GetName(this DayOfWeek weekday) => weekday switch
{
DayOfWeek.Monday => "Montag",
DayOfWeek.Tuesday => "Dienstag",
DayOfWeek.Wednesday => "Mittwoch",
DayOfWeek.Thursday => "Donnerstag",
DayOfWeek.Friday => "Freitag",
DayOfWeek.Saturday => "Samstag",
DayOfWeek.Sunday => "Sonntag",
_ => "Unbekannt"
};
}
}