Skip to content

Commit

Permalink
improve ussages of date formating
Browse files Browse the repository at this point in the history
  • Loading branch information
dzonidoo committed Jul 31, 2024
1 parent e7409f9 commit f515248
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 23 deletions.
8 changes: 8 additions & 0 deletions scripts/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down
5 changes: 2 additions & 3 deletions scripts/apps/archive/services/ArchiveService.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions scripts/apps/authoring-react/data-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
item: IArticle,
Expand Down Expand Up @@ -421,8 +421,10 @@ export const authoringStorageIArticleCorrect: IAuthoringStorage<IArticle> = {
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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) => {
Expand Down
34 changes: 24 additions & 10 deletions scripts/core/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
};
}

Expand Down
38 changes: 33 additions & 5 deletions scripts/core/get-superdesk-api-implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit f515248

Please sign in to comment.