From 1de67af0c8f4c429b5e43c88581f7121ed4006ee Mon Sep 17 00:00:00 2001 From: jsconan Date: Thu, 20 May 2021 12:23:24 +0200 Subject: [PATCH 1/3] feat: add support for UTC timestamp to the helper formatDateTime() in util/locale --- src/util/locale.js | 12 +++-- test/util/locale/momentMock.js | 54 ++++++++++++++++++++ test/util/locale/test.html | 6 +++ test/util/locale/test.js | 93 ++++++++++++++++++++++++++++++++-- 4 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 test/util/locale/momentMock.js diff --git a/src/util/locale.js b/src/util/locale.js index ecec6de..5c9d12c 100644 --- a/src/util/locale.js +++ b/src/util/locale.js @@ -13,7 +13,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2016-2019 (original work) Open Assessment Technologies SA; + * Copyright (c) 2016-2021 (original work) Open Assessment Technologies SA; * */ @@ -115,11 +115,13 @@ export default { /** * Parse unix timestamp - * Note that user's (browser's) timezone will be used. - * @param {Number} timestamp + * Note that user's (browser's) timezone will be used by default, unless the utc parameter is set to true. + * @param {Number} timestamp - The timestamp to format. It is considered as in the target timezone. + * @param {Boolean} [utc=false] - For the UTC timezone. By default the user's timezone will be used. * @return string */ - formatDateTime: function(timestamp) { - return moment(timestamp, 'X').format(this.getDateTimeFormat()); + formatDateTime(timestamp, utc = false) { + const datetime = utc ? moment.utc(timestamp, 'X') : moment(timestamp, 'X'); + return datetime.format(this.getDateTimeFormat()); } }; diff --git a/test/util/locale/momentMock.js b/test/util/locale/momentMock.js new file mode 100644 index 0000000..b64a567 --- /dev/null +++ b/test/util/locale/momentMock.js @@ -0,0 +1,54 @@ +/** + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; under version 2 + * of the License (non-upgradable). + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright (c) 2021 Open Assessment Technologies SA ; + */ +/** + * @author Jean-Sébastien Conan + */ +define([ + 'core/eventifier' +], function (eventifier) { + 'use strict'; + + const mockValues = {}; + + const momentMock = eventifier(function (...args) { + momentMock.trigger('moment', ...args); + return momentMockFactory(...args); + }); + + function momentMockFactory(...factoryArgs) { + momentMock.trigger('factory', ...factoryArgs); + + return { + format(...args) { + momentMock.trigger('format', ...args); + return mockValues.format; + } + }; + } + + return Object.assign(momentMock, { + utc(...args) { + momentMock.trigger('utc', ...args); + return momentMockFactory(...args); + }, + + mockFormat(value) { + mockValues.format = value; + } + }); +}); \ No newline at end of file diff --git a/test/util/locale/test.html b/test/util/locale/test.html index bc891fd..f41a0dc 100644 --- a/test/util/locale/test.html +++ b/test/util/locale/test.html @@ -7,6 +7,12 @@