NetTime is a small collection of data types that represent date and time:
- LocalTime: A time of day without relationship to date or timezone.
- LocalDate: A date that represent an entire day in any timezone or calendar.
- LocalDateTime: A time and date independent of timezones and calendars.
- TimeOffset: Amount of hours and minutes difference from a timezone.
- DateTime: A moment in time, expressed in relationship to UTC in Gregorian calendar, in the current era, between 0000AD and 9999AD.
Each type is capable of serializing and deserializing from strings format specified in RFC 3339.
let date = DateTime(staticRFC3339String: "1979-05-27T00:32:00.999999-07:00") // Original representation is preserved. For example: date.time.secondFraction // [9, 9, 9, 9, 9, 9] // Serialize back to RFC 3339 timestamp. date.description // 1979-05-27T00:32:00.999999-07:00 // Use it with Foundation: Foundation.Date(timeIntervalSinceReferenceDate: date.timeIntervalSince2001) Foundation.TimeZone(secondsFromGMT: date.utcOffset.asSeconds)
But why not just ues
Foundation.Date, you ask?
Turns out, time is hard to represent if you account for different calendars and timezones. But sometimes it's legitimate to ignore this problem. That's why RFC 3339 exists: "to improve consistency and interoperability when representing and using date and time in Internet protocols." Suffice to say, consistency and interoperability goes beyond internet protocols.
Further, converting a timestamp to a in-memory object such as
Foundation.Date a destructive operation: the original time representation is
lost. Want to know what timezone offset was used? what the intended precesion
for the fraction of seconds was? Tough luck. NetTime preserves all information
in an RFC 3339-compliant timestamp.
use_frameworks! pod "NetTime"
.package(url: "http://github.com/dduan/NetTime", from: "0.2.2")
Do not use NetTime's data types as direct source for time displayed to end
users. Use something like
Foundation.DateFormatter and follow best practices.
Never assume you know enough about timezones and/or calendars to format date
You may find interesting
- 2019-10-14 03:54:32
0.2.1 - 2019-03-26 06:28:28
Swift 5 Support
0.1.1 - 2019-03-09 05:59:47
All public types are now Codable.
0.1.0 - 2019-03-04 07:56:16
0.0.4 - 2019-02-25 04:54:24
Relax interfaces introduced in 0.0.3.
0.0.3 - 2019-02-23 21:18:05
Include initializers that takes a
[CChar] that was previously internal. These initializers will be useful as part of a larger parser that deals with C strings.
0.0.2 - 2019-02-18 05:50:55
Add ways to calculate number of seconds from Unix epoch.
0.0.1 - 2019-02-17 11:09:55
Initial release. RFC 3339 parsing, validation and serialization is supported and tested.