-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Use ISOWeek for week numbers in Get-Date #11536
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
Conversation
You can produce a test by comparing these two (assuming that python is 100% correct): $dt = Get-Date "1900-01-01"
for ($i = 0; $i -lt 100000; $i++)
{
echo ("{0:00} {1}" -f (get-date $dt -UFormat "%V"), $dt.ToString("yyyy-MM-dd"));
$dt = $dt.AddDays(1);
} import datetime
dt = datetime.datetime(1900, 1, 1)
for _ in range(100000):
print(dt.strftime("%V %Y-%m-%d"))
dt += datetime.timedelta(days=1) |
I'm not really sure what to do with the unchecked boxes. Guidance is wanted. |
If you run the above PowerShell code with the current master and with this PR fix you will get this diff: 117 week numbers are wrong in these ~300 years.
|
@paalbra it may be worth filing a documentation issue so that we can make a note in the docs and users are aware that this functionality was broken and in which versions. Is it broken in Windows PowerShell as well, or was it broken sometime during 6.x? I would definitely recommend adding some tests as well -- perhaps test a handful of dates that are known to be affected, as well as testing perhaps at least a few weeks' worth of dates from a few different years so we have a decent test surface for the feature and can catch regressions if they occur. If you would like to test dates from a whole century, be my guess -- just be mindful of the runtime of those tests. If it's not significant, it may be worth doing, if a little bit excessive. At the least, we should definitely test the cases we know were previously broken in the last century or so and a handful of "control" cases just to be sure. Let us know if you have any questions / need some pointers. 🙂 |
@vexx32 Thanks for the reply. Adding some more docs is a good idea (dates can be pretty confusing if you're not really precise). I don't think I do believe that Windows Powershell (my Windows 10 has 5.1.18362.145) and PowerShell 6.0.0 does a simple $date = Get-Date "1900-01-01"
$count = 0
while ($count -lt 100000){
$formatdate = Get-Date $date -UFormat '%Y-%m-%d : %V'
$week1 = Get-Date $date -UFormat '%V'
$week2 = [string] ([math]::Floor(($date.DayOfYear / 7) + 1))
if ($week1 -ne $week2) {
Write-Output "$formatdate : ($week1 != $week2)"
}
$date = $date.AddDays(1)
$count += 1
} The current behavior of master branch is better, but has the bug mentioned. Looks like this behavior was added in #6542, but I'm not sure which PowerShell 6.x this got released in? How can I find out? More tests is also a good idea. I'll look into that. I think adding at least some of the dates in the sdiff output above might be enough. |
I've added tests for some dates that triggers this issue. Also: @KalleOlaviNiemitalo has mentioned (in the issue) that |
Regarding the change in 6bd70bc . I'm not really sure how to find out when this was merged in a release, but I believe that this command might be correct?
I.e. it was released in 6.1.0 |
@paalbra Thanks for your contribution! @SteveL-MSFT Formally it is a breaking change but I think it is right thing to get in 7.0 GA - we follow .Net Core behavior and ISO standard. |
🎉 Handy links: |
PR Summary
In #6542 we had to use a workaround to get a week of the year because .Net Core did not support the feature.
As result Get-Date -UFormat "%V" returns wrong week numbers around new year in some cases mentioned in issue #11534 .
Now we have ISOWeek API in .Net Core 3.1. The PR removes our workaround and utilizes new ISOWeek.GetWeekOfYear() method.
PR Context
This PR fixes #11534 .
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.