# Conflicts:
#	samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/DateWithoutTime.swift
This commit is contained in:
Jonas Reichert
2022-09-25 13:06:09 +02:00
parent f9795fe810
commit fa1cc3dbf9
2 changed files with 56 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ import Foundation
/// an appropriate padding in order to transform to GMT+0 which is the assumed timezone in ISO 8601.
/// When decoding, GMT+0 can be assumed (again: ISO8601) so there is no padding necessary and wrappedDate
/// can be used safely.
public struct DateWithoutTime: Codable, Hashable, Equatable {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct DateWithoutTime: Codable, Hashable, Equatable {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let wrappedDate: Date
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let timezone: TimeZone

View File

@@ -0,0 +1,55 @@
// DateWithoutTime.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//
import Foundation
/// Represents a date without time information (e.g. a birthday) for transmission from and to a REST API
///
/// This type is used as a representation for openapi specs `date` format which does not contain
/// time information as opposed to the `date-time` format. Although it internally uses `Date` for
/// (de-)serialization as well the generator needs to be able to distinguish between the two formats.
/// - note: As `Date` is agnostic to timezones (and calendars), timezone information is needed to be able to add
/// an appropriate padding in order to transform to GMT+0 which is the assumed timezone in ISO 8601.
/// When decoding, GMT+0 can be assumed (again: ISO8601) so there is no padding necessary and wrappedDate
/// can be used safely.
internal struct DateWithoutTime: Codable, Hashable, Equatable {
internal let wrappedDate: Date
internal let timezone: TimeZone
internal enum CodingKeys: CodingKey, CaseIterable {
case wrappedDate
case timezone
}
internal init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.wrappedDate = try container.decode(Date.self)
self.timezone = OpenISO8601DateFormatter.withoutTime.timeZone
}
internal init?(wrappedDate: Date?, timezone: TimeZone = .current) {
guard let wrappedDate = wrappedDate else {
return nil
}
self.init(wrappedDate: wrappedDate, timezone: timezone)
}
internal init(wrappedDate: Date, timezone: TimeZone) {
self.wrappedDate = wrappedDate
self.timezone = timezone
}
internal func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(OpenISO8601DateFormatter.withoutTime.string(from: normalizedWrappedDate()))
}
internal func normalizedWrappedDate() -> Date {
return wrappedDate.addingTimeInterval(
Double(timezone.secondsFromGMT(for: wrappedDate)))
}
}