Skip to content
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

Change all Cookie localized DateTime logic with Universal alternative #100489

Merged
merged 16 commits into from
Apr 24, 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
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Net/CookieParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ private static FieldInfo IsQuotedVersionField
expiresSet = true;
if (int.TryParse(CheckQuoted(_tokenizer.Value), out int parsed))
{
cookie!.Expires = DateTime.Now.AddSeconds(parsed);
cookie!.Expires = DateTime.UtcNow.AddSeconds(parsed);
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class Cookie
private bool m_secure; // Do not rename (binary serialization)
[System.Runtime.Serialization.OptionalField]
private bool m_httpOnly = false; // Do not rename (binary serialization)
private DateTime m_timeStamp = DateTime.Now; // Do not rename (binary serialization)
private DateTime m_timeStamp = DateTime.UtcNow; // Do not rename (binary serialization)
private string m_value = string.Empty; // Do not rename (binary serialization)
private int m_version; // Do not rename (binary serialization)

Expand Down Expand Up @@ -199,13 +199,13 @@ public bool Expired
{
get
{
return (m_expires != DateTime.MinValue) && (m_expires.ToLocalTime() <= DateTime.Now);
return (m_expires != DateTime.MinValue) && (m_expires.ToUniversalTime() <= DateTime.UtcNow);
}
set
{
if (value)
{
m_expires = DateTime.Now;
m_expires = DateTime.UtcNow;
}
}
}
Expand Down Expand Up @@ -801,7 +801,7 @@ internal void ToString(StringBuilder sb)
}
if (Expires != DateTime.MinValue)
{
int seconds = (int)(Expires.ToLocalTime() - DateTime.Now).TotalSeconds;
int seconds = (int)(Expires.ToUniversalTime() - DateTime.UtcNow).TotalSeconds;
if (seconds < 0)
{
// This means that the cookie has already expired. Set Max-Age to 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public static void Expired_GetSet_Success()
Cookie c = new Cookie();
Assert.False(c.Expired);

c.Expires = DateTime.Now.AddDays(-1);
c.Expires = DateTime.UtcNow.AddDays(-1);
Assert.True(c.Expired);

c.Expires = DateTime.Now.AddDays(1);
c.Expires = DateTime.UtcNow.AddDays(1);
Assert.False(c.Expired);

c.Expired = true;
Expand All @@ -135,7 +135,7 @@ public static void Expires_GetSet_Success()
Cookie c = new Cookie();
Assert.Equal(c.Expires, DateTime.MinValue);

DateTime dt = DateTime.Now;
DateTime dt = DateTime.UtcNow;
c.Expires = dt;
Assert.Equal(dt, c.Expires);
}
Expand Down Expand Up @@ -226,7 +226,9 @@ public static void Secure_GetSet_Success()
[Fact]
public static void Timestamp_GetSet_Success()
{
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0); //DateTime.Now changes as the test runs
//DateTime.UtcNow changes as the test runs
DateTime dt = DateTime.UtcNow;
dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
Cookie c = new Cookie();
Assert.True(c.TimeStamp >= dt);
}
Expand Down
Loading