Files
purchases-ios/Sources/Misc/DateAndTime/DateExtensions.swift
T
NachoSoto cb3593d9b7 Refactor: reorganized files in new Security and Misc folders (#2326)
Thanks to @aboedo for the suggestion.

### Changes
- Moved signature verification files and tests to new `Security` folder
- Moved `CacheFetchPolicy` inside `Caching`
- Split files inside `Misc` into subfolders
2023-03-13 11:25:36 -07:00

51 lines
1.4 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Created by Andrés Boedo on 8/7/20.
//
import Foundation
enum DateExtensionsError: Error {
case invalidDateComponents(_ dateComponents: DateComponents)
}
extension DateExtensionsError: CustomStringConvertible {
var description: String {
switch self {
case .invalidDateComponents(let dateComponents):
return "invalid date components: \(dateComponents.description)"
}
}
}
extension Date {
// swiftlint:disable:next function_parameter_count
static func from(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int) throws -> Date {
let calendar = Calendar(identifier: .gregorian)
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = second
guard let date = calendar.date(from: dateComponents) else {
throw DateExtensionsError.invalidDateComponents(dateComponents)
}
return date
}
}