@@ -3,51 +3,51 @@ import Testing
33@testable import RFC_4287
44
55@Suite
6- struct `Date Formatting Tests` {
7- @Test func dateToAtomString( ) async throws {
8- let date = Date ( timeIntervalSince1970: 1_609_459_200 ) // 2021-01-01 00:00:00 UTC
9-
10- let formatted = date. atomFormatted ( )
11-
12- // Should be ISO 8601 format
13- #expect( formatted. contains ( " 2021-01-01 " ) )
14- #expect( formatted. contains ( " T " ) )
15- #expect( formatted. contains ( " Z " ) )
6+ struct `RFC 3339 DateTime Tests` {
7+ @Test func createDateTimeFromComponents( ) async throws {
8+ let time = try Time ( year: 2021 , month: 1 , day: 1 , hour: 0 , minute: 0 , second: 0 )
9+ let dateTime = RFC_3339 . DateTime ( time: time, offset: . utc)
10+
11+ #expect( dateTime. time. year == 2021 )
12+ #expect( dateTime. time. month == 1 )
13+ #expect( dateTime. time. day == 1 )
14+ #expect( dateTime. offset == . utc)
1615 }
1716
18- @Test func atomStringToDate ( ) async throws {
17+ @Test func parseRFC3339String ( ) async throws {
1918 let dateString = " 2021-01-01T00:00:00Z "
19+ let dateTime = try RFC_3339 . Parser. parse ( dateString)
2020
21- let date = try Date ( atomString: dateString)
22-
23- #expect( date. timeIntervalSince1970 == 1_609_459_200 )
21+ #expect( dateTime. time. year == 2021 )
22+ #expect( dateTime. time. month == 1 )
23+ #expect( dateTime. time. day == 1 )
24+ #expect( dateTime. offset == . utc)
2425 }
2526
26- @Test func atomStringWithFractionalSeconds( ) async throws {
27- let dateString = " 2021-01-01T00:00:00.123Z "
27+ @Test func formatRFC3339String( ) async throws {
28+ let time = try Time ( year: 2021 , month: 1 , day: 1 , hour: 12 , minute: 30 , second: 45 )
29+ let dateTime = RFC_3339 . DateTime ( time: time, offset: . utc)
2830
29- let date = try Date ( atomString : dateString )
31+ let formatted = RFC_3339 . Formatter . format ( dateTime )
3032
31- #expect( date. timeIntervalSince1970 > 0 )
33+ // Should be ISO 8601 format
34+ #expect( formatted. contains ( " 2021-01-01 " ) )
35+ #expect( formatted. contains ( " T " ) )
36+ #expect( formatted. contains ( " 12:30:45 " ) )
3237 }
3338
34- @Test func roundTripDateFormatting( ) async throws {
35- let originalDate = Date ( timeIntervalSince1970: 1_609_459_200 )
36-
37- let formatted = originalDate. atomFormatted ( )
38- let parsedDate = try Date ( atomString: formatted)
39+ @Test func roundTripDateTimeFormatting( ) async throws {
40+ let time = try Time ( year: 2021 , month: 6 , day: 15 , hour: 14 , minute: 30 , second: 0 )
41+ let original = RFC_3339 . DateTime ( time: time, offset: . utc)
3942
40- // Should be equal within a second (accounting for fractional seconds)
41- let difference = abs ( originalDate. timeIntervalSince1970 - parsedDate. timeIntervalSince1970)
42- #expect( difference < 1.0 )
43- }
44-
45- @Test func invalidDateStringThrows( ) async throws {
46- let invalidString = " not a date "
43+ let formatted = RFC_3339 . Formatter. format ( original)
44+ let parsed = try RFC_3339 . Parser. parse ( formatted)
4745
48- #expect( throws: RFC_4287 . ValidationError. self) {
49- try Date ( atomString: invalidString)
50- }
46+ #expect( parsed. time. year == original. time. year)
47+ #expect( parsed. time. month == original. time. month)
48+ #expect( parsed. time. day == original. time. day)
49+ #expect( parsed. time. hour == original. time. hour)
50+ #expect( parsed. time. minute == original. time. minute)
5151 }
5252
5353 @Test func variousRFC3339Formats( ) async throws {
@@ -59,8 +59,8 @@ struct `Date Formatting Tests` {
5959 ]
6060
6161 for format in formats {
62- let date = try Date ( atomString : format)
63- #expect( date . timeIntervalSince1970 > 0 )
62+ let dateTime = try RFC_3339 . Parser . parse ( format)
63+ #expect( dateTime . time . year > 0 )
6464 }
6565 }
6666}
0 commit comments