Skip to content

Commit

Permalink
Change all Cookie localized DateTime logic with Universal alternative (
Browse files Browse the repository at this point in the history
…#100489)

* Change all localized DateTime logic with Universal alternative

* tests adjusted

* comment updated according to changes

* Update src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs

Co-authored-by: Radek Zikmund <32671551+rzikm@users.noreply.github.com>

* Code review comment fix

---------

Co-authored-by: Radek Zikmund <32671551+rzikm@users.noreply.github.com>
Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent 3027ff1 commit 33e3f0c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
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

0 comments on commit 33e3f0c

Please sign in to comment.