app/assets/javascripts/local_time.js.coffee in local_time-0.3.0 vs app/assets/javascripts/local_time.js.coffee in local_time-1.0.0
- old
+ new
@@ -28,11 +28,11 @@
year = time.getFullYear()
hour = time.getHours()
minute = time.getMinutes()
second = time.getSeconds()
- formatString.replace /%([%aAbBcdeHIlmMpPSwyY])/g, ([match, modifier]) ->
+ formatString.replace /%([%aAbBcdeHIlmMpPSwyYZ])/g, ([match, modifier]) ->
switch modifier
when '%' then '%'
when 'a' then weekdays[day].slice 0, 3
when 'A' then weekdays[day]
when 'b' then months[month].slice 0, 3
@@ -49,10 +49,11 @@
when 'P' then (if hour > 11 then 'pm' else 'am')
when 'S' then pad second
when 'w' then day
when 'y' then pad year % 100
when 'Y' then year
+ when 'Z' then time.toString().match(/\((\w+)\)$/)?[1] ? ''
class CalendarDate
@fromDate: (date) ->
new this date.getFullYear(), date.getMonth() + 1, date.getDate()
@@ -65,11 +66,21 @@
@date.setUTCDate day
@year = @date.getUTCFullYear()
@month = @date.getUTCMonth() + 1
@day = @date.getUTCDate()
+ @value = @date.getTime()
+ equals: (calendarDate) ->
+ calendarDate?.value is @value
+
+ is: (calendarDate) ->
+ @equals calendarDate
+
+ isToday: ->
+ @is @constructor.today()
+
occursOnSameYearAs: (date) ->
@year is date?.year
occursThisYear: ->
@occursOnSameYearAs @constructor.today()
@@ -80,11 +91,11 @@
daysPassed: ->
@constructor.today().daysSince @
-class RelativeTimeAgo
+class RelativeTime
constructor: (@date) ->
@calendarDate = CalendarDate.fromDate @date
toString: ->
# Today: "Saved 5 hours ago"
@@ -98,10 +109,16 @@
# Older: "Saved on Dec 15"
else
"on #{@formatDate()}"
+ toTimeOrDateString: ->
+ if @calendarDate.isToday()
+ @formatTime()
+ else
+ @formatDate()
+
timeElapsed: ->
ms = new Date().getTime() - @date.getTime()
sec = Math.round ms / 1000
min = Math.round sec / 60
hr = Math.round min / 60
@@ -141,27 +158,34 @@
strftime @date, format
formatTime: ->
strftime @date, '%l:%M%P'
+relativeDate = (date) ->
+ new RelativeTime(date).formatDate()
+
relativeTimeAgo = (date) ->
- new RelativeTimeAgo(date).toString()
+ new RelativeTime(date).toString()
+relativeTimeOrDate = (date) ->
+ new RelativeTime(date).toTimeOrDateString()
+
+relativeWeekday = (date) ->
+ if day = new RelativeTime(date).relativeWeekday()
+ day.charAt(0).toUpperCase() + day.substring(1)
+
+
domLoaded = false
update = (callback) ->
callback() if domLoaded
document.addEventListener "time:elapse", callback
if Turbolinks?.supported
document.addEventListener "page:update", callback
else
- setTimeout ->
- window.addEventListener "popstate", callback
- , 1
-
jQuery?(document).on "ajaxSuccess", (event, xhr) ->
callback() if jQuery.trim xhr.responseText
process = (selector, callback) ->
update ->
@@ -178,21 +202,32 @@
local = element.getAttribute "data-local"
time = new Date Date.parse datetime
return if isNaN time
+ unless element.hasAttribute("title")
+ element.setAttribute "title", strftime(time, "%B %e, %Y at %l:%M%P %Z")
+
element[textProperty] =
switch local
+ when "date"
+ element.setAttribute "data-localized", true
+ relativeDate time
when "time"
element.setAttribute "data-localized", true
strftime time, format
when "time-ago"
relativeTimeAgo time
+ when "time-or-date"
+ relativeTimeOrDate time
+ when "weekday"
+ relativeWeekday(time) ? ""
- setInterval ->
- event = document.createEvent "Events"
- event.initEvent "time:elapse", true, true
- document.dispatchEvent event
- , 60 * 1000
+run = ->
+ event = document.createEvent "Events"
+ event.initEvent "time:elapse", true, true
+ document.dispatchEvent event
+setInterval run, 60 * 1000
+
# Public API
-@LocalTime = {strftime, relativeTimeAgo}
+@LocalTime = {relativeDate, relativeTimeAgo, relativeTimeOrDate, relativeWeekday, run, strftime}