@@ -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 ]
0 commit comments