Skip to content

Adjust YearWeek Year and StartDate #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Unosquare.DateTimeExt/YearWeek.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public YearWeek(DateTime? dateTime)

public bool IsCurrent => IsCurrentYear && Week == DateTime.UtcNow.GetWeekOfYear();

public bool IsCurrentYear => Year == DateTime.UtcNow.Year;
public bool IsCurrentYear => Year == WeekYear;

private static DateTime GetStartDate(int? week = null, int? year = null) =>
DateExtensions.FirstDateOfWeek(year ?? DateTime.UtcNow.Year, week ?? DateTime.UtcNow.GetWeekOfYear());
DateExtensions.FirstDateOfWeek(year ?? WeekYear, week ?? DateTime.UtcNow.GetWeekOfYear());

public YearWeek AddWeeks(int count) => new(StartDate.AddDays(WeekDays * count));

Expand Down Expand Up @@ -83,4 +83,8 @@ public static bool TryParse(string? value, out YearWeek result)
result = new(week, year);
return true;
}

private static int WeekYear => DateTime.UtcNow.Month == 1 && DateTime.UtcNow.GetWeekOfYear() > 5
? DateTime.UtcNow.Year - 1
: DateTime.UtcNow.Year;
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public void YearWeek_Current()
{
var yearWeek = YearWeek.Current;

Assert.Equal(DateTime.UtcNow.Year, yearWeek.Year);
Assert.Equal(DateTime.UtcNow.Month == 1 && DateTime.UtcNow.GetWeekOfYear() > 5
? DateTime.UtcNow.Year - 1
: DateTime.UtcNow.Year, yearWeek.Year);
}

[Fact]
Expand Down