JustTime is a lightweight Swift library that provides types for working with time and duration independently of dates.
The library consists of three main types: Time, TimeRange and Duration.
The library is almost fully tested with 94.2% code coverage
To add JustTime to your project using Swift Package Manager, add the following dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/ChristianFox/JustTime.git", from: "1.0.0")
]
Don't forget to add JustTime to your target dependencies:
targets: [
.target(
name: "YourTarget",
dependencies: ["JustTime"]),
]
Create a new Time instance:
let time = Time(hour: 14, minute: 30, second: 45) // 14:30:45
let midnight = Time() // 00:00:00
Create a Time instance from a Date:
let now = Date()
let currentTime = try Time(fromDate: now)
Round a Time instance to a number of minutes:
let time = Time(hour: 14, minute: 20)
let newTime = time.roundedToNearest(minutes: 30) // 14:30
Add a TimeInterval to a Time instance:
let time = Time(hour: 14, minute: 30)
let newTime = time.adding(3600) // Adds 1 hour, 15:30
Subtract a TimeInterval from a Time instance:
let time = Time(hour: 14, minute: 30)
let newTime = time.subtracting(3600) // Subtracts 1 hour, 13:30
Calculate the duration between a Time instance and a later time:
let startTime = Time(hour: 9, minute: 0)
let endTime = Time(hour: 17, minute: 0)
let duration = startTime.duration(tillLaterTime: endTime) // Duration of 8 hours
If we call the function with a "later time" which has a lower value than the receiver, the declaration that the argument is later than the receiver is considered to be the truth and therefore timeB
must represent a time on the previous day, the result is then 16 hours.
let timeA = Time(hour: 9, minute: 0)
let timeB = Time(hour: 17, minute: 0)
let duration = timeB(tillLaterTime: timeA) // Duration of 16 hours
Create a new TimeRange instance:
let workHours = TimeRange(start: Time(hour: 9), end: Time(hour: 17))
Check if a Time instance is contained within a TimeRange:
let time = Time(hour: 12)
let isWithinRange = workHours.contains(time) // true
Check if two TimeRange instances overlap:
let lunchBreak = TimeRange(start: Time(hour: 12), end: Time(hour: 13))
let meeting = TimeRange(start: Time(hour: 11, minute: 30), end: Time(hour: 12, minute: 30))
let hasOverlap = lunchBreak.overlaps(meeting) // true
Split a TimeRange into smaller TimeRanges:
let workHours = TimeRange(start: Time(hour: 9), end: Time(hour: 17))
let hourlySlots = workHour.split(by: 60) // An array of 8 TimeRanges
Create a new Duration:
let duration = Duration(hours: 1, minutes: 1, seconds: 1) // 01:01:01
let zero = Duration() // 00:00:00
let normalised = Duration(hours: 0, minutes: 175, seconds: 300) // 03:00:00
Add a Duration to another:
let a = Duration(hours: 1, minutes: 1, seconds: 1)
let b = Duration(hours: 10, minutes: 10, seconds: 10)
let c = a + b // 11:11:11
Subtract a Duration from another:
let a = Duration(hours: 100, minutes: 30, seconds: 20)
let b = Duration(hours: 10, minutes: 10, seconds: 10)
let c = b - a // 90:20:10
Please open an issue for support.
Pull requests are welcome. I welcome developers of all skill levels to help improve the library, fix bugs, or add new features.
For major changes, please open an issue first to discuss what you would like to change.
Before submitting a pull request, please ensure that your code adheres to the existing code style and conventions, and that all tests pass. Additionally, if you're adding new functionality, please make sure to include unit tests to verify the behavior.
If you have any questions or need assistance, feel free to open an issue, and I'll do my best to help you out.
JustTime is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 1 |
Last commit: 4 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics