Time::Duration
The Time::Duration
library provides a structured and convenient way to work with durations of time in the Crystal programming language. It allows the creation of duration instances from various units of time such as seconds, minutes, hours, days, weeks, months, and years, and also supports duration operations like addition, subtraction, multiplication, and division.
This library is inspired by the ActiveSupport::Duration library from the Ruby on Rails framework.
Installation
-
Add the dependency to your
shard.yml
:dependencies: time_duration: github: mamantoha/time_duration
-
Run
shards install
Usage
require "time_duration"
Initialization
Initialize a duration from a float value representing seconds:
duration = Time::Duration.new(3600.0) # Creates a duration of 1 hour.
You can also create durations directly from various units of time:
hour = Time::Duration.hours(1) # Creates a duration of 1 hour.
day = Time::Duration.days(1) # Creates a duration of 1 day.
week = Time::Duration.weeks(1) # Creates a duration of 1 week.
month = Time::Duration.months(1) # Creates a duration of 1 month.
year = Time::Duration.years(1) # Creates a duration of 1 year.
Operations
The library supports standard mathematical operations for durations:
total = day + hour # Addition
difference = day - hour # Subtraction
doubled = day * 2 # Multiplication
halved = day / 2 # Division
You can also compare two durations:
if day > hour
puts "A day is longer than an hour."
end
Conversion
You can convert a duration into various units of time:
puts "In seconds: #{hour.in_seconds}"
puts "In minutes: #{hour.in_minutes}"
puts "In hours: #{hour.in_hours}"
puts "In days: #{day.in_days}"
puts "In weeks: #{week.in_weeks}"
puts "In months: #{month.in_months}"
puts "In years: #{year.in_years}"
Time
Calculate a time in the past or future based on a duration:
duration = Time::Duration.hours(1)
puts duration.ago # prints the time 1 hour ago
puts duration.since # prints the time 1 hour in the future
You can also use it with a specific time:
specific_time = Time.new(2023, 1, 1)
puts duration.ago(specific_time) # prints the time 1 hour before the specific_time
puts duration.since(specific_time) # prints the time 1 hour after the specific_time
Contributing
- Fork it (https://github.com/mamantoha/time_duration/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