Skip to content

Commit e108e93

Browse files
committed
test: updated TZ mocker, add date formatting tests for multiple TZs
1 parent f53fb7b commit e108e93

2 files changed

Lines changed: 70 additions & 19 deletions

File tree

src/NetCoreForce.Client.Tests/DateFormatTests.cs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ namespace NetCoreForce.Client.Tests
99
//https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
1010
public class DateFormatTests
1111
{
12+
1213
public class TestObject
1314
{
1415
public DateTimeOffset DateProp { get; set; }
1516
}
1617

17-
readonly DateTimeOffset _dto = new DateTimeOffset(2017,5,1,12,0,0,0,new TimeSpan(-5,0,0));
18+
readonly DateTimeOffset _dto = new DateTimeOffset(2017, 5, 1, 12, 0, 0, 0, new TimeSpan(-5, 0, 0));
1819
readonly string _expectedDate = "2017-05-01T12:00:00-05:00";
1920

2021
[Fact]
2122
public void SerializedFullDate()
2223
{
2324
TestObject obj = new TestObject() { DateProp = _dto };
2425

25-
string serialized = JsonSerializer.SerializeComplete(obj, false);
26+
string serialized = JsonSerializer.SerializeComplete(obj, false);
2627

2728
Assert.Contains(_expectedDate, serialized);
2829
}
@@ -43,16 +44,65 @@ public void FullDateFormat()
4344
Assert.Equal(_expectedDate, convertedDate);
4445
}
4546

46-
[Fact]
47-
public void FullDateFormatFromDateTime()
47+
[Theory]
48+
[InlineData(DateTimeKind.Utc)]
49+
[InlineData(DateTimeKind.Local)]
50+
[InlineData(DateTimeKind.Unspecified)]
51+
public void FullDateFormat_From_DateTime_Kind(DateTimeKind kind)
4852
{
49-
var dt = new DateTime(2018,1,1,0,0,0, DateTimeKind.Utc);
53+
DateTime dateTimeLocal = new DateTime(2018, 1, 1, 0, 0, 0, kind);
54+
55+
TimeSpan localOffset = TimeZoneInfo.Local.GetUtcOffset(dateTimeLocal);
56+
57+
if(kind == DateTimeKind.Utc)
58+
{
59+
// for UTC DateTime, the offset is always zero
60+
localOffset = TimeSpan.Zero;
61+
}
62+
63+
string convertedDate = DateFormats.FullDateString(dateTimeLocal);
64+
65+
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
66+
string timeSpanFormat = GetTimeSpanFormat(localOffset);
67+
string expectedConvertedDate = $"2018-01-01T00:00:00{localOffset.ToString(timeSpanFormat)}";
68+
69+
Assert.Equal(expectedConvertedDate, convertedDate);
70+
}
71+
72+
private string GetTimeSpanFormat(TimeSpan ts)
73+
{
74+
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
75+
return (ts < TimeSpan.Zero ? "\\-" : "\\+") + "hh\\:mm";
76+
}
77+
78+
[Theory]
79+
[InlineData("America/New_York")]
80+
[InlineData("America/Phoenix")]
81+
[InlineData("Europe/London")]
82+
[InlineData("Asia/Tokyo")]
83+
[InlineData("Asia/Kathmandu")] // Nepal Time (UTC+5:45)
84+
[InlineData("Pacific/Auckland")]
85+
[InlineData("Europe/Moscow")]
86+
[InlineData("Asia/Shanghai")]
87+
public void FullDateFormat_From_Other_Timezone(string timeZoneId)
88+
{
89+
using (new LocalTimeZoneInfoMocker(TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)))
90+
{
91+
DateTime dateTimeUnspecified = new DateTime(2018, 1, 1, 0, 0, 0);
92+
93+
// check that the DateTimeKind is Unspecified
94+
Assert.Equal(DateTimeKind.Unspecified, dateTimeUnspecified.Kind);
95+
96+
TimeSpan localOffset = TimeZoneInfo.Local.GetUtcOffset(dateTimeUnspecified);
5097

51-
string convertedDate = DateFormats.FullDateString(dt);
98+
string convertedDate = DateFormats.FullDateString(dateTimeUnspecified);
5299

53-
string expected = "2018-01-01T00:00:00+00:00";
100+
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
101+
string timeSpanFormat = GetTimeSpanFormat(localOffset);
102+
string expectedConvertedDate = $"2018-01-01T00:00:00{localOffset.ToString(timeSpanFormat)}";
54103

55-
Assert.Equal(expected, convertedDate);
104+
Assert.Equal(expectedConvertedDate, convertedDate);
105+
}
56106
}
57107

58108
[Fact]

src/NetCoreForce.Client.Tests/TimeZoneInfoMocker.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ namespace NetCoreForce.Client.Tests
55
{
66
public class LocalTimeZoneInfoMocker : IDisposable
77
{
8+
private readonly TimeZoneInfo _actualLocalTimeZoneInfo;
9+
810
public LocalTimeZoneInfoMocker(TimeZoneInfo mockTimeZoneInfo)
911
{
10-
var info = typeof(TimeZoneInfo).GetField(
11-
"s_cachedData",
12-
BindingFlags.NonPublic | BindingFlags.Static
13-
);
14-
15-
var cachedData = info.GetValue(null);
12+
_actualLocalTimeZoneInfo = TimeZoneInfo.Local;
13+
SetLocalTimeZone(mockTimeZoneInfo);
14+
}
1615

17-
var field = cachedData.GetType().GetField(
18-
"_localTimeZone",
19-
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance
20-
);
16+
private static void SetLocalTimeZone(TimeZoneInfo timeZoneInfo)
17+
{
18+
var info = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
19+
object cachedData = info.GetValue(null);
2120

22-
field.SetValue(cachedData, mockTimeZoneInfo);
21+
var field = cachedData.GetType().GetField("_localTimeZone", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance);
22+
field.SetValue(cachedData, timeZoneInfo);
2323
}
2424

2525
public void Dispose()
2626
{
2727
TimeZoneInfo.ClearCachedData();
28+
SetLocalTimeZone(_actualLocalTimeZoneInfo);
2829
}
2930
}
3031
}

0 commit comments

Comments
 (0)