-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathDateFormatTests.cs
More file actions
145 lines (113 loc) · 5.1 KB
/
DateFormatTests.cs
File metadata and controls
145 lines (113 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using NetCoreForce.Client;
using NetCoreForce.Client.Models;
using Xunit;
namespace NetCoreForce.Client.Tests
{
//https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
public class DateFormatTests
{
public class TestObject
{
public DateTimeOffset DateProp { get; set; }
}
readonly DateTimeOffset _dto = new DateTimeOffset(2017, 5, 1, 12, 0, 0, 0, new TimeSpan(-5, 0, 0));
readonly string _expectedDate = "2017-05-01T12:00:00-05:00";
[Fact]
public void SerializedFullDate()
{
TestObject obj = new TestObject() { DateProp = _dto };
string serialized = JsonSerializer.SerializeComplete(obj, false);
Assert.Contains(_expectedDate, serialized);
}
[Fact]
public void FullDateFormatExtension()
{
string convertedDate = _dto.ToSfDateString();
Assert.Equal(_expectedDate, convertedDate);
}
[Fact]
public void FullDateFormat()
{
string convertedDate = _dto.ToString(DateFormats.FullDateFormatString);
Assert.Equal(_expectedDate, convertedDate);
}
[Theory]
[InlineData(DateTimeKind.Utc)]
[InlineData(DateTimeKind.Local)]
[InlineData(DateTimeKind.Unspecified)]
public void FullDateFormat_From_DateTime_Kind(DateTimeKind kind)
{
DateTime dateTimeLocal = new DateTime(2018, 1, 1, 0, 0, 0, kind);
TimeSpan localOffset = TimeZoneInfo.Local.GetUtcOffset(dateTimeLocal);
if(kind == DateTimeKind.Utc)
{
// for UTC DateTime, the offset is always zero
localOffset = TimeSpan.Zero;
}
string convertedDate = DateFormats.FullDateString(dateTimeLocal);
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
string timeSpanFormat = GetTimeSpanFormat(localOffset);
string expectedConvertedDate = $"2018-01-01T00:00:00{localOffset.ToString(timeSpanFormat)}";
Assert.Equal(expectedConvertedDate, convertedDate);
}
private string GetTimeSpanFormat(TimeSpan ts)
{
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
return (ts < TimeSpan.Zero ? "\\-" : "\\+") + "hh\\:mm";
}
[Theory]
[InlineData("America/New_York")]
[InlineData("America/Phoenix")]
[InlineData("Europe/London")]
[InlineData("Asia/Tokyo")]
[InlineData("Asia/Kathmandu")] // Nepal Time (UTC+5:45)
[InlineData("Pacific/Auckland")]
[InlineData("Europe/Moscow")]
[InlineData("Asia/Shanghai")]
public void FullDateFormat_From_Other_Timezone(string timeZoneId)
{
using (new LocalTimeZoneInfoMocker(TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)))
{
DateTime dateTimeUnspecified = new DateTime(2018, 1, 1, 0, 0, 0);
// check that the DateTimeKind is Unspecified
Assert.Equal(DateTimeKind.Unspecified, dateTimeUnspecified.Kind);
TimeSpan localOffset = TimeZoneInfo.Local.GetUtcOffset(dateTimeUnspecified);
string convertedDate = DateFormats.FullDateString(dateTimeUnspecified);
// get timespan formatting to end up with e.g. "2018-01-01T00:00:00-05:00"
string timeSpanFormat = GetTimeSpanFormat(localOffset);
string expectedConvertedDate = $"2018-01-01T00:00:00{localOffset.ToString(timeSpanFormat)}";
Assert.Equal(expectedConvertedDate, convertedDate);
}
}
[Fact]
public void DateTimeAndDateTimeOffset()
{
DateTimeOffset dto = DateTimeOffset.Now;
DateTime dt = DateTime.Now;
DateTimeOffset dtoConverted = dt;
string convertedDateTimeOffset = dto.ToString(DateFormats.FullDateFormatString);
string convertedDateTime = dt.ToString(DateFormats.FullDateFormatString);
string convertedDateTimeOffsetConverted = dt.ToString(DateFormats.FullDateFormatString);
Assert.Equal(convertedDateTimeOffset, convertedDateTime);
Assert.Equal(convertedDateTimeOffset, convertedDateTimeOffsetConverted);
}
[Fact]
public void AccessTokenTimetampConversionSeconds()
{
AccessTokenResponse token = new AccessTokenResponse();
token.IssuedAt = 1530216542;
var expected = new DateTime(2018, 6, 28, 20, 9, 2, DateTimeKind.Utc);
Assert.Equal(expected, token.IssuedAtDateTime);
}
[Fact]
public void AccessTokenTimetampConversionMilliseconds()
{
AccessTokenResponse token = new AccessTokenResponse();
token.IssuedAt = 1537283732843;
var expected = new DateTime(2018, 9, 18, 15, 15, 32, 843, DateTimeKind.Utc);
Assert.Equal(expected, token.IssuedAtDateTime);
}
}
}