Skip to content
This repository was archived by the owner on Oct 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.3
2.6.6
2 changes: 1 addition & 1 deletion app/assets/javascripts/local-time.js

Large diffs are not rendered by default.

67 changes: 14 additions & 53 deletions app/helpers/local_time_helper.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
module LocalTimeHelper
def local_time(time, options = nil)
time = utc_time(time)
def local_date(time_or_date, format: :default, **options)
# we ensure to fallback to :default
return options[:default] unless time_or_date

options, format = extract_options_and_value(options, :format)
format = find_time_format(format)
# we ensure the format will be of correct type
# we ensure we have an UTC formatted time
# we ensure there is at least a formatted value (if javascript fails)
# we ensure to fallback to :default
value = ::I18n.l(time_or_date, format: format, default: options[:default])
type = time_or_date.is_a?(Date) ? :date : :time
time = utc_time(time_or_date)

options[:data] ||= {}
options[:data].merge! local: :time, format: format

time_tag time, time.strftime(format), options
end

def local_date(time, options = nil)
options, format = extract_options_and_value(options, :format)
options[:format] = format || LocalTime.default_date_format
local_time time, options
end

def local_relative_time(time, options = nil)
time = utc_time(time)
options, type = extract_options_and_value(options, :type)
format = ::I18n.get_date_format(type: type, format: format)

options[:data] ||= {}
options[:data].merge! local: type
options[:data].merge! local: type, format: format

time_tag time, time.strftime(LocalTime.default_time_format), options
time_tag time, value, options
end

def local_time_ago(time, options = nil)
options, * = extract_options_and_value(options, :type)
options[:type] = 'time-ago'
local_relative_time time, options
end
alias_method :local_time, :local_date

def utc_time(time_or_date)
if time_or_date.respond_to?(:in_time_zone)
Expand All @@ -40,31 +28,4 @@ def utc_time(time_or_date)
time_or_date.to_time.utc
end
end

private
def find_time_format(format)
if format.is_a?(Symbol)
if (i18n_format = I18n.t("time.formats.#{format}", default: [:"date.formats.#{format}", ''])).present?
i18n_format
elsif (date_format = Time::DATE_FORMATS[format] || Date::DATE_FORMATS[format])
date_format.is_a?(Proc) ? LocalTime.default_time_format : date_format
else
LocalTime.default_time_format
end
else
format.presence || LocalTime.default_time_format
end
end

def extract_options_and_value(options, value_key = nil)
case options
when Hash
value = options.delete(value_key)
[ options, value ]
when NilClass
[ {} ]
else
[ {}, options ]
end
end
end
10 changes: 6 additions & 4 deletions lib/assets/javascripts/src/local-time/config/i18n.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
LocalTime.config.i18n =
en:
date:
dayNames: [
day_names: [
"Sunday"
"Monday"
"Tuesday"
Expand All @@ -10,7 +10,7 @@ LocalTime.config.i18n =
"Friday"
"Saturday"
]
abbrDayNames: [
abbr_day_names: [
"Sun"
"Mon"
"Tue"
Expand All @@ -19,7 +19,8 @@ LocalTime.config.i18n =
"Fri"
"Sat"
]
monthNames: [
month_names: [
null
"January"
"February"
"March"
Expand All @@ -33,7 +34,8 @@ LocalTime.config.i18n =
"November"
"December"
]
abbrMonthNames: [
abbr_month_names: [
null
"Jan"
"Feb"
"Mar"
Expand Down
6 changes: 3 additions & 3 deletions lib/assets/javascripts/src/local-time/controller.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#= require ./relative_time
#= require ./page_observer

{parseDate, strftime, getI18nValue, config} = LocalTime
{ parseDate, strftime, getI18nValue, config } = LocalTime

class LocalTime.Controller
SELECTOR = "time[data-local]:not([data-localized])"
Expand Down Expand Up @@ -33,7 +33,7 @@ class LocalTime.Controller
return if isNaN time

unless element.hasAttribute("title")
title = strftime(time, getI18nValue("datetime.formats.default"))
title = strftime(time, getI18nValue("#{local}.formats.default"))
element.setAttribute("title", title)

element.textContent = content =
Expand All @@ -43,7 +43,7 @@ class LocalTime.Controller
strftime(time, format)
when "date"
markAsLocalized(element)
relative(time).toDateString()
strftime(time, format)
when "time-ago"
relative(time).toString()
when "time-or-date"
Expand Down
10 changes: 4 additions & 6 deletions lib/assets/javascripts/src/local-time/helpers/i18n.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{config} = LocalTime
{i18n} = config

LocalTime.getI18nValue = (keyPath = "", {locale} = locale: config.locale) ->
value = getValue(i18n[locale], keyPath)
LocalTime.getI18nValue = (keyPath = "", { locale } = { locale: LocalTime.config.locale }) ->
value = getValue(LocalTime.config.i18n[locale], keyPath)
if value?
value
else if locale isnt config.defaultLocale
LocalTime.getI18nValue(keyPath, locale: config.defaultLocale)
else if locale isnt LocalTime.config.defaultLocale
LocalTime.getI18nValue(keyPath, { locale: LocalTime.config.defaultLocale })

LocalTime.translate = (keyPath, interpolations = {}, options) ->
string = LocalTime.getI18nValue(keyPath, options)
Expand Down
10 changes: 5 additions & 5 deletions lib/assets/javascripts/src/local-time/helpers/strftime.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{getI18nValue, translate} = LocalTime
{ getI18nValue, translate } = LocalTime

LocalTime.strftime = strftime = (time, formatString) ->
day = time.getDay()
Expand All @@ -12,10 +12,10 @@ LocalTime.strftime = strftime = (time, formatString) ->
formatString.replace /%(-?)([%aAbBcdeHIlmMpPSwyYZ])/g, (match, flag, modifier) ->
switch modifier
when "%" then "%"
when "a" then getI18nValue("date.abbrDayNames")[day]
when "A" then getI18nValue("date.dayNames")[day]
when "b" then getI18nValue("date.abbrMonthNames")[month]
when "B" then getI18nValue("date.monthNames")[month]
when "a" then getI18nValue("date.abbr_day_names")[day]
when "A" then getI18nValue("date.day_names")[day]
when "b" then getI18nValue("date.abbr_month_names")[month + 1]
when "B" then getI18nValue("date.month_names")[month + 1]
when "c" then time.toString()
when "d" then pad(date, flag)
when "e" then date
Expand Down
4 changes: 0 additions & 4 deletions lib/assets/javascripts/src/local-time/start.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ nextFrame = (fn) ->

startController = ->
controller = LocalTime.getController()
controller.start()

LocalTime.start = ->
unless started
Expand All @@ -20,6 +19,3 @@ LocalTime.start = ->
startController()
else
nextFrame(startController)

if window.LocalTime is LocalTime
LocalTime.start()