From f5152486a1bd4b6914602144c745224d90cd1d41 Mon Sep 17 00:00:00 2001 From: dzonidoo Date: Wed, 31 Jul 2024 12:14:31 +0200 Subject: [PATCH] improve ussages of date formating --- scripts/appConfig.ts | 8 ++++ .../apps/archive/services/ArchiveService.ts | 5 +-- scripts/apps/authoring-react/data-layer.ts | 8 ++-- .../directives/AuthoringHeaderDirective.ts | 4 +- scripts/core/datetime/datetime.ts | 34 ++++++++++++----- .../core/get-superdesk-api-implementation.tsx | 38 ++++++++++++++++--- 6 files changed, 74 insertions(+), 23 deletions(-) diff --git a/scripts/appConfig.ts b/scripts/appConfig.ts index cf89498749..0a5b9b0986 100644 --- a/scripts/appConfig.ts +++ b/scripts/appConfig.ts @@ -12,6 +12,14 @@ if (appConfig.shortTimeFormat == null) { appConfig.shortTimeFormat = 'HH:mm'; // 24h format } +if (appConfig.view.dateformat == null) { + appConfig.view.dateformat = 'MM/DD'; // 24h format +} + +if (appConfig.view.timeformat == null) { + appConfig.view.timeformat = 'hh:mm'; // 24h format +} + if (appConfig.ui == null) { appConfig.ui = {}; diff --git a/scripts/apps/archive/services/ArchiveService.ts b/scripts/apps/archive/services/ArchiveService.ts index c8a20bda20..da7ff210de 100644 --- a/scripts/apps/archive/services/ArchiveService.ts +++ b/scripts/apps/archive/services/ArchiveService.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import moment from 'moment'; -import {appConfig} from 'appConfig'; +import {formatDate} from 'core/get-superdesk-api-implementation'; ArchiveService.$inject = ['desks', 'session', 'api', '$q', 'search', '$location']; export function ArchiveService(desks, session, api, $q, search, $location) { @@ -81,8 +81,7 @@ export function ArchiveService(desks, session, api, $q, search, $location) { * @return {Object} the list of archive items */ this.getRelatedItems = function(item, fromDateTime) { - var beforeDateTime = fromDateTime || moment().subtract(1, 'days') - .format(appConfig.view.dateformat); + var beforeDateTime = formatDate(fromDateTime || moment().subtract(1, 'days')); var params: any = {}; params.q = 'slugline.phrase:"' + _.trim(item.slugline) + '"'; // exact match diff --git a/scripts/apps/authoring-react/data-layer.ts b/scripts/apps/authoring-react/data-layer.ts index fbce850b35..58e83a9355 100644 --- a/scripts/apps/authoring-react/data-layer.ts +++ b/scripts/apps/authoring-react/data-layer.ts @@ -25,7 +25,7 @@ import {getArticleAdapter} from './article-adapter'; import {gettext} from 'core/utils'; import {PACKAGE_ITEMS_FIELD_ID} from './fields/package-items'; import {description_text} from './field-adapters/description_text'; -import moment from 'moment'; +import {superdesk} from './../../../scripts/extensions/datetimeField/src/superdesk'; export function getArticleContentProfile( item: IArticle, @@ -421,8 +421,10 @@ export const authoringStorageIArticleCorrect: IAuthoringStorage = { newItem.sms_message = ''; const {override_ednote_for_corrections, override_ednote_template} = appConfig; - const date = moment(newItem.versioncreated) - .format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat); + + const {formatDateTime} = superdesk.localization; + + const date = formatDateTime(newItem.versioncreated); if (override_ednote_for_corrections && override_ednote_template == null) { const lineBreak = '\r\n\r\n'; diff --git a/scripts/apps/authoring/authoring/directives/AuthoringHeaderDirective.ts b/scripts/apps/authoring/authoring/directives/AuthoringHeaderDirective.ts index 29758a9234..02355b4a23 100644 --- a/scripts/apps/authoring/authoring/directives/AuthoringHeaderDirective.ts +++ b/scripts/apps/authoring/authoring/directives/AuthoringHeaderDirective.ts @@ -10,6 +10,7 @@ import {translateArticleType, gettext} from 'core/utils'; import {IArticle} from 'superdesk-api'; import {slideUpDown} from 'core/ui/slide-up-down'; import {runBeforeUpdateMiddlware} from '../services/authoring-helpers'; +import {formatDate} from 'core/get-superdesk-api-implementation'; AuthoringHeaderDirective.$inject = [ 'api', @@ -194,8 +195,7 @@ export function AuthoringHeaderDirective( scope.missing_link = false; if (scope.item.slugline && scope.item.type === 'text') { // get the midnight based on the default timezone not the user timezone. - var fromDateTime = moment().tz(appConfig.default_timezone) - .format(appConfig.view.dateformat); + var fromDateTime = formatDate(moment()); archiveService.getRelatedItems(scope.item, fromDateTime) .then((items) => { diff --git a/scripts/core/datetime/datetime.ts b/scripts/core/datetime/datetime.ts index ea955b066b..778a520f5f 100644 --- a/scripts/core/datetime/datetime.ts +++ b/scripts/core/datetime/datetime.ts @@ -3,6 +3,7 @@ import {gettext} from 'core/utils'; import moment from 'moment-timezone'; import {appConfig} from 'appConfig'; import {IArticle} from 'superdesk-api'; +import {formatDate, longFormatDate} from 'core/get-superdesk-api-implementation'; const ISO_DATE_FORMAT = 'YYYY-MM-DD'; const ISO_WEEK_FORMAT = 'YYYY-W'; @@ -106,23 +107,36 @@ export function scheduledFormat(__item: IArticle): {short: string, long: string} const item = __item.archive_item ?? __item; - const datetime = item?.schedule_settings?.time_zone == null - ? moment(item.publish_schedule).tz(browserTimezone) - : moment.tz( - item.publish_schedule.replace('+0000', ''), - item.schedule_settings.time_zone, - ).tz(browserTimezone); + const datetime: {moment: moment.Moment, str: string} = (() => { + if (item?.schedule_settings?.time_zone == null) { + const momentObj = moment(item.publish_schedule).tz(browserTimezone); + + return { + moment: momentObj, + str: formatDate(momentObj), + }; + } else { + const momentObj = moment + .tz(item.publish_schedule.replace('+0000', ''), item.schedule_settings.time_zone) + .tz(browserTimezone); + + return { + moment: momentObj, + str: formatDate(momentObj), + }; + } + })(); var now = moment(); - const _date = datetime.format(appConfig.view.dateformat || 'MM/DD'), - _time = datetime.format(appConfig.view.timeformat || 'hh:mm'); + const _date = datetime.str, + _time = datetime.moment.format(appConfig.view.timeformat); - let short = isSameDay(datetime, now) ? '@ '.concat(_time) : _date.concat(' @ ', _time); + let short = isSameDay(datetime.moment, now) ? '@ '.concat(_time) : _date.concat(' @ ', _time); return { short: short, - long: longFormat(datetime), + long: longFormatDate(datetime.moment), }; } diff --git a/scripts/core/get-superdesk-api-implementation.tsx b/scripts/core/get-superdesk-api-implementation.tsx index 3f506a63a2..40346fa434 100644 --- a/scripts/core/get-superdesk-api-implementation.tsx +++ b/scripts/core/get-superdesk-api-implementation.tsx @@ -220,11 +220,39 @@ export function isArticleLockedInCurrentSession(article: IArticle): boolean { return ng.get('lock').isLockedInCurrentSession(article); } -export const formatDate = (date: Date | string) => ( - moment(date) - .tz(appConfig.default_timezone) - .format(appConfig.view.dateformat) -); +export const formatDate = (date: Date | string | moment.Moment, timezoneId?: string): string => { + const momentDate = moment.isMoment(date) === true ? date as moment.Moment : moment(date); + + if (timezoneId != null) { + return momentDate + .tz(timezoneId) + .format(appConfig.view.dateformat); + } else { + const timezone: 'browser' | 'server' = appConfig.view.timezone ?? 'browser'; + const keepLocalTime = timezone === 'browser'; + + return momentDate + .tz(appConfig.default_timezone, keepLocalTime) + .format(appConfig.view.dateformat); + } +}; + +export const longFormatDate = (date: Date | string | moment.Moment, timezoneId?: string): string => { + const momentDate = moment.isMoment(date) === true ? date as moment.Moment : moment(date); + + if (timezoneId != null) { + return momentDate + .tz(timezoneId) + .format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat); + } else { + const timezone: 'browser' | 'server' = appConfig.view.timezone ?? 'browser'; + const keepLocalTime = timezone === 'browser'; + + return momentDate + .tz(appConfig.default_timezone, keepLocalTime) + .format(appConfig.longDateFormat || 'LLL'); + } +}; export function dateToServerString(date: Date) { return date.toISOString().slice(0, 19) + '+0000';