DatabaseDateDecodingStrategy
DatabaseDateDecodingStrategy
specifies how FetchableRecord
types that
also adopt the standard Decodable
protocol decode their
Date
properties.
For example:
struct Player: FetchableRecord, Decodable {
static let databaseDateDecodingStrategy = DatabaseDateDecodingStrategy.timeIntervalSince1970
var name: String
var registrationDate: Date // decoded from epoch timestamp
}
-
The strategy that uses formatting from the Date structure.
It decodes numeric values as a number of seconds since Epoch (midnight UTC on January 1st, 1970).
It decodes strings in the following formats, assuming UTC time zone. Missing components are assumed to be zero:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
-
Decodes numeric values as a number of seconds between the date and midnight UTC on 1 January 2001
-
Decodes numeric values as a number of seconds between the date and midnight UTC on 1 January 1970
-
Decodes numeric values as a number of milliseconds between the date and midnight UTC on 1 January 1970
-
Decodes dates according to the ISO 8601 standards
-
Decodes a String, according to the provided formatter
-
Decodes according to the user-provided function.
If the database value does not contain a suitable value, the function must return nil (GRDB will interpret this nil result as a conversion error, and react accordingly).