Skip to content

Commit

Permalink
fix: message date localization (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgepp authored Jul 29, 2024
1 parent 7be9e05 commit 4802f7a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
44 changes: 44 additions & 0 deletions src/commons/utilis.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { getTimeLabel } from './utils';
import * as shell from '../carbonio-ui-commons/test/mocks/carbonio-shell-ui';
import defaultSettings from '../carbonio-ui-commons/test/mocks/settings/default-settings';

describe('getTimeLabel', () => {
describe('the date is formatted using local', () => {
test.each([
{ locale: 'en', output: 'MM/DD/YYYY', expected: '07/01/2020 12:00 AM' },
{ locale: 'it', output: 'DD/MM/YYYY', expected: '01/07/2020 00:00' }
])('when locale is $locale the output is $output', ({ locale, expected }) => {
shell.getUserSettings.mockReturnValueOnce({
...defaultSettings,
prefs: {
...defaultSettings.prefs,
zimbraPrefLocale: locale
}
});
const date = 1593554400000;
const timeLabel = getTimeLabel(date);

expect(timeLabel).toBe(expected);
});
});
test('if the date is today it will shows only the hours', () => {
jest.setSystemTime(new Date('2022-01-01'));
const date = Date.now();
const expected = '1:00 AM';
const timeLabel = getTimeLabel(date);
expect(timeLabel).toBe(expected);
});
test('if the date is not today it will shows date and hours', () => {
jest.setSystemTime(new Date('2022-01-01'));
const date = new Date('2021-01-01');
const expected = '01/01/2021 1:00 AM';
const timeLabel = getTimeLabel(date.getTime());
expect(timeLabel).toBe(expected);
});
});
13 changes: 4 additions & 9 deletions src/commons/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Account, t } from '@zextras/carbonio-shell-ui';
import { Account, getUserSettings, t } from '@zextras/carbonio-shell-ui';
import { find, isArray } from 'lodash';
import moment from 'moment';

import type { Participant } from '../types';

export const getTimeLabel = (date: number): string => {
const momentDate = moment(date);
const { zimbraPrefLocale = 'en' } = getUserSettings().prefs;
const momentDate = moment(date).locale(zimbraPrefLocale);
if (momentDate.isSame(new Date(), 'day')) {
return momentDate.format('LT');
}
if (momentDate.isSame(new Date(), 'week')) {
return momentDate.format('dddd, LT');
}
if (momentDate.isSame(new Date(), 'month')) {
return momentDate.format('DD MMMM');
}
return momentDate.format('DD/MM/YYYY');
return momentDate.format('L LT');
};

export const participantToString = (
Expand Down

0 comments on commit 4802f7a

Please sign in to comment.