A Swift package providing parsers for RFC 2822, RFC 5322 date formats, and Unix epoch timestamps, built on top of swift-parsing.
- RFC 2822 Date Parser: Parse and format dates according to RFC 2822 specification
- RFC 5322 Date Parser: Parse and format dates according to RFC 5322 specification
- Unix Epoch Parsing: Parse Unix timestamps (integer and floating-point) to Date objects
- Type-safe parsing: Built on swift-parsing for composable, type-safe date parsing
- Bidirectional conversion: Both parsing and printing capabilities
- Comprehensive error handling: Clear error messages for invalid date formats
Add swift-date-parsing
to your Swift package dependencies in Package.swift
:
dependencies: [
.package(url: "https://github.com/coenttb/swift-date-parsing.git", from: "0.1.0")
]
import DateParsing
let parser = RFC_2822.Date.Parser()
// Parse an RFC 2822 date string
let dateString = "Mon, 01 Jan 2024 12:00:00 GMT"
let date = try parser.parse(dateString[...])
// Format a Date as RFC 2822 string
let formattedString = try parser.print(date)
import DateParsing
let parser = RFC_5322.Date.Parser()
// Parse an RFC 5322 date string
let dateString = "Mon, 01 Jan 2024 12:00:00 +0000"
let date = try parser.parse(dateString[...])
// Format a Date as RFC 5322 string
let formattedString = try parser.print(date)
import UnixEpochParsing
let parser = Date.UnixEpoch.Parser()
// Parse Unix timestamp to Date
let timestamp = "1234567890"
let date = try parser.parse(timestamp[...])
// Parse floating-point timestamp
let floatTimestamp = "1234567890.5"
let preciseDate = try parser.parse(floatTimestamp[...])
// Parse negative timestamp (pre-1970)
let negativeTimestamp = "-86400"
let preEpochDate = try parser.parse(negativeTimestamp[...])
// Convert Date back to Unix timestamp string
let timestampString = try parser.print(date)
- Standard format:
Mon, 01 Jan 2024 12:00:00 GMT
- With timezone offsets:
Tue, 15 Mar 2024 14:30:15 +0100
- Various timezone abbreviations (GMT, UTC, EST, PST, etc.)
- Standard format:
Mon, 01 Jan 2024 12:00:00 +0000
- Timezone offsets:
+0000
,+0100
,-0500
, etc. - Full weekday and month names supported
- Integer timestamps:
1234567890
- Floating-point timestamps:
1234567890.5
- Negative timestamps (pre-1970):
-86400
- Large timestamps:
9223372036854775807
- Zero (Unix epoch start):
0
- Swift 6.0+
- macOS 13.0+ / iOS 16.0+
This package depends on:
- swift-parsing - Composable parsing library
- swift-rfc-2822 - RFC 2822 implementations
- swift-rfc-5322 - RFC 5322 implementations
- swift-html: A Swift DSL for type-safe HTML & CSS
- coenttb-web: Web development tools and utilities
- coenttb-server: Server development framework
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
If you're working on your own Swift project involving date parsing, feel free to learn, fork, and contribute.
Got thoughts? Found something you love? Something you hate? Let me know! Your feedback helps make this project better for everyone. Open an issue or start a discussion—I'm all ears.