Skip to content

Commit c584af0

Browse files
committed
README
1 parent 9790cea commit c584af0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ let user = try KeyValueDecoder().decode(User.self, from: [["id": 1, "name": "Her
5757
let ascii = try KeyValueDecoder().decode([UInt8].self, from: [10, 100, 1000])
5858
```
5959

60+
61+
## Date Encoding/Decoding Strategy
62+
63+
The encoding of `Date` can be adjusted by setting the strategy.
64+
65+
By default `Date` instances are encoded by simply casting to `Any` but this adjusted by setting the strategy.
66+
67+
The default strategy casts to `Any` leaving the instance unchanged:
68+
69+
```swift
70+
var encoder = KeyValueEncoder()
71+
encoder.dateEncodingStrategy = .date
72+
73+
// Date()
74+
let any = try encoder.encode(Date())
75+
```
76+
77+
ISO8601 compatible strings can be used:
78+
79+
```swift
80+
encoder.dateEncodingStrategy = .iso8601()
81+
82+
// "1970-01-01T00:00:00Z"
83+
let any = try encoder.encode(Date(timeIntervalSince1970: 0))
84+
```
85+
86+
Epochs are also supported using `.secondsSince1970` and `millisecondsSince1970`.
87+
6088
## Nil Encoding/Decoding Strategy
6189

6290
The encoding of `Optional.none` can be adjusted by setting the strategy.

0 commit comments

Comments
 (0)