Skip to content

Commit 33e3f0c

Browse files
unknovvnrzikmManickaP
authored
Change all Cookie localized DateTime logic with Universal alternative (#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>
1 parent 3027ff1 commit 33e3f0c

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/libraries/Common/src/System/Net/CookieParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ private static FieldInfo IsQuotedVersionField
665665
expiresSet = true;
666666
if (int.TryParse(CheckQuoted(_tokenizer.Value), out int parsed))
667667
{
668-
cookie!.Expires = DateTime.Now.AddSeconds(parsed);
668+
cookie!.Expires = DateTime.UtcNow.AddSeconds(parsed);
669669
}
670670
else
671671
{

src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public sealed class Cookie
6363
private bool m_secure; // Do not rename (binary serialization)
6464
[System.Runtime.Serialization.OptionalField]
6565
private bool m_httpOnly = false; // Do not rename (binary serialization)
66-
private DateTime m_timeStamp = DateTime.Now; // Do not rename (binary serialization)
66+
private DateTime m_timeStamp = DateTime.UtcNow; // Do not rename (binary serialization)
6767
private string m_value = string.Empty; // Do not rename (binary serialization)
6868
private int m_version; // Do not rename (binary serialization)
6969

@@ -199,13 +199,13 @@ public bool Expired
199199
{
200200
get
201201
{
202-
return (m_expires != DateTime.MinValue) && (m_expires.ToLocalTime() <= DateTime.Now);
202+
return (m_expires != DateTime.MinValue) && (m_expires.ToUniversalTime() <= DateTime.UtcNow);
203203
}
204204
set
205205
{
206206
if (value)
207207
{
208-
m_expires = DateTime.Now;
208+
m_expires = DateTime.UtcNow;
209209
}
210210
}
211211
}
@@ -801,7 +801,7 @@ internal void ToString(StringBuilder sb)
801801
}
802802
if (Expires != DateTime.MinValue)
803803
{
804-
int seconds = (int)(Expires.ToLocalTime() - DateTime.Now).TotalSeconds;
804+
int seconds = (int)(Expires.ToUniversalTime() - DateTime.UtcNow).TotalSeconds;
805805
if (seconds < 0)
806806
{
807807
// This means that the cookie has already expired. Set Max-Age to 0

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ public static void Expired_GetSet_Success()
116116
Cookie c = new Cookie();
117117
Assert.False(c.Expired);
118118

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

122-
c.Expires = DateTime.Now.AddDays(1);
122+
c.Expires = DateTime.UtcNow.AddDays(1);
123123
Assert.False(c.Expired);
124124

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

138-
DateTime dt = DateTime.Now;
138+
DateTime dt = DateTime.UtcNow;
139139
c.Expires = dt;
140140
Assert.Equal(dt, c.Expires);
141141
}
@@ -226,7 +226,9 @@ public static void Secure_GetSet_Success()
226226
[Fact]
227227
public static void Timestamp_GetSet_Success()
228228
{
229-
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
229+
//DateTime.UtcNow changes as the test runs
230+
DateTime dt = DateTime.UtcNow;
231+
dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
230232
Cookie c = new Cookie();
231233
Assert.True(c.TimeStamp >= dt);
232234
}

0 commit comments

Comments
 (0)