Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
Updated for Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Jun 4, 2020
1 parent 9b2b384 commit c9eb4fa
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 463 deletions.
301 changes: 131 additions & 170 deletions Sources/SwiftFoundation/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,201 +6,162 @@
// Copyright © 2015 PureSwift. All rights reserved.
//

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin.C
import struct Foundation.Date
#elseif os(Linux)
import Glibc
#endif

#if os(Linux) || XcodeLinux
/// `Date` structs represent a single point in time.
public struct Date: Equatable, Hashable {

/// `Date` structs represent a single point in time.
public struct Date: Equatable, Hashable, Comparable, CustomStringConvertible {

// MARK: - Static Properties and Methods

/// The number of seconds from 1 January 1970 to the reference date, 1 January 2001.
public static let timeIntervalBetween1970AndReferenceDate = 978307200.0

/**
Creates and returns a Date value representing a date in the distant future.

The distant future is in terms of centuries.
*/
public static let distantFuture = Date(timeIntervalSinceReferenceDate: 63113904000.0)

/**
Creates and returns a Date value representing a date in the distant past.

The distant past is in terms of centuries.
*/
public static let distantPast = Date(timeIntervalSinceReferenceDate: -63114076800.0)

/// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
public static var timeIntervalSinceReferenceDate: TimeInterval {

return try! timeval.timeOfDay().timeInterval - Date.timeIntervalBetween1970AndReferenceDate
}

// MARK: - Properties

/// The time interval between the date and the reference date (1 January 2001, GMT).
public var timeIntervalSinceReferenceDate: TimeInterval

/// The time interval between the current date and 1 January 1970, GMT.
public var timeIntervalsince1970: TimeInterval {

get { return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate }

set { timeIntervalSinceReferenceDate = timeIntervalsince1970 - Date.timeIntervalBetween1970AndReferenceDate }
}

/**
The time interval between the date and the current date and time.

If the date is earlier than the current date and time, the this property’s value is negative.

- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public var timeIntervalSinceNow: TimeInterval {
return timeIntervalSinceReferenceDate - Date.timeIntervalSinceReferenceDate
}

/**
The interval between the date object and 00:00:00 UTC on 1 January 1970.

This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970.

- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public var timeIntervalSince1970: TimeInterval {
return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate
}

public var description: String {

return "\(timeIntervalSinceReferenceDate)"
}

public var hashValue: Int {

return timeIntervalSinceReferenceDate.hashValue
}

// MARK: - Initialization

/// Returns a `Date` initialized to the current date and time.
public init() {

self.timeIntervalSinceReferenceDate = Date.timeIntervalSinceReferenceDate
}

/// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.
public init(timeIntervalSinceReferenceDate timeInterval: TimeInterval) {

self.timeIntervalSinceReferenceDate = timeInterval
}

/// Returns a `Date` initialized relative to the current date and time by a given number of seconds.
public init(timeIntervalSinceNow: TimeInterval) {

self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate
}

/// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
public init(timeIntervalSince1970: TimeInterval) {

self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate
}

/**
Returns a `Date` initialized relative to another given date by a given number of seconds.

- Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`.
- Parameter date: The reference date.
*/
public init(timeInterval: TimeInterval, since date: Date) {

self.timeIntervalSinceReferenceDate = date.timeIntervalSinceReferenceDate + timeInterval
}

// MARK: - Methods

/**
Returns the interval between the receiver and another given date.

- Parameter another: The date with which to compare the receiver.

- Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined.

- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public func timeIntervalSince(_ date: Date) -> TimeInterval {

return timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate
}
}
// MARK: - Static Properties and Methods

// MARK: - Operators
/// The number of seconds from 1 January 1970 to the reference date, 1 January 2001.
public static var timeIntervalBetween1970AndReferenceDate: SwiftFoundation.TimeInterval { return 978307200.0 }

public func == (lhs: Date, rhs: Date) -> Bool {

return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate
/**
Creates and returns a Date value representing a date in the distant future.

The distant future is in terms of centuries.
*/
public static var distantFuture: SwiftFoundation.Date { return Date(timeIntervalSinceReferenceDate: 63113904000.0) }

/**
Creates and returns a Date value representing a date in the distant past.

The distant past is in terms of centuries.
*/
public static var distantPast: SwiftFoundation.Date { return Date(timeIntervalSinceReferenceDate: -63114076800.0) }

// MARK: - Properties

/// The time interval between the date and the reference date (1 January 2001, GMT).
public var timeIntervalSinceReferenceDate: TimeInterval

/// The time interval between the current date and 1 January 1970, GMT.
public var timeIntervalsince1970: TimeInterval {
get { return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate }
set { timeIntervalSinceReferenceDate = newValue - Date.timeIntervalBetween1970AndReferenceDate }
}

public func < (lhs: Date, rhs: Date) -> Bool {

return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
/**
The time interval between the date and the current date and time.

If the date is earlier than the current date and time, the this property’s value is negative.

- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public var timeIntervalSinceNow: TimeInterval {
return timeIntervalSinceReferenceDate - Date.timeIntervalSinceReferenceDate
}

public func - (lhs: Date, rhs: Date) -> TimeInterval {

return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
/**
The interval between the date object and 00:00:00 UTC on 1 January 1970.

This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970.

- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public var timeIntervalSince1970: TimeInterval {
return timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate
}

public func + (lhs: Date, rhs: TimeInterval) -> Date {
// MARK: - Initialization

/// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.
public init(timeIntervalSinceReferenceDate timeInterval: TimeInterval) {

return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
self.timeIntervalSinceReferenceDate = timeInterval
}

public func - (lhs: Date, rhs: TimeInterval) -> Date {
/// Returns a `Date` initialized relative to the current date and time by a given number of seconds.
public init(timeIntervalSinceNow: TimeInterval) {

return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
self.timeIntervalSinceReferenceDate = timeIntervalSinceNow + Date.timeIntervalSinceReferenceDate
}

public func += (lhs: inout Date, rhs: TimeInterval) {
/// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
public init(timeIntervalSince1970: TimeInterval) {

lhs = lhs + rhs
self.timeIntervalSinceReferenceDate = timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate
}

public func -= (lhs: inout Date, rhs: TimeInterval) {

lhs = lhs - rhs
/**
Returns a `Date` initialized relative to another given date by a given number of seconds.

- Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`.
- Parameter date: The reference date.
*/
public init(timeInterval: SwiftFoundation.TimeInterval, since date: SwiftFoundation.Date) {

self.timeIntervalSinceReferenceDate = date.timeIntervalSinceReferenceDate + timeInterval
}

// MARK: - Supporting Types
// MARK: - Methods

/// Time interval difference between two dates, in seconds.
public typealias TimeInterval = Double
/**
Returns the interval between the receiver and another given date.

- Parameter another: The date with which to compare the receiver.

- Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined.

- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
public func timeIntervalSince(_ date: SwiftFoundation.Date) -> SwiftFoundation.TimeInterval {
return timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate
}
}

// MARK: - CustomStringConvertible

#endif
extension SwiftFoundation.Date: CustomStringConvertible {

public var description: String {
// TODO: Custom date printing
return timeIntervalSinceReferenceDate.description
}
}

// MARK: - Darwin
// MARK: - Comparable

#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && !XcodeLinux
extension SwiftFoundation.Date: Comparable {

public typealias Date = Foundation.Date
public static func < (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}

public func - (lhs: Date, rhs: Date) -> TimeInterval {

return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
public static func > (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate
}
}

// MARK: - Operators

public func - (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.Date) -> SwiftFoundation.TimeInterval {
return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}

public func + (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) -> SwiftFoundation.Date {

#endif
return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
}

public func - (lhs: SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) -> SwiftFoundation.Date {

return SwiftFoundation.Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
}

public func += (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) {
lhs = lhs + rhs
}

public func -= (lhs: inout SwiftFoundation.Date, rhs: SwiftFoundation.TimeInterval) {
lhs = lhs - rhs
}

// MARK: - Supporting Types

/// Time interval difference between two dates, in seconds.
public typealias TimeInterval = Double
Loading

0 comments on commit c9eb4fa

Please sign in to comment.