basecamp/local_time

Name: local_time

Owner: Basecamp

Description: Rails engine for cache-friendly, client-side local time

Created: 2013-11-17 21:24:07.0

Updated: 2018-01-18 17:50:33.0

Pushed: 2017-10-20 19:47:08.0

Homepage:

Size: 162

Language: CoffeeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Local Time

Local Time makes it easy to display times and dates to users in their local time. Its Rails helpers render <time> elements in UTC (making them cache friendly), and its JavaScript component immediately converts those elements from UTC to the browser's local time.

Installation
  1. Add gem 'local_time' to your Gemfile.

  2. Include local-time.js in your application's JavaScript bundle.

    Using the asset pipeline:

    require local-time
    

    Using the local-time npm package:

    rt LocalTime from "local-time"
    lTime.start()
    
Example
mment.created_at
, 27 Nov 2013 18:43:22 EST -0500"
rb
local_time(comment.created_at) %>

Renders:

e data-format="%B %e, %Y %l:%M%P"
  data-local="time"
  datetime="2013-11-27T23:43:22Z">November 27, 2013 11:43pm</time>

And is converted client-side to:

e data-format="%B %e, %Y %l:%M%P"
  data-local="time"
  datetime="2013-11-27T23:43:22Z"
  title="November 27, 2013 6:43pm EDT"
  data-localized="true">November 27, 2013 6:43pm</time>

(Line breaks added for readability)

Time and date helpers
local_time(time) %>

Format with a strftime string (default format shown here)

local_time(time, '%B %e, %Y %l:%M%P') %>

Alias for local_time with a month-formatted default

local_date(time, '%B %e, %Y') %>

To set attributes on the time tag, pass a hash as the second argument with a :format key and your attributes.

local_time(time, format: '%B %e, %Y %l:%M%P', class: 'my-time') %>

To use a strftime format already defined in your app, pass a symbol as the format.

local_time(date, :long) %>

I18n.t("time.formats.#{format}"), I18n.t("date.formats.#{format}"), Time::DATE_FORMATS[format], and Date::DATE_FORMATS[format] will be scanned (in that order) for your format.

Note: The included strftime JavaScript implementation is not 100% complete. It supports the following directives: %a %A %b %B %c %d %e %H %I %l %m %M %p %P %S %w %y %Y %Z

Time ago helpers
local_time_ago(time) %>

Displays the relative amount of time passed. With age, the descriptions transition from {quantity of seconds, minutes, or hours} to {date + time} to {date}. The <time> elements are updated every 60 seconds.

Examples (in quotes):

Relative time helpers

Preset time and date formats that vary with age. The available types are date, time-ago, time-or-date, and weekday. Like the local_time helper, :type can be passed a string or in an options hash.

local_relative_time(time, 'weekday') %>
local_relative_time(time, type: 'time-or-date') %>

Available :type options

Configuration

Internationalization (I18n)

Local Time includes a set of default en translations which can be updated directly. Or, you can provide an entirely new set in a different locale:

lTime.config.i18n["es"] = {
te: {
dayNames: [ ? ],
monthNames: [ ? ],
?

me: {
?

tetime: {
?



lTime.config.locale = "es"
Version History

2.0.0 (August 7, 2017)

1.0.3

1.0.2 (February 3, 2015)

1.0.1 (December 3, 2014)

1.0.0 (April 12, 2014)

0.3.0 (February 9, 2014)

0.2.0 (December 10, 2013)

0.1.0 (November 29, 2013)


Build Status

Sauce Test Status


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.