Skip to content

Commit

Permalink
Adjust YearWeek Year and StartDate
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarfgq authored and geoperez committed Jan 3, 2024
1 parent 4fc3312 commit a8ed51e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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

0 comments on commit a8ed51e

Please sign in to comment.