Provides a Time::Span::StringConverter module for parsing and serializing Time::Span objects as strings.
Can be used standalone or with JSON & YAML.
-
Add the dependency to your
shard.yml:dependencies: tssc: github: Sija/tssc.cr
-
Run
shards install
require "tssc"
Time::Span::StringConverter.parse("1 hour, 15 minutes") # => 01:15:00
Time::Span::StringConverter.dump(1.hour + 15.minutes) # => "1 hour, 15 minutes"require "json"
require "yaml"
require "tssc"
record FooWithTimeSpan, ttl : Time::Span? do
include JSON::Serializable
include YAML::Serializable
@[JSON::Field(converter: Time::Span::StringConverter)]
@[YAML::Field(converter: Time::Span::StringConverter)]
@ttl : Time::Span?
end
foo = FooWithTimeSpan.from_yaml("ttl: 1 hour, 15 minutes")
foo.ttl # => 01:15:00Tip
Valid string examples:
1w2d3h4m5s1w 2d 3h 4m 5s1w 2d 3h 4 min 5 sec1 week 2 days 3 hours 4 minutes 5 seconds1 week, 2 days, 3 hours, 4 minutes, 5 seconds
Note
Allowed suffixes:
- week —
w,week(s) - day —
d,day(s) - hour —
h,hour(s) - minute —
m,min,minute(s) - second —
s,sec,second(s) - nanosecond —
ns,nanosecond(s)
- Fork it (https://github.com/Sija/tssc.cr/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
- Sijawusz Pur Rahnama - creator and maintainer