countdown
Version, currently 0.1.23 versions
- 0.1.2latestAug 25, 2024
- 0.1.1not indexedJun 22, 2026
- 0.1.0not indexedJun 22, 2026
github.com/mamantoha/countdown-crystal
A simple Crystal library for calculating and producing an accurate, intuitive description of the timespan between two Time instances.
Installation
# Add this to your shard.yml
dependencies:
countdown:
github: mamantoha/countdown-crystal
version: ~> 0.1.2Then run:
shards installshard.yml
- Crystal
>= 1.0.0- License
- MIT
- Author
- Anton Maminov
Dependencies
This version declares no dependencies.
README
Countdown
A simple Crystal library for calculating and producing an accurate, intuitive description of the timespan between two Time instances.
Installation
-
Add the dependency to your
shard.yml:dependencies: countdown: github: mamantoha/countdown-crystal -
Run
shards install
Features
- Handles leap years, varying month lengths, timezone differences.
- Generates human-readable strings representing the timespan.
- Provides easy access to individual components (years, months, days, hours, minutes, seconds) of the timespan.
Usage
require "countdown"
start_time = Time.local(2023, 12, 31, 23, 59)
end_time = Time.local(2024, 2, 29, 12, 30)
countdown = Countdown.new(start_time, end_time)
puts countdown.components # => {years: 0, months: 1, days: 28, hours: 12, minutes: 31, seconds: 0}
puts countdown.to_s # => "1 month, 28 days, 12 hours and 31 minutes"
require "countdown"
Time::Location.local = Time::Location.load("Europe/Kyiv")
start_time = Time.local(2022, 3, 27, 2, 59)
# => 2022-03-27 02:59:00.0 +02:00 Local
end_time = Time.local(2022, 3, 28, 3, 0)
# => 2022-03-28 03:00:00.0 +03:00 Local
countdown = Countdown.new(start_time, end_time)
countdown.components
# => {years: 0, months: 0, days: 1, hours: 1, minutes: 1, seconds: 0}
countdown.to_s
# => "1 day, 1 hour and 1 minute"
countdown.to_s(oxford_comma: true)
# => "1 day, 1 hour, and 1 minute"
The Algorithm
The Countdown library calculates the difference between two dates in a way that feels natural and consistent. It treats "a month from now" as "the same date next month," ensuring smooth, predictable countdowns without sudden jumps.
The library works like basic subtraction, adjusting for time units (seconds, minutes, hours, days, months, years) as needed. Since months have different numbers of days, Countdown carefully tracks each month to provide accurate results.
Time Zones & Daylight Saving Time
Countdown also handles time zone differences and daylight saving time changes, ensuring that your countdowns are accurate regardless of the time zone or DST shifts between the start and end dates.
Inspiration
This library was inspired by Countdown.js. While the idea was taken from Countdown.js, no code was reused, and this implementation was developed entirely from scratch.
Contributing
- Fork it (https://github.com/mamantoha/countdown-crystal/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Anton Maminov - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.2- Tagged
- Aug 25, 2024
- Commit
a06a11a6c113- Crystal
>= 1.0.0- Indexed
- yes
Dependents
Repository
github.com/mamantoha/countdown-crystal
Metadata
- Created
- Aug 12, 2026
- Updated
- Sep 24, 2026
- Synced
- Sep 24, 2026
- Versions
- 3