fc77ee76d6
The primary license of the project is changing to: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later The Additional Permission is designed to permit publicly distributed Javascript code to be relicensed under LGPL-3.0-or-later, but not server-side Javascript code. As such, we've relicensed here static Javscript files under LGPL-3.0-or-later, and those that run as part of build and/or server side under AGPL-3.0-or-later. Note that in future, Javascript files may be updated to be stronger copyleft license with the Additional Permission, particularly if they adapted to run on server side and/or turned into templates. Of course, we'd seek public discussion with the contributor community about such changes. This commit is one of the many steps to relicense the entire codebase. Documentation granting permission for this relicensing (from all past contributors who hold copyrights) is on file with Software Freedom Conservancy, Inc.
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
// License: LGPL-3.0-or-later
|
|
const flyd = require('flyd')
|
|
const flyd_every = require('flyd/module/every')
|
|
const moment = require('moment-timezone')
|
|
const format = require('../common/format')
|
|
const pluralize = format.pluralize
|
|
|
|
// Given an end dateTime ("2015-11-17 19:00") and a time-zone ("America/Los_Angeles"),
|
|
// if the end dateTime has passed, return false
|
|
// if the end dateTime is more than a day away
|
|
// then return the number of days away
|
|
// if the end dateTime is less than a day away
|
|
// then return a countdown stream with seconds precision
|
|
//
|
|
// This function returns a stream.
|
|
//
|
|
// This function takes a timezone in the format "Country/City"
|
|
// See here: http://momentjs.com/timezone/
|
|
//
|
|
|
|
|
|
const timeRemaining = (endDateTime, tz) => {
|
|
if(!endDateTime) return flyd.stream(false)
|
|
const format = "YYYY-MM-DD hh:mm:ss zz"
|
|
tz = tz || ENV.nonprofitTimezone || 'America/Los_Angeles'
|
|
const [now, end] = [moment().tz(tz), moment(endDateTime, format).tz(tz).seconds(59)]
|
|
console.log({now, end})
|
|
if(end.isBefore(now)) return flyd.stream(false)
|
|
|
|
if(end.diff(now, 'hours') <= 24) {
|
|
return flyd.map(
|
|
t => moment.utc(end.diff(moment(t))).format("HH:mm:ss")
|
|
, flyd_every(1000))
|
|
} else {
|
|
return flyd.stream(pluralize(end.diff(now, 'days'), 'days'))
|
|
}
|
|
}
|
|
|
|
module.exports = timeRemaining
|