Ghost/core/frontend/helpers/date.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

// # Date Helper
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
//
timezones: Always use the timezone of blog setting closes #6406 - adding timeZone Service to get the offset (=timezone reg. moment-timezone) overall available - new publishedAtOffset date as CP using timeZone service and moment-timezone to calculate offset incl. DST - removing timezone-obj transform as it became obsolete with moment-timezone - reading timezones from configuration/timezones api endpoint - adding a moment-utc transform to only work with utc times in backend - when switching the timezone in the select box, the user will be shown the local time of the selected timezone - added clock service to show actual time ticking below select box - default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London' - if no timezone is saved in the settings yet, the default value will be used - showing local time in 'Publish Date' when it's a draft and no actual publishedAt value exists - Removed the format 'DD MMM YY @ HH:mm (UTC Z)' which resolves to '01 Jan 16 @ 14:00 (UTC +02:00)' - Changing the date.js helper in core/server for moment-timezone - Fix timezone select: updates `selectedTimezone` to return the matching object from `availableTimezones` - Including timezones in test for date-helper - update to moment-timezone 0.5.1 - moving form-group of 'selectTimezone' further up so - Tests: - Set except for clock service in test env - adding fixtures to mirage - adding 'service.ajax' to navigation-test.js - adding 'service:ghostPaths' to navigation-test.js - Code improvements - Changing clockservice to ES6
2016-02-02 10:04:40 +03:00
// Formats a date using moment-timezone.js. Formats published_at by default but will also take a date as a parameter
const {SafeString} = require('../services/proxy');
const moment = require('moment-timezone');
const _ = require('lodash');
module.exports = function (...attrs) {
// Options is the last argument
const options = attrs.pop();
let date;
// If there is any more arguments, date is the first one
if (!_.isEmpty(attrs)) {
date = attrs.shift();
// If there is no date argument & the current context contains published_at use that by default,
// else date being undefined means moment will use the current date
} else if (this.published_at) {
date = this.published_at;
}
// ensure that date is undefined, not null, as that can cause errors
date = date === null ? undefined : date;
const timezone = options.data.site.timezone;
const locale = options.data.site.locale;
const {
format = 'll',
timeago
} = options.hash;
const timeNow = moment().tz(timezone);
// Our date might be user input
let testDateInput = Date.parse(date);
let dateMoment;
if (isNaN(testDateInput) === false) {
dateMoment = moment.parseZone(date);
} else {
dateMoment = timeNow;
}
// i18n: Making dates, including month names, translatable to any language.
// Documentation: http://momentjs.com/docs/#/i18n/
// Locales: https://github.com/moment/moment/tree/develop/locale
dateMoment.locale(locale);
if (timeago) {
date = dateMoment.tz(timezone).from(timeNow);
} else {
date = dateMoment.tz(timezone).format(format);
}
timezones: Always use the timezone of blog setting closes #6406 - adding timeZone Service to get the offset (=timezone reg. moment-timezone) overall available - new publishedAtOffset date as CP using timeZone service and moment-timezone to calculate offset incl. DST - removing timezone-obj transform as it became obsolete with moment-timezone - reading timezones from configuration/timezones api endpoint - adding a moment-utc transform to only work with utc times in backend - when switching the timezone in the select box, the user will be shown the local time of the selected timezone - added clock service to show actual time ticking below select box - default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London' - if no timezone is saved in the settings yet, the default value will be used - showing local time in 'Publish Date' when it's a draft and no actual publishedAt value exists - Removed the format 'DD MMM YY @ HH:mm (UTC Z)' which resolves to '01 Jan 16 @ 14:00 (UTC +02:00)' - Changing the date.js helper in core/server for moment-timezone - Fix timezone select: updates `selectedTimezone` to return the matching object from `availableTimezones` - Including timezones in test for date-helper - update to moment-timezone 0.5.1 - moving form-group of 'selectTimezone' further up so - Tests: - Set except for clock service in test env - adding fixtures to mirage - adding 'service.ajax' to navigation-test.js - adding 'service:ghostPaths' to navigation-test.js - Code improvements - Changing clockservice to ES6
2016-02-02 10:04:40 +03:00
return new SafeString(date);
};