Skip to content

Format time and dates in natural language - time.like "It was sunday and the clocks were striking 11"

Notifications You must be signed in to change notification settings

mdemare/like_1999

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

like_1999

DESCRIPTION:

Specify date/time formats via dates and times, instead of opaque strftime codes.

Time.now.like('31 december in the year 1999')
=> "17 december in the year 2018"

Note that strftime does not handle localization. To handle localization, you should put the format string for various languages in a locale yaml file, and use something like ‘Time.now.like(t(’datetime.long’)) to convert the datetimes.

SYNOPSIS:

Uses a natural format to specify a date/time format, by using a reference date/time. This canonical, easy to remember timestamp is Sunday, December 31 1999, 23:59:00 UTC, or Sun 12-31-99 11:59:00 PM. Also known as the last second of the previous century. Capitalization of months/weekdays will match that in template. Encoding should be UTF-8. Capitalization of accented letters may not work. The following subset of strftime is supported:

  • Year long (1999)

  • Year short (99)

  • Month long (december)

  • Month short (dec)

  • Month numeric (12)

  • Yearday (366)

  • Date (31)

  • Weekday long (Sunday)

  • Weekday short (Sun)

  • Weekday very short (Su)

  • Hour, 24-hour clock (23)

  • Hour, 12-hour clock (11)

  • AM/PM (pm)

  • Minutes (59)

  • Seconds (00)

  • Timezone (UTC)

Unsupported (but you may use strftime % codes)

  • milliseconds

  • weekday (numeric)

  • week number

[List of strftime codes](ruby-doc.org/core-2.5.3/Time.html#method-i-strftime)

It’s possible to specify the format string in the following languages, although we recommend you just use English. Supported languages are: :en (english), :nl (Dutch), :fr (French), :de (German). Spanish(:es), Italian(:it) and Swedish(:se) are not yet fully supported. The default language can be set using Time.set_default_template_language. Examples for English:

require 'like_1999'

t = Time.now
=> 2018-12-17 11:09:11 +0100

t.like("Sunday DECEMBER 31, 11:59 PM")
=> "Monday DECEMBER 17, 11:09 AM"

t.like("Sun 31 dec")
=> "Mon 17 dec"

t.like("12-31-99")    
=> "12-17-18"

t.like("Next sunday at 11 o'clock")    
=> "Next monday at 11 o'clock"

t.like("Sun 12-31-99 11:59 PM UTC")
=> "Mon 12-17-18 11:09 AM CET"

Examples for Dutch:

Time.set_default_template_language(:nl)
t = Time.now
t.like("Zondag 31 DECEMBER 23:59")
t.like("Zo 31 dec")
t.like("31-12-99")
t.like("Zondag om 23 uur 59")
t.like("December, 1999")

Examples for French:

Time.set_default_template_language(:fr)
t = Time.now
t.like("Dimanche 31 DÉCEMBRE 23:59")
t.like("Di 31 déc")

Examples for German:

Time.set_default_template_language(:de)
t = Time.now
t.like("Sonntag 31 DEZEMBER 23:59")
t.like("So 31 dez")

INSTALL:

gem install like_1999

LICENSE:

(The MIT License)

Copyright © 2012-2018 Michiel de Mare

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Format time and dates in natural language - time.like "It was sunday and the clocks were striking 11"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages