Swiftpack.co - reuschj/TimeKeeper as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by reuschj.
reuschj/TimeKeeper 0.6.0
This package has a utility class called TimeKeeper, which is a convenience class to keep track of the current time and date.
⭐️ 1
🕓 1 week ago
.package(url: "https://github.com/reuschj/TimeKeeper.git", from: "0.6.0")

TimeKeeper

This package has a utility class called TimeKeeper, which is a convenience class to keep track of the current time and date. Behind the scenes, it uses a Timer to get the updated Date, using the current system Calendar to extract time/date components. At the specified time interval, the time will be updated. A TimeKeeper instance can be started with it's own built-in Timer or without to relay on an external Timer.

This package also has a class called TimeEmitter, which can be used with projects using SwiftUI and/or the Combine framework. TimeEmitter is an ObservableObject with a published TimeKeeper.

Basic Usage

If you already have a Timer you want to use, you can simply start your TimeKeeper without parameters. Just tell your timer to call the TimeKeeper's update() method whenever your Timer fires.

let timeKeeper = TimeKeeper()

If you want the TimeKeeper to manage it's own Timer, just give it a TimeInterval:

let timeKeeper = TimeKeeper(updatedEvery: 1.0)

Whenever you need a date component, just call one of the TimeKeeper's methods:

let minute = timeKeeper.minute
let second = timeKeeper.second

Keep in mind that most methods will return an optional value.

TimeEmitter

If your project is using SwiftUI and/or the Combine framework, TimeEmitter is a useful wrapper for TimeKeeper:

let timeEmitter = TimeEmitter(updatedEvery: 1.0)

TimeEmitter is an ObservableObject which creates a published TimeKeeper, which can be accessed with the time property:

let minute = timeEmitter.time.minute
let second = timeEmitter.time.second

GitHub

link
Stars: 1
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

Beta Version 6
1 week ago
  • WatchOS Support

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics