chore: add hrynco common library solution
- add the standalone HrynCo.Common solution and projects - include the shared common library source and tests - add package metadata and a repo gitignore
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
namespace HrynCo.Common;
|
||||
|
||||
public sealed class Clock : IClock
|
||||
{
|
||||
private readonly DateTimeOffset _createdAt;
|
||||
private readonly DateTimeOffset _initialNow;
|
||||
private readonly DateTimeOffset _initialUtcNow;
|
||||
|
||||
public Clock()
|
||||
: this(DateTimeOffset.UtcNow)
|
||||
{
|
||||
}
|
||||
|
||||
public Clock(DateTimeOffset initialUtcNow)
|
||||
{
|
||||
_initialUtcNow = initialUtcNow;
|
||||
_initialNow = initialUtcNow.ToLocalTime();
|
||||
_createdAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public DateTimeOffset Now => _initialNow + (DateTimeOffset.UtcNow - _createdAt);
|
||||
public DateTimeOffset UtcNow => _initialUtcNow + (DateTimeOffset.UtcNow - _createdAt);
|
||||
public DateOnly Today => DateOnly.FromDateTime(Now.DateTime);
|
||||
|
||||
public DateOnly GetNextDayOfWeek(DayOfWeek dayOfWeek)
|
||||
{
|
||||
int daysToAdd = ((int)dayOfWeek - (int)Today.DayOfWeek + 7) % 7;
|
||||
return Today.AddDays(daysToAdd == 0 ? 7 : daysToAdd);
|
||||
}
|
||||
|
||||
public DateOnly GetLastDayOfMonth()
|
||||
{
|
||||
return new DateOnly(Now.Year, Now.Month, 1).AddMonths(1).AddDays(-1);
|
||||
}
|
||||
|
||||
public DateOnly GetNextMonthStart()
|
||||
{
|
||||
return new DateOnly(Now.Year, Now.Month, 1).AddMonths(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user