@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
92 lines (90 loc) • 3.36 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import moment from 'moment-timezone';
import { defineMessages } from 'react-intl';
import { getCurrentIntl } from './i18n';
const translations = defineMessages({
ordinals: {
id: 'dateTime.ordinals',
defaultMessage: '{day, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}'
}
});
export const getTimezones = ((zoneNames) => {
let timeZones = [];
zoneNames.forEach((name, i) => {
timeZones.push({
name,
offset: `${moment.tz(zoneNames[i]).format('Z')}`
});
});
const sorted = timeZones.sort((a, b) => (parseInt(a.offset) > parseInt(b.offset) ? 1 : -1));
return () => sorted;
})(moment.tz.names());
export function asDayMonthDateTime(date) {
const parts = getCurrentIntl().formatDateToParts(date, {
month: 'long',
day: 'numeric',
weekday: 'long',
year: 'numeric'
});
return `${parts[0].value} ${parts[2].value} ${getCurrentIntl().formatMessage(translations.ordinals, {
day: parts[4].value
})} ${parts[6].value} @ ${getCurrentIntl().formatTime(date)}`;
}
export function asLocalizedDateTime(date, localeCode, dateTimeFormatOptions) {
return new Intl.DateTimeFormat(localeCode, dateTimeFormatOptions).format(new Date(date));
}
export function getUserTimeZone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
export function getUserLocaleCode() {
return Intl.DateTimeFormat().resolvedOptions().locale;
}
/**
* Create ISO 8601 string
**/
export const create8601String = (date, time, offset) => `${date}T${time}${offset}`;
/**
* Returns an array as ['yyyy-mm-dd', 'hh:mm:ss', '+/-nn:nn'] out of a ISO 8601 date string
**/
export const get8601Pieces = (date) => {
const pieces = moment(date)
.format()
.match(/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(.{6})/);
return [pieces[1], pieces[2], pieces[3]];
};
export function isSameDay(date1, date2) {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}