Skip to content

Add 'Minutes' type that represents minutes of time #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Sources/TaggedTime/TaggedTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ public enum SecondsTag {}
/// A type that represents seconds of time.
public typealias Seconds<A> = Tagged<SecondsTag, A>

public enum MinutesTag {}
/// A type that represents minutes of time.
public typealias Minutes<A> = Tagged<MinutesTag, A>

extension Tagged where Tag == MillisecondsTag, RawValue: BinaryFloatingPoint {
/// Converts milliseconds to seconds.
public var seconds: Seconds<RawValue> {
return .init(rawValue: self.rawValue / 1000)
}

/// Converts milliseconds to minutes.
public var minutes: Minutes<RawValue> {
return .init(rawValue: self.rawValue / 1000 / 60)
}

/// Converts milliseconds into `TimeInterval`, which is measured in seconds.
public var timeInterval: TimeInterval {
let seconds = self.seconds.rawValue
Expand Down Expand Up @@ -57,6 +66,11 @@ extension Tagged where Tag == SecondsTag, RawValue: Numeric {
}

extension Tagged where Tag == SecondsTag, RawValue: BinaryInteger {
/// Converts seconds to minutes.
public var minutes: Minutes<RawValue> {
return .init(rawValue: self.rawValue / 60)
}

/// Converts seconds into `TimeInterval`.
public var timeInterval: TimeInterval {
return TimeInterval(Int64(self.rawValue))
Expand All @@ -73,7 +87,46 @@ extension Tagged where Tag == SecondsTag, RawValue: BinaryInteger {
}
}

extension Tagged where Tag == SecondsTag, RawValue: FloatingPoint {
/// Converts seconds to minutes.
public var minutes: Minutes<RawValue> {
return .init(rawValue: self.rawValue / 60)
}
}

extension Tagged where Tag == MinutesTag, RawValue: Numeric {
/// Converts minutes in seconds.
public var seconds: Seconds<RawValue> {
return .init(rawValue: self.rawValue * 60)
}
}

extension Tagged where Tag == MinutesTag, RawValue: BinaryInteger {
/// Converts minutes into `TimeInterval` using seconds.
public var timeInterval: TimeInterval {
return seconds.timeInterval
}

/// Converts seconds into `DispatchTimeInterval` using seconds.
public var dispatchTimeInterval: DispatchTimeInterval {
return seconds.dispatchTimeInterval
}

/// Converts seconds in `Date` using seconds.
public var date: Date {
return seconds.date
}
}

extension Date {
/// Computes the number of minutes between the receiver and another given dte.
///
/// - Parameter date: The date with which to compare the receiver.
/// - Returns: The number of minutes between the receiver and the other date.
public func minutesSince(_ date: Date) -> Minutes<TimeInterval> {
return secondsSince(date).minutes
}

/// Computes the number of seconds between the receiver and another given dte.
///
/// - Parameter date: The date with which to compare the receiver.
Expand Down
57 changes: 52 additions & 5 deletions Tests/TaggedTimeTests/TaggedTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,67 @@ import XCTest
import TaggedTime

final class TaggedTimeTests: XCTestCase {
func testMinutesToSeconds() {
let minutes: Minutes<Int> = 12
XCTAssertEqual(720, minutes.seconds)
}

func testSecondsToMilliseconds() {
let seconds: Seconds<Int> = 12
XCTAssertEqual(12000, seconds.milliseconds)
}

func testSecondsToMinutes() {
let seconds: Seconds<Int> = 120
XCTAssertEqual(2, seconds.minutes)
}

func testMillisecondsToSeconds() {
let milliseconds: Milliseconds<Double> = 12000
XCTAssertEqual(12.0, milliseconds.seconds)
}


func testMillisecondsToMinutes() {
let milliseconds: Milliseconds<Double> = 120000
XCTAssertEqual(2, milliseconds.minutes)
}

func testMillisecondsToTimeInterval() {
let milliseconds: Milliseconds<Int> = 12000
XCTAssertEqual(12.0, milliseconds.timeInterval)
}


func testSecondsToTimeInterval() {
let seconds: Seconds<Int> = 120
XCTAssertEqual(120, seconds.timeInterval)
}

func testMinutesToTimeInterval() {
let minutes: Minutes<Int> = 12
XCTAssertEqual(720, minutes.timeInterval)
}

func testMillisecondsToDate() {
let milliseconds: Milliseconds<Int> = 12000
XCTAssertEqual(Date(timeIntervalSince1970: 12), milliseconds.date)
}


func testSecondsToDate() {
let seconds: Seconds<Int> = 120
XCTAssertEqual(Date(timeIntervalSince1970: 120), seconds.date)
}

func testMinutesToDate() {
let minutes: Minutes<Int> = 12
XCTAssertEqual(Date(timeIntervalSince1970: 720), minutes.date)
}

func testMinutesSince() {
let date1 = Date(timeIntervalSince1970: 5)
let date2 = Date(timeIntervalSince1970: 125)
XCTAssertEqual(2, date2.minutesSince(date1))
}

func testSecondsSince() {
let date1 = Date(timeIntervalSince1970: 12)
let date2 = Date(timeIntervalSince1970: 15)
Expand All @@ -35,6 +76,9 @@ final class TaggedTimeTests: XCTestCase {
}

func testDate() {
let minutes: Minutes<Int> = 2
XCTAssertEqual(Date(timeIntervalSince1970: 120), minutes.date)

let seconds: Seconds<Int> = 12
XCTAssertEqual(Date(timeIntervalSince1970: 12), seconds.date)

Expand All @@ -43,8 +87,11 @@ final class TaggedTimeTests: XCTestCase {
}

func testLossyMillisecondsToSeconds() {
let milliseconds: Milliseconds<Int> = 12500
let milliseconds: Milliseconds<Int> = 120500
let seconds = milliseconds.map(Double.init).seconds.map(Int.init)
XCTAssertEqual(12, seconds)
let minutes = milliseconds.map(Double.init).minutes.map(Int.init)

XCTAssertEqual(120, seconds)
XCTAssertEqual(2, minutes)
}
}