This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align TimeInterval formatter with modern FormatStyle protocol
- Loading branch information
Showing
6 changed files
with
72 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// CustomTimeIntervalFormatStyle.swift | ||
// Aware | ||
// | ||
// Created by Joshua Peek on 02/12/24. | ||
// Copyright © 2024 Joshua Peek. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CustomTimeIntervalFormatStyle: FormatStyle { | ||
/// Formats a time interval as a human readable duration string. | ||
/// - Parameter value: The time interval to format. | ||
/// - Returns: A string representation of the time interval. | ||
func format(_ value: TimeInterval) -> String { | ||
let minutes = Int(value) / 60 | ||
if minutes < 60 { | ||
return "\(minutes)m" | ||
} else { | ||
return "\(minutes / 60)h \(minutes % 60)m" | ||
} | ||
} | ||
} | ||
|
||
@available(macOS 12.0, *) | ||
extension FormatStyle where Self == CustomTimeIntervalFormatStyle { | ||
static var custom: CustomTimeIntervalFormatStyle { .init() } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import XCTest | ||
|
||
@testable import Aware | ||
|
||
class CustomTimeIntervalFormatStyleTests: XCTestCase { | ||
func testFormat() { | ||
let formatter = CustomTimeIntervalFormatStyle() | ||
|
||
XCTAssertEqual(formatter.format(0), "0m") | ||
XCTAssertEqual(formatter.format(1), "0m") | ||
XCTAssertEqual(formatter.format(30), "0m") | ||
XCTAssertEqual(formatter.format(59), "0m") | ||
|
||
XCTAssertEqual(formatter.format(60), "1m") | ||
XCTAssertEqual(formatter.format(61), "1m") | ||
XCTAssertEqual(formatter.format(119), "1m") | ||
|
||
XCTAssertEqual(formatter.format(120), "2m") | ||
XCTAssertEqual(formatter.format(300), "5m") | ||
XCTAssertEqual(formatter.format(900), "15m") | ||
XCTAssertEqual(formatter.format(1800), "30m") | ||
XCTAssertEqual(formatter.format(2700), "45m") | ||
XCTAssertEqual(formatter.format(3540), "59m") | ||
XCTAssertEqual(formatter.format(3599), "59m") | ||
|
||
XCTAssertEqual(formatter.format(3600), "1h 0m") | ||
XCTAssertEqual(formatter.format(3601), "1h 0m") | ||
XCTAssertEqual(formatter.format(3660), "1h 1m") | ||
XCTAssertEqual(formatter.format(4500), "1h 15m") | ||
XCTAssertEqual(formatter.format(5400), "1h 30m") | ||
XCTAssertEqual(formatter.format(6300), "1h 45m") | ||
|
||
XCTAssertEqual(formatter.format(7200), "2h 0m") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.