@wordpress/date
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
@@ -0,1 +1,4 @@ | ||
import "core-js/modules/es6.regexp.split"; | ||
import "core-js/modules/es6.regexp.replace"; | ||
/** | ||
@@ -7,21 +10,30 @@ * External dependencies | ||
import 'moment-timezone/moment-timezone-utils'; | ||
var settings = { | ||
l10n: { | ||
locale: 'en_US', | ||
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
meridiem: { am: 'am', pm: 'pm', AM: 'AM', PM: 'PM' }, | ||
relative: { future: ' % s from now', past: '% s ago' } | ||
}, | ||
formats: { | ||
time: 'g: i a', | ||
date: 'F j, Y', | ||
datetime: 'F j, Y g: i a' | ||
}, | ||
timezone: { offset: '0', string: '' } | ||
l10n: { | ||
locale: 'en_US', | ||
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
meridiem: { | ||
am: 'am', | ||
pm: 'pm', | ||
AM: 'AM', | ||
PM: 'PM' | ||
}, | ||
relative: { | ||
future: ' % s from now', | ||
past: '% s ago' | ||
} | ||
}, | ||
formats: { | ||
time: 'g: i a', | ||
date: 'F j, Y', | ||
datetime: 'F j, Y g: i a' | ||
}, | ||
timezone: { | ||
offset: '0', | ||
string: '' | ||
} | ||
}; | ||
/** | ||
@@ -32,52 +44,50 @@ * Adds a locale to moment, using the format supplied by `wp_localize_script()`. | ||
*/ | ||
export function setSettings(dateSettings) { | ||
settings = dateSettings; | ||
settings = dateSettings; // Backup and restore current locale. | ||
// Backup and restore current locale. | ||
var currentLocale = momentLib.locale(); | ||
momentLib.updateLocale(dateSettings.l10n.locale, { | ||
// Inherit anything missing from the default locale. | ||
parentLocale: currentLocale, | ||
months: dateSettings.l10n.months, | ||
monthsShort: dateSettings.l10n.monthsShort, | ||
weekdays: dateSettings.l10n.weekdays, | ||
weekdaysShort: dateSettings.l10n.weekdaysShort, | ||
meridiem: function meridiem(hour, minute, isLowercase) { | ||
if (hour < 12) { | ||
return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM; | ||
} | ||
return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM; | ||
}, | ||
var currentLocale = momentLib.locale(); | ||
momentLib.updateLocale(dateSettings.l10n.locale, { | ||
// Inherit anything missing from the default locale. | ||
parentLocale: currentLocale, | ||
months: dateSettings.l10n.months, | ||
monthsShort: dateSettings.l10n.monthsShort, | ||
weekdays: dateSettings.l10n.weekdays, | ||
weekdaysShort: dateSettings.l10n.weekdaysShort, | ||
meridiem: function meridiem(hour, minute, isLowercase) { | ||
if (hour < 12) { | ||
return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM; | ||
} | ||
longDateFormat: { | ||
LT: dateSettings.formats.time, | ||
LTS: null, | ||
L: null, | ||
LL: dateSettings.formats.date, | ||
LLL: dateSettings.formats.datetime, | ||
LLLL: null | ||
}, | ||
// From human_time_diff? | ||
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead. | ||
relativeTime: { | ||
future: dateSettings.l10n.relative.future, | ||
past: dateSettings.l10n.relative.past, | ||
s: 'seconds', | ||
m: 'a minute', | ||
mm: '%d minutes', | ||
h: 'an hour', | ||
hh: '%d hours', | ||
d: 'a day', | ||
dd: '%d days', | ||
M: 'a month', | ||
MM: '%d months', | ||
y: 'a year', | ||
yy: '%d years' | ||
} | ||
}); | ||
momentLib.locale(currentLocale); | ||
setupWPTimezone(); | ||
return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM; | ||
}, | ||
longDateFormat: { | ||
LT: dateSettings.formats.time, | ||
LTS: null, | ||
L: null, | ||
LL: dateSettings.formats.date, | ||
LLL: dateSettings.formats.datetime, | ||
LLLL: null | ||
}, | ||
// From human_time_diff? | ||
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead. | ||
relativeTime: { | ||
future: dateSettings.l10n.relative.future, | ||
past: dateSettings.l10n.relative.past, | ||
s: 'seconds', | ||
m: 'a minute', | ||
mm: '%d minutes', | ||
h: 'an hour', | ||
hh: '%d hours', | ||
d: 'a day', | ||
dd: '%d days', | ||
M: 'a month', | ||
MM: '%d months', | ||
y: 'a year', | ||
yy: '%d years' | ||
} | ||
}); | ||
momentLib.locale(currentLocale); | ||
setupWPTimezone(); | ||
} | ||
/** | ||
@@ -88,28 +98,28 @@ * Returns the currently defined date settings. | ||
*/ | ||
export function getSettings() { | ||
return settings; | ||
return settings; | ||
} | ||
function setupWPTimezone() { | ||
// Create WP timezone based off dateSettings. | ||
momentLib.tz.add(momentLib.tz.pack({ | ||
name: 'WP', | ||
abbrs: ['WP'], | ||
untils: [null], | ||
offsets: [-settings.timezone.offset * 60 || 0] | ||
})); | ||
} | ||
// Create a new moment object which mirrors moment but includes | ||
// Create WP timezone based off dateSettings. | ||
momentLib.tz.add(momentLib.tz.pack({ | ||
name: 'WP', | ||
abbrs: ['WP'], | ||
untils: [null], | ||
offsets: [-settings.timezone.offset * 60 || 0] | ||
})); | ||
} // Create a new moment object which mirrors moment but includes | ||
// the attached timezone, instead of setting a default timezone on | ||
// the global moment object. | ||
export var moment = function moment() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return momentLib.tz.apply(momentLib, args.concat(['WP'])); | ||
}; | ||
return momentLib.tz.apply(momentLib, args.concat(['WP'])); | ||
}; // Date constants. | ||
// Date constants. | ||
/** | ||
@@ -120,2 +130,3 @@ * Number of seconds in one minute. | ||
*/ | ||
var MINUTE_IN_SECONDS = 60; | ||
@@ -127,2 +138,3 @@ /** | ||
*/ | ||
var HOUR_IN_MINUTES = 60; | ||
@@ -134,4 +146,4 @@ /** | ||
*/ | ||
var HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS; | ||
/** | ||
@@ -149,138 +161,133 @@ * Map of PHP formats to Moment.js formats. | ||
*/ | ||
var formatMap = { | ||
// Day | ||
d: 'DD', | ||
D: 'ddd', | ||
j: 'D', | ||
l: 'dddd', | ||
N: 'E', | ||
// Day | ||
d: 'DD', | ||
D: 'ddd', | ||
j: 'D', | ||
l: 'dddd', | ||
N: 'E', | ||
/** | ||
* Gets the ordinal suffix. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
S: function S(momentDate) { | ||
// Do - D | ||
var num = momentDate.format('D'); | ||
var withOrdinal = momentDate.format('Do'); | ||
return withOrdinal.replace(num, ''); | ||
}, | ||
/** | ||
* Gets the ordinal suffix. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
S: function S(momentDate) { | ||
// Do - D | ||
var num = momentDate.format('D'); | ||
var withOrdinal = momentDate.format('Do'); | ||
return withOrdinal.replace(num, ''); | ||
}, | ||
w: 'd', | ||
/** | ||
* Gets the day of the year (zero-indexed). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
z: function z(momentDate) { | ||
// DDD - 1 | ||
return '' + parseInt(momentDate.format('DDD'), 10) - 1; | ||
}, | ||
// Week | ||
W: 'W', | ||
// Month | ||
F: 'MMMM', | ||
m: 'MM', | ||
M: 'MMM', | ||
n: 'M', | ||
w: 'd', | ||
/** | ||
* Gets the day of the year (zero-indexed). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
z: function z(momentDate) { | ||
// DDD - 1 | ||
return '' + parseInt(momentDate.format('DDD'), 10) - 1; | ||
}, | ||
/** | ||
* Gets the days in the month. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
t: function t(momentDate) { | ||
return momentDate.daysInMonth(); | ||
}, | ||
// Year | ||
/** | ||
* Gets whether the current year is a leap year. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
L: function L(momentDate) { | ||
return momentDate.isLeapYear() ? '1' : '0'; | ||
}, | ||
o: 'GGGG', | ||
Y: 'YYYY', | ||
y: 'YY', | ||
// Time | ||
a: 'a', | ||
A: 'A', | ||
// Week | ||
W: 'W', | ||
/** | ||
* Gets the current time in Swatch Internet Time (.beats). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
B: function B(momentDate) { | ||
var timezoned = momentLib(momentDate).utcOffset(60); | ||
var seconds = parseInt(timezoned.format('s'), 10), | ||
minutes = parseInt(timezoned.format('m'), 10), | ||
hours = parseInt(timezoned.format('H'), 10); | ||
return parseInt((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4, 10); | ||
}, | ||
g: 'h', | ||
G: 'H', | ||
h: 'hh', | ||
H: 'HH', | ||
i: 'mm', | ||
s: 'ss', | ||
u: 'SSSSSS', | ||
v: 'SSS', | ||
// Timezone | ||
e: 'zz', | ||
// Month | ||
F: 'MMMM', | ||
m: 'MM', | ||
M: 'MMM', | ||
n: 'M', | ||
/** | ||
* Gets the days in the month. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
t: function t(momentDate) { | ||
return momentDate.daysInMonth(); | ||
}, | ||
/** | ||
* Gets whether the timezone is in DST currently. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
I: function I(momentDate) { | ||
return momentDate.isDST() ? '1' : '0'; | ||
}, | ||
O: 'ZZ', | ||
P: 'Z', | ||
T: 'z', | ||
// Year | ||
/** | ||
* Gets whether the current year is a leap year. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
L: function L(momentDate) { | ||
return momentDate.isLeapYear() ? '1' : '0'; | ||
}, | ||
o: 'GGGG', | ||
Y: 'YYYY', | ||
y: 'YY', | ||
// Time | ||
a: 'a', | ||
A: 'A', | ||
/** | ||
* Gets the current time in Swatch Internet Time (.beats). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
B: function B(momentDate) { | ||
var timezoned = momentLib(momentDate).utcOffset(60); | ||
var seconds = parseInt(timezoned.format('s'), 10), | ||
minutes = parseInt(timezoned.format('m'), 10), | ||
hours = parseInt(timezoned.format('H'), 10); | ||
return parseInt((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4, 10); | ||
}, | ||
g: 'h', | ||
G: 'H', | ||
h: 'hh', | ||
H: 'HH', | ||
i: 'mm', | ||
s: 'ss', | ||
u: 'SSSSSS', | ||
v: 'SSS', | ||
// Timezone | ||
e: 'zz', | ||
/** | ||
* Gets whether the timezone is in DST currently. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
I: function I(momentDate) { | ||
return momentDate.isDST() ? '1' : '0'; | ||
}, | ||
O: 'ZZ', | ||
P: 'Z', | ||
T: 'z', | ||
/** | ||
* Gets the timezone offset in seconds. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
Z: function Z(momentDate) { | ||
// Timezone offset in seconds. | ||
var offset = momentDate.format('Z'); | ||
var sign = offset[0] === '-' ? -1 : 1; | ||
var parts = offset.substring(1).split(':'); | ||
return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS; | ||
}, | ||
// Full date/time | ||
c: 'YYYY-MM-DDTHH:mm:ssZ', // .toISOString | ||
r: 'ddd, D MMM YYYY HH:mm:ss ZZ', | ||
U: 'X' | ||
/** | ||
* Gets the timezone offset in seconds. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
Z: function Z(momentDate) { | ||
// Timezone offset in seconds. | ||
var offset = momentDate.format('Z'); | ||
var sign = offset[0] === '-' ? -1 : 1; | ||
var parts = offset.substring(1).split(':'); | ||
return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS; | ||
}, | ||
// Full date/time | ||
c: 'YYYY-MM-DDTHH:mm:ssZ', | ||
// .toISOString | ||
r: 'ddd, D MMM YYYY HH:mm:ss ZZ', | ||
U: 'X' | ||
}; | ||
/** | ||
@@ -296,36 +303,37 @@ * Formats a date. Does not alter the date's timezone. | ||
*/ | ||
export function format(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var i, char; | ||
var newFormat = []; | ||
var momentDate = momentLib(dateValue); | ||
var i = void 0, | ||
char = void 0; | ||
var newFormat = []; | ||
var momentDate = momentLib(dateValue); | ||
for (i = 0; i < dateFormat.length; i++) { | ||
char = dateFormat[i]; | ||
// Is this an escape? | ||
if ('\\' === char) { | ||
// Add next character, then move on. | ||
i++; | ||
newFormat.push('[' + dateFormat[i] + ']'); | ||
continue; | ||
} | ||
if (char in formatMap) { | ||
if (typeof formatMap[char] !== 'string') { | ||
// If the format is a function, call it. | ||
newFormat.push('[' + formatMap[char](momentDate) + ']'); | ||
} else { | ||
// Otherwise, add as a formatting string. | ||
newFormat.push(formatMap[char]); | ||
} | ||
} else { | ||
newFormat.push('[' + char + ']'); | ||
} | ||
} | ||
// Join with [] between to separate characters, and replace | ||
// unneeded separators with static text. | ||
newFormat = newFormat.join('[]'); | ||
return momentDate.format(newFormat); | ||
for (i = 0; i < dateFormat.length; i++) { | ||
char = dateFormat[i]; // Is this an escape? | ||
if ('\\' === char) { | ||
// Add next character, then move on. | ||
i++; | ||
newFormat.push('[' + dateFormat[i] + ']'); | ||
continue; | ||
} | ||
if (char in formatMap) { | ||
if (typeof formatMap[char] !== 'string') { | ||
// If the format is a function, call it. | ||
newFormat.push('[' + formatMap[char](momentDate) + ']'); | ||
} else { | ||
// Otherwise, add as a formatting string. | ||
newFormat.push(formatMap[char]); | ||
} | ||
} else { | ||
newFormat.push('[' + char + ']'); | ||
} | ||
} // Join with [] between to separate characters, and replace | ||
// unneeded separators with static text. | ||
newFormat = newFormat.join('[]'); | ||
return momentDate.format(newFormat); | ||
} | ||
/** | ||
@@ -341,10 +349,9 @@ * Formats a date (like `date()` in PHP), in the site's timezone. | ||
*/ | ||
export function date(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var offset = settings.timezone.offset * HOUR_IN_MINUTES; | ||
var dateMoment = momentLib(dateValue).utcOffset(offset, true); | ||
return format(dateFormat, dateMoment); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var offset = settings.timezone.offset * HOUR_IN_MINUTES; | ||
var dateMoment = momentLib(dateValue).utcOffset(offset, true); | ||
return format(dateFormat, dateMoment); | ||
} | ||
/** | ||
@@ -360,9 +367,8 @@ * Formats a date (like `date()` in PHP), in the UTC timezone. | ||
*/ | ||
export function gmdate(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateMoment = momentLib(dateValue).utc(); | ||
return format(dateFormat, dateMoment); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateMoment = momentLib(dateValue).utc(); | ||
return format(dateFormat, dateMoment); | ||
} | ||
/** | ||
@@ -380,17 +386,15 @@ * Formats a date (like `dateI18n()` in PHP). | ||
*/ | ||
export function dateI18n(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var gmt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var gmt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
// Defaults. | ||
var offset = gmt ? 0 : settings.timezone.offset * HOUR_IN_MINUTES; // Convert to moment object. | ||
// Defaults. | ||
var offset = gmt ? 0 : settings.timezone.offset * HOUR_IN_MINUTES; | ||
// Convert to moment object. | ||
var dateMoment = momentLib(dateValue).utcOffset(offset, true); | ||
var dateMoment = momentLib(dateValue).utcOffset(offset, true); // Set the locale. | ||
// Set the locale. | ||
dateMoment.locale(settings.l10n.locale); | ||
// Format and return. | ||
return format(dateFormat, dateMoment); | ||
dateMoment.locale(settings.l10n.locale); // Format and return. | ||
return format(dateFormat, dateMoment); | ||
} | ||
setupWPTimezone(); |
@@ -1,7 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.moment = undefined; | ||
exports.setSettings = setSettings; | ||
@@ -13,31 +14,45 @@ exports.getSettings = getSettings; | ||
exports.dateI18n = dateI18n; | ||
exports.moment = void 0; | ||
var _moment = require('moment'); | ||
require("core-js/modules/es6.regexp.split"); | ||
var _moment2 = _interopRequireDefault(_moment); | ||
require("core-js/modules/es6.regexp.replace"); | ||
require('moment-timezone'); | ||
var _moment = _interopRequireDefault(require("moment")); | ||
require('moment-timezone/moment-timezone-utils'); | ||
require("moment-timezone"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
require("moment-timezone/moment-timezone-utils"); | ||
/** | ||
* External dependencies | ||
*/ | ||
var settings = { | ||
l10n: { | ||
locale: 'en_US', | ||
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
meridiem: { am: 'am', pm: 'pm', AM: 'AM', PM: 'PM' }, | ||
relative: { future: ' % s from now', past: '% s ago' } | ||
}, | ||
formats: { | ||
time: 'g: i a', | ||
date: 'F j, Y', | ||
datetime: 'F j, Y g: i a' | ||
}, | ||
timezone: { offset: '0', string: '' } | ||
l10n: { | ||
locale: 'en_US', | ||
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
meridiem: { | ||
am: 'am', | ||
pm: 'pm', | ||
AM: 'AM', | ||
PM: 'PM' | ||
}, | ||
relative: { | ||
future: ' % s from now', | ||
past: '% s ago' | ||
} | ||
}, | ||
formats: { | ||
time: 'g: i a', | ||
date: 'F j, Y', | ||
datetime: 'F j, Y g: i a' | ||
}, | ||
timezone: { | ||
offset: '0', | ||
string: '' | ||
} | ||
}; | ||
/** | ||
@@ -48,55 +63,53 @@ * Adds a locale to moment, using the format supplied by `wp_localize_script()`. | ||
*/ | ||
/** | ||
* External dependencies | ||
*/ | ||
function setSettings(dateSettings) { | ||
settings = dateSettings; | ||
settings = dateSettings; // Backup and restore current locale. | ||
// Backup and restore current locale. | ||
var currentLocale = _moment2.default.locale(); | ||
_moment2.default.updateLocale(dateSettings.l10n.locale, { | ||
// Inherit anything missing from the default locale. | ||
parentLocale: currentLocale, | ||
months: dateSettings.l10n.months, | ||
monthsShort: dateSettings.l10n.monthsShort, | ||
weekdays: dateSettings.l10n.weekdays, | ||
weekdaysShort: dateSettings.l10n.weekdaysShort, | ||
meridiem: function meridiem(hour, minute, isLowercase) { | ||
if (hour < 12) { | ||
return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM; | ||
} | ||
return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM; | ||
}, | ||
var currentLocale = _moment.default.locale(); | ||
longDateFormat: { | ||
LT: dateSettings.formats.time, | ||
LTS: null, | ||
L: null, | ||
LL: dateSettings.formats.date, | ||
LLL: dateSettings.formats.datetime, | ||
LLLL: null | ||
}, | ||
// From human_time_diff? | ||
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead. | ||
relativeTime: { | ||
future: dateSettings.l10n.relative.future, | ||
past: dateSettings.l10n.relative.past, | ||
s: 'seconds', | ||
m: 'a minute', | ||
mm: '%d minutes', | ||
h: 'an hour', | ||
hh: '%d hours', | ||
d: 'a day', | ||
dd: '%d days', | ||
M: 'a month', | ||
MM: '%d months', | ||
y: 'a year', | ||
yy: '%d years' | ||
} | ||
}); | ||
_moment2.default.locale(currentLocale); | ||
_moment.default.updateLocale(dateSettings.l10n.locale, { | ||
// Inherit anything missing from the default locale. | ||
parentLocale: currentLocale, | ||
months: dateSettings.l10n.months, | ||
monthsShort: dateSettings.l10n.monthsShort, | ||
weekdays: dateSettings.l10n.weekdays, | ||
weekdaysShort: dateSettings.l10n.weekdaysShort, | ||
meridiem: function meridiem(hour, minute, isLowercase) { | ||
if (hour < 12) { | ||
return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM; | ||
} | ||
setupWPTimezone(); | ||
return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM; | ||
}, | ||
longDateFormat: { | ||
LT: dateSettings.formats.time, | ||
LTS: null, | ||
L: null, | ||
LL: dateSettings.formats.date, | ||
LLL: dateSettings.formats.datetime, | ||
LLLL: null | ||
}, | ||
// From human_time_diff? | ||
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead. | ||
relativeTime: { | ||
future: dateSettings.l10n.relative.future, | ||
past: dateSettings.l10n.relative.past, | ||
s: 'seconds', | ||
m: 'a minute', | ||
mm: '%d minutes', | ||
h: 'an hour', | ||
hh: '%d hours', | ||
d: 'a day', | ||
dd: '%d days', | ||
M: 'a month', | ||
MM: '%d months', | ||
y: 'a year', | ||
yy: '%d years' | ||
} | ||
}); | ||
_moment.default.locale(currentLocale); | ||
setupWPTimezone(); | ||
} | ||
/** | ||
@@ -107,28 +120,29 @@ * Returns the currently defined date settings. | ||
*/ | ||
function getSettings() { | ||
return settings; | ||
return settings; | ||
} | ||
function setupWPTimezone() { | ||
// Create WP timezone based off dateSettings. | ||
_moment2.default.tz.add(_moment2.default.tz.pack({ | ||
name: 'WP', | ||
abbrs: ['WP'], | ||
untils: [null], | ||
offsets: [-settings.timezone.offset * 60 || 0] | ||
})); | ||
} | ||
// Create a new moment object which mirrors moment but includes | ||
// Create WP timezone based off dateSettings. | ||
_moment.default.tz.add(_moment.default.tz.pack({ | ||
name: 'WP', | ||
abbrs: ['WP'], | ||
untils: [null], | ||
offsets: [-settings.timezone.offset * 60 || 0] | ||
})); | ||
} // Create a new moment object which mirrors moment but includes | ||
// the attached timezone, instead of setting a default timezone on | ||
// the global moment object. | ||
var moment = exports.moment = function moment() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return _moment2.default.tz.apply(_moment2.default, args.concat(['WP'])); | ||
}; | ||
// Date constants. | ||
var moment = function moment() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return _moment.default.tz.apply(_moment.default, args.concat(['WP'])); | ||
}; // Date constants. | ||
/** | ||
@@ -139,2 +153,5 @@ * Number of seconds in one minute. | ||
*/ | ||
exports.moment = moment; | ||
var MINUTE_IN_SECONDS = 60; | ||
@@ -146,2 +163,3 @@ /** | ||
*/ | ||
var HOUR_IN_MINUTES = 60; | ||
@@ -153,4 +171,4 @@ /** | ||
*/ | ||
var HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS; | ||
/** | ||
@@ -168,138 +186,133 @@ * Map of PHP formats to Moment.js formats. | ||
*/ | ||
var formatMap = { | ||
// Day | ||
d: 'DD', | ||
D: 'ddd', | ||
j: 'D', | ||
l: 'dddd', | ||
N: 'E', | ||
// Day | ||
d: 'DD', | ||
D: 'ddd', | ||
j: 'D', | ||
l: 'dddd', | ||
N: 'E', | ||
/** | ||
* Gets the ordinal suffix. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
S: function S(momentDate) { | ||
// Do - D | ||
var num = momentDate.format('D'); | ||
var withOrdinal = momentDate.format('Do'); | ||
return withOrdinal.replace(num, ''); | ||
}, | ||
/** | ||
* Gets the ordinal suffix. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
S: function S(momentDate) { | ||
// Do - D | ||
var num = momentDate.format('D'); | ||
var withOrdinal = momentDate.format('Do'); | ||
return withOrdinal.replace(num, ''); | ||
}, | ||
w: 'd', | ||
/** | ||
* Gets the day of the year (zero-indexed). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
z: function z(momentDate) { | ||
// DDD - 1 | ||
return '' + parseInt(momentDate.format('DDD'), 10) - 1; | ||
}, | ||
// Week | ||
W: 'W', | ||
// Month | ||
F: 'MMMM', | ||
m: 'MM', | ||
M: 'MMM', | ||
n: 'M', | ||
w: 'd', | ||
/** | ||
* Gets the day of the year (zero-indexed). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
z: function z(momentDate) { | ||
// DDD - 1 | ||
return '' + parseInt(momentDate.format('DDD'), 10) - 1; | ||
}, | ||
/** | ||
* Gets the days in the month. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
t: function t(momentDate) { | ||
return momentDate.daysInMonth(); | ||
}, | ||
// Year | ||
/** | ||
* Gets whether the current year is a leap year. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
L: function L(momentDate) { | ||
return momentDate.isLeapYear() ? '1' : '0'; | ||
}, | ||
o: 'GGGG', | ||
Y: 'YYYY', | ||
y: 'YY', | ||
// Time | ||
a: 'a', | ||
A: 'A', | ||
// Week | ||
W: 'W', | ||
/** | ||
* Gets the current time in Swatch Internet Time (.beats). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
B: function B(momentDate) { | ||
var timezoned = (0, _moment.default)(momentDate).utcOffset(60); | ||
var seconds = parseInt(timezoned.format('s'), 10), | ||
minutes = parseInt(timezoned.format('m'), 10), | ||
hours = parseInt(timezoned.format('H'), 10); | ||
return parseInt((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4, 10); | ||
}, | ||
g: 'h', | ||
G: 'H', | ||
h: 'hh', | ||
H: 'HH', | ||
i: 'mm', | ||
s: 'ss', | ||
u: 'SSSSSS', | ||
v: 'SSS', | ||
// Timezone | ||
e: 'zz', | ||
// Month | ||
F: 'MMMM', | ||
m: 'MM', | ||
M: 'MMM', | ||
n: 'M', | ||
/** | ||
* Gets the days in the month. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
t: function t(momentDate) { | ||
return momentDate.daysInMonth(); | ||
}, | ||
/** | ||
* Gets whether the timezone is in DST currently. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
I: function I(momentDate) { | ||
return momentDate.isDST() ? '1' : '0'; | ||
}, | ||
O: 'ZZ', | ||
P: 'Z', | ||
T: 'z', | ||
// Year | ||
/** | ||
* Gets whether the current year is a leap year. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
L: function L(momentDate) { | ||
return momentDate.isLeapYear() ? '1' : '0'; | ||
}, | ||
o: 'GGGG', | ||
Y: 'YYYY', | ||
y: 'YY', | ||
// Time | ||
a: 'a', | ||
A: 'A', | ||
/** | ||
* Gets the current time in Swatch Internet Time (.beats). | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
B: function B(momentDate) { | ||
var timezoned = (0, _moment2.default)(momentDate).utcOffset(60); | ||
var seconds = parseInt(timezoned.format('s'), 10), | ||
minutes = parseInt(timezoned.format('m'), 10), | ||
hours = parseInt(timezoned.format('H'), 10); | ||
return parseInt((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4, 10); | ||
}, | ||
g: 'h', | ||
G: 'H', | ||
h: 'hh', | ||
H: 'HH', | ||
i: 'mm', | ||
s: 'ss', | ||
u: 'SSSSSS', | ||
v: 'SSS', | ||
// Timezone | ||
e: 'zz', | ||
/** | ||
* Gets whether the timezone is in DST currently. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
I: function I(momentDate) { | ||
return momentDate.isDST() ? '1' : '0'; | ||
}, | ||
O: 'ZZ', | ||
P: 'Z', | ||
T: 'z', | ||
/** | ||
* Gets the timezone offset in seconds. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
Z: function Z(momentDate) { | ||
// Timezone offset in seconds. | ||
var offset = momentDate.format('Z'); | ||
var sign = offset[0] === '-' ? -1 : 1; | ||
var parts = offset.substring(1).split(':'); | ||
return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS; | ||
}, | ||
// Full date/time | ||
c: 'YYYY-MM-DDTHH:mm:ssZ', // .toISOString | ||
r: 'ddd, D MMM YYYY HH:mm:ss ZZ', | ||
U: 'X' | ||
/** | ||
* Gets the timezone offset in seconds. | ||
* | ||
* @param {moment} momentDate Moment instance. | ||
* | ||
* @return {string} Formatted date. | ||
*/ | ||
Z: function Z(momentDate) { | ||
// Timezone offset in seconds. | ||
var offset = momentDate.format('Z'); | ||
var sign = offset[0] === '-' ? -1 : 1; | ||
var parts = offset.substring(1).split(':'); | ||
return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS; | ||
}, | ||
// Full date/time | ||
c: 'YYYY-MM-DDTHH:mm:ssZ', | ||
// .toISOString | ||
r: 'ddd, D MMM YYYY HH:mm:ss ZZ', | ||
U: 'X' | ||
}; | ||
/** | ||
@@ -315,36 +328,37 @@ * Formats a date. Does not alter the date's timezone. | ||
*/ | ||
function format(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var i, char; | ||
var newFormat = []; | ||
var momentDate = (0, _moment.default)(dateValue); | ||
var i = void 0, | ||
char = void 0; | ||
var newFormat = []; | ||
var momentDate = (0, _moment2.default)(dateValue); | ||
for (i = 0; i < dateFormat.length; i++) { | ||
char = dateFormat[i]; | ||
// Is this an escape? | ||
if ('\\' === char) { | ||
// Add next character, then move on. | ||
i++; | ||
newFormat.push('[' + dateFormat[i] + ']'); | ||
continue; | ||
} | ||
if (char in formatMap) { | ||
if (typeof formatMap[char] !== 'string') { | ||
// If the format is a function, call it. | ||
newFormat.push('[' + formatMap[char](momentDate) + ']'); | ||
} else { | ||
// Otherwise, add as a formatting string. | ||
newFormat.push(formatMap[char]); | ||
} | ||
} else { | ||
newFormat.push('[' + char + ']'); | ||
} | ||
} | ||
// Join with [] between to separate characters, and replace | ||
// unneeded separators with static text. | ||
newFormat = newFormat.join('[]'); | ||
return momentDate.format(newFormat); | ||
for (i = 0; i < dateFormat.length; i++) { | ||
char = dateFormat[i]; // Is this an escape? | ||
if ('\\' === char) { | ||
// Add next character, then move on. | ||
i++; | ||
newFormat.push('[' + dateFormat[i] + ']'); | ||
continue; | ||
} | ||
if (char in formatMap) { | ||
if (typeof formatMap[char] !== 'string') { | ||
// If the format is a function, call it. | ||
newFormat.push('[' + formatMap[char](momentDate) + ']'); | ||
} else { | ||
// Otherwise, add as a formatting string. | ||
newFormat.push(formatMap[char]); | ||
} | ||
} else { | ||
newFormat.push('[' + char + ']'); | ||
} | ||
} // Join with [] between to separate characters, and replace | ||
// unneeded separators with static text. | ||
newFormat = newFormat.join('[]'); | ||
return momentDate.format(newFormat); | ||
} | ||
/** | ||
@@ -360,10 +374,10 @@ * Formats a date (like `date()` in PHP), in the site's timezone. | ||
*/ | ||
function date(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var offset = settings.timezone.offset * HOUR_IN_MINUTES; | ||
var dateMoment = (0, _moment2.default)(dateValue).utcOffset(offset, true); | ||
return format(dateFormat, dateMoment); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var offset = settings.timezone.offset * HOUR_IN_MINUTES; | ||
var dateMoment = (0, _moment.default)(dateValue).utcOffset(offset, true); | ||
return format(dateFormat, dateMoment); | ||
} | ||
/** | ||
@@ -379,9 +393,9 @@ * Formats a date (like `date()` in PHP), in the UTC timezone. | ||
*/ | ||
function gmdate(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateMoment = (0, _moment2.default)(dateValue).utc(); | ||
return format(dateFormat, dateMoment); | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var dateMoment = (0, _moment.default)(dateValue).utc(); | ||
return format(dateFormat, dateMoment); | ||
} | ||
/** | ||
@@ -399,17 +413,17 @@ * Formats a date (like `dateI18n()` in PHP). | ||
*/ | ||
function dateI18n(dateFormat) { | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var gmt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var dateValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
var gmt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
// Defaults. | ||
var offset = gmt ? 0 : settings.timezone.offset * HOUR_IN_MINUTES; // Convert to moment object. | ||
// Defaults. | ||
var offset = gmt ? 0 : settings.timezone.offset * HOUR_IN_MINUTES; | ||
// Convert to moment object. | ||
var dateMoment = (0, _moment2.default)(dateValue).utcOffset(offset, true); | ||
var dateMoment = (0, _moment.default)(dateValue).utcOffset(offset, true); // Set the locale. | ||
// Set the locale. | ||
dateMoment.locale(settings.l10n.locale); | ||
// Format and return. | ||
return format(dateFormat, dateMoment); | ||
dateMoment.locale(settings.l10n.locale); // Format and return. | ||
return format(dateFormat, dateMoment); | ||
} | ||
setupWPTimezone(); |
{ | ||
"name": "@wordpress/date", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"description": "Date module for WordPress", | ||
@@ -22,8 +22,9 @@ "author": "The WordPress Contributors", | ||
"dependencies": { | ||
"moment": "2.22.1", | ||
"moment-timezone": "0.5.16" | ||
"moment": "^2.22.1", | ||
"moment-timezone": "^0.5.16" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
}, | ||
"gitHead": "8827c049ec802471f51a5cb906d9096ffc1b4e48" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32425
1072
+ Addedmoment@2.30.1(transitive)
+ Addedmoment-timezone@0.5.46(transitive)
- Removedmoment@2.22.1(transitive)
- Removedmoment-timezone@0.5.16(transitive)
Updatedmoment@^2.22.1
Updatedmoment-timezone@^0.5.16