From 88a19ea0663f58aa806110b438ef3c2cc244e8f6 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 27 Jul 2022 08:45:51 +0200 Subject: [PATCH 1/2] MOBILE-4116 moment: Force timezone for moment in behat tests --- package-lock.json | 8 ++++++++ package.json | 1 + src/addons/block/timeline/services/timeline.ts | 2 +- .../calendar/components/calendar/calendar.ts | 2 +- src/addons/calendar/pages/day/day.page.ts | 2 +- src/addons/calendar/services/calendar-helper.ts | 2 +- src/addons/calendar/services/calendar-sync.ts | 2 +- src/addons/calendar/services/calendar.ts | 2 +- .../messages/pages/discussion/discussion.page.ts | 2 +- .../mod/assign/tests/behat/basic_usage-310.feature | 4 ++-- .../mod/assign/tests/behat/basic_usage.feature | 4 ++-- src/addons/mod/chat/services/chat-helper.ts | 2 +- .../features/comments/pages/viewer/viewer.page.ts | 2 +- src/core/features/compile/services/compile.ts | 2 +- src/core/features/course/services/course-helper.ts | 2 +- .../features/courses/services/courses-helper.ts | 2 +- src/core/pipes/date-day-or-time.ts | 2 +- src/core/pipes/time-ago.ts | 2 +- src/core/services/lang.ts | 2 +- src/core/services/utils/time.ts | 2 +- src/core/singletons/logger.ts | 2 +- src/testing/testing.module.ts | 14 ++++++++++++-- 22 files changed, 42 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 635045583..f55d7a84e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23663,6 +23663,14 @@ "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.2.tgz", "integrity": "sha512-UgzG4rvxYpN15jgCmVJwac49h9ly9NurikMWGPdVxm8GZD6XjkKPxDTjQQ43gtGgnV3X0cAyWDdP2Wexoquifg==" }, + "moment-timezone": { + "version": "0.5.34", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.34.tgz", + "integrity": "sha512-3zAEHh2hKUs3EXLESx/wsgw6IQdusOT8Bxm3D9UrHPQR7zlMmzwybC8zHEM1tQ4LJwP7fcxrWr8tuBg05fFCbg==", + "requires": { + "moment": ">= 2.9.0" + } + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index ea93e1d63..609ca4529 100644 --- a/package.json +++ b/package.json @@ -119,6 +119,7 @@ "jszip": "^3.7.1", "mathjax": "2.7.7", "moment": "^2.29.2", + "moment-timezone": "^0.5.34", "nl.kingsquare.cordova.background-audio": "^1.0.1", "rxjs": "~6.5.5", "ts-md5": "^1.2.7", diff --git a/src/addons/block/timeline/services/timeline.ts b/src/addons/block/timeline/services/timeline.ts index 8b7f41bd7..144aa8fe9 100644 --- a/src/addons/block/timeline/services/timeline.ts +++ b/src/addons/block/timeline/services/timeline.ts @@ -22,7 +22,7 @@ import { AddonCalendarGetActionEventsByTimesortWSParams, AddonCalendarGetActionEventsByCoursesWSParams, } from '@addons/calendar/services/calendar'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { makeSingleton } from '@singletons'; import { CoreSiteWSPreSets } from '@classes/site'; diff --git a/src/addons/calendar/components/calendar/calendar.ts b/src/addons/calendar/components/calendar/calendar.ts index 1ff4753a3..f273bdf41 100644 --- a/src/addons/calendar/components/calendar/calendar.ts +++ b/src/addons/calendar/components/calendar/calendar.ts @@ -48,7 +48,7 @@ import { CoreSwipeSlidesDynamicItemsManagerSource, } from '@classes/items-management/swipe-slides-dynamic-items-manager-source'; import { CoreSwipeSlidesDynamicItemsManager } from '@classes/items-management/swipe-slides-dynamic-items-manager'; -import moment from 'moment'; +import moment from 'moment-timezone'; /** * Component that displays a calendar. diff --git a/src/addons/calendar/pages/day/day.page.ts b/src/addons/calendar/pages/day/day.page.ts index eea350110..839a3a7d5 100644 --- a/src/addons/calendar/pages/day/day.page.ts +++ b/src/addons/calendar/pages/day/day.page.ts @@ -32,7 +32,7 @@ import { AddonCalendarSync, AddonCalendarSyncProvider } from '../../services/cal import { CoreCategoryData, CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses'; import { CoreCoursesHelper } from '@features/courses/services/courses-helper'; import { AddonCalendarFilterComponent } from '../../components/filter/filter'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { NgZone } from '@singletons'; import { CoreNavigator } from '@services/navigator'; import { Params } from '@angular/router'; diff --git a/src/addons/calendar/services/calendar-helper.ts b/src/addons/calendar/services/calendar-helper.ts index 26bbe55ea..07201765f 100644 --- a/src/addons/calendar/services/calendar-helper.ts +++ b/src/addons/calendar/services/calendar-helper.ts @@ -31,7 +31,7 @@ import { CoreConfig } from '@services/config'; import { CoreUtils } from '@services/utils/utils'; import { CoreCourse } from '@features/course/services/course'; import { ContextLevel, CoreConstants } from '@/core/constants'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { makeSingleton } from '@singletons'; import { AddonCalendarSyncInvalidateEvent } from './calendar-sync'; import { AddonCalendarOfflineEventDBRecord } from './database/calendar-offline'; diff --git a/src/addons/calendar/services/calendar-sync.ts b/src/addons/calendar/services/calendar-sync.ts index 97bb81eae..47c78cbcd 100644 --- a/src/addons/calendar/services/calendar-sync.ts +++ b/src/addons/calendar/services/calendar-sync.ts @@ -29,7 +29,7 @@ import { AddonCalendarHelper } from './calendar-helper'; import { makeSingleton, Translate } from '@singletons'; import { CoreSync } from '@services/sync'; import { CoreNetworkError } from '@classes/errors/network-error'; -import moment from 'moment'; +import moment from 'moment-timezone'; /** * Service to sync calendar. diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts index 3e20b9706..dc5c47d18 100644 --- a/src/addons/calendar/services/calendar.ts +++ b/src/addons/calendar/services/calendar.ts @@ -27,7 +27,7 @@ import { ILocalNotification } from '@ionic-native/local-notifications'; import { AddonCalendarOffline } from './calendar-offline'; import { CoreUser } from '@features/user/services/user'; import { CoreWSExternalWarning, CoreWSDate } from '@services/ws'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { AddonCalendarEventDBRecord, AddonCalendarReminderDBRecord, EVENTS_TABLE, REMINDERS_TABLE } from './database/calendar'; import { CoreCourses } from '@features/courses/services/courses'; import { ContextLevel, CoreConstants } from '@/core/constants'; diff --git a/src/addons/messages/pages/discussion/discussion.page.ts b/src/addons/messages/pages/discussion/discussion.page.ts index 17cca33e9..f1ad3d33b 100644 --- a/src/addons/messages/pages/discussion/discussion.page.ts +++ b/src/addons/messages/pages/discussion/discussion.page.ts @@ -36,7 +36,7 @@ import { CoreLogger } from '@singletons/logger'; import { CoreApp } from '@services/app'; import { CoreInfiniteLoadingComponent } from '@components/infinite-loading/infinite-loading'; import { Md5 } from 'ts-md5/dist/md5'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { CoreError } from '@classes/errors/error'; import { Translate } from '@singletons'; import { CoreNavigator } from '@services/navigator'; diff --git a/src/addons/mod/assign/tests/behat/basic_usage-310.feature b/src/addons/mod/assign/tests/behat/basic_usage-310.feature index 532f7a134..4b89fe7e6 100755 --- a/src/addons/mod/assign/tests/behat/basic_usage-310.feature +++ b/src/addons/mod/assign/tests/behat/basic_usage-310.feature @@ -16,8 +16,8 @@ Feature: Test basic usage of assignment activity in app | teacher1 | C1 | editingteacher | | student1 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | intro | assignsubmission_onlinetext_enabled | duedate | attemptreopenmethod | - | assign | C1 | assign1 | assignment1 | Test assignment description1 | 1 | 1029844800 | manual | + | activity | course | idnumber | name | intro | assignsubmission_onlinetext_enabled | duedate | attemptreopenmethod | + | assign | C1 | assign1 | assignment1 | Test assignment description1 | 1 | ## 20 August 2002 12:00 PM ## | manual | Scenario: View assign description, due date & View list of student submissions (as teacher) & View own submission or student submission # Create, edit and submit as a student diff --git a/src/addons/mod/assign/tests/behat/basic_usage.feature b/src/addons/mod/assign/tests/behat/basic_usage.feature index 67e535139..f89f072a3 100755 --- a/src/addons/mod/assign/tests/behat/basic_usage.feature +++ b/src/addons/mod/assign/tests/behat/basic_usage.feature @@ -16,8 +16,8 @@ Feature: Test basic usage of assignment activity in app | teacher1 | C1 | editingteacher | | student1 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | intro | assignsubmission_onlinetext_enabled | duedate | attemptreopenmethod | - | assign | C1 | assign1 | assignment1 | Test assignment description1 | 1 | 1029844800 | manual | + | activity | course | idnumber | name | intro | assignsubmission_onlinetext_enabled | duedate | attemptreopenmethod | + | assign | C1 | assign1 | assignment1 | Test assignment description1 | 1 | ## 20 August 2002 12:00 PM ## | manual | @lms_from3.11 Scenario: View assign description, due date & View list of student submissions (as teacher) & View own submission or student submission diff --git a/src/addons/mod/chat/services/chat-helper.ts b/src/addons/mod/chat/services/chat-helper.ts index 9b2c03d06..2f4f6e02d 100644 --- a/src/addons/mod/chat/services/chat-helper.ts +++ b/src/addons/mod/chat/services/chat-helper.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; import { makeSingleton, Translate } from '@singletons'; -import * as moment from 'moment'; +import moment from 'moment-timezone'; import { AddonModChatMessage, AddonModChatSessionMessage } from './chat'; const patternTo = new RegExp(/^To\s([^:]+):(.*)/); diff --git a/src/core/features/comments/pages/viewer/viewer.page.ts b/src/core/features/comments/pages/viewer/viewer.page.ts index 32084a2ac..4d7ddf9b7 100644 --- a/src/core/features/comments/pages/viewer/viewer.page.ts +++ b/src/core/features/comments/pages/viewer/viewer.page.ts @@ -40,7 +40,7 @@ import { CoreCommentsDBRecord } from '@features/comments/services/database/comme import { CoreTimeUtils } from '@services/utils/time'; import { CoreApp } from '@services/app'; import { CoreNetwork } from '@services/network'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { Subscription } from 'rxjs'; import { CoreAnimations } from '@components/animations'; diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index 204ec0408..b18fa7a8a 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -72,7 +72,7 @@ import { DomSanitizer } from '@angular/platform-browser'; import { FormBuilder, Validators } from '@angular/forms'; import { HttpClient } from '@angular/common/http'; import { CoreConstants } from '@/core/constants'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { Md5 } from 'ts-md5/dist/md5'; // Import core classes that can be useful for site plugins. diff --git a/src/core/features/course/services/course-helper.ts b/src/core/features/course/services/course-helper.ts index f4f9703cd..094f08205 100644 --- a/src/core/features/course/services/course-helper.ts +++ b/src/core/features/course/services/course-helper.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; import { Params } from '@angular/router'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; import { diff --git a/src/core/features/courses/services/courses-helper.ts b/src/core/features/courses/services/courses-helper.ts index 688db4f43..f79686c9c 100644 --- a/src/core/features/courses/services/courses-helper.ts +++ b/src/core/features/courses/services/courses-helper.ts @@ -25,7 +25,7 @@ import { import { makeSingleton, Translate } from '@singletons'; import { CoreWSExternalFile } from '@services/ws'; import { AddonCourseCompletion } from '@addons/coursecompletion/services/coursecompletion'; -import moment from 'moment'; +import moment from 'moment-timezone'; /** * Helper to gather some common courses functions. diff --git a/src/core/pipes/date-day-or-time.ts b/src/core/pipes/date-day-or-time.ts index 504ce72b6..b59784a13 100644 --- a/src/core/pipes/date-day-or-time.ts +++ b/src/core/pipes/date-day-or-time.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Pipe, PipeTransform } from '@angular/core'; -import moment from 'moment'; +import moment from 'moment-timezone'; import { CoreTimeUtils } from '@services/utils/time'; import { Translate } from '@singletons'; diff --git a/src/core/pipes/time-ago.ts b/src/core/pipes/time-ago.ts index ea2657c53..b301c5751 100644 --- a/src/core/pipes/time-ago.ts +++ b/src/core/pipes/time-ago.ts @@ -15,7 +15,7 @@ import { Pipe, PipeTransform } from '@angular/core'; import { Translate } from '@singletons'; import { CoreLogger } from '@singletons/logger'; -import moment from 'moment'; +import moment from 'moment-timezone'; /** * Pipe to turn a UNIX timestamp to "time ago". diff --git a/src/core/services/lang.ts b/src/core/services/lang.ts index 67efec309..b7359f366 100644 --- a/src/core/services/lang.ts +++ b/src/core/services/lang.ts @@ -21,7 +21,7 @@ import { CoreConfig } from '@services/config'; import { CoreSubscriptions } from '@singletons/subscriptions'; import { makeSingleton, Translate, Http } from '@singletons'; -import * as moment from 'moment'; +import moment from 'moment-timezone'; import { CoreSite } from '../classes/site'; import { CorePlatform } from '@services/platform'; diff --git a/src/core/services/utils/time.ts b/src/core/services/utils/time.ts index 446181a79..bbc4a3e19 100644 --- a/src/core/services/utils/time.ts +++ b/src/core/services/utils/time.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; -import moment, { LongDateFormatKey } from 'moment'; +import moment, { LongDateFormatKey } from 'moment-timezone'; import { makeSingleton, Translate } from '@singletons'; import { CoreTime } from '@singletons/time'; diff --git a/src/core/singletons/logger.ts b/src/core/singletons/logger.ts index 6d59785b8..727c3f9bf 100644 --- a/src/core/singletons/logger.ts +++ b/src/core/singletons/logger.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import moment from 'moment'; +import moment from 'moment-timezone'; import { CoreConstants } from '@/core/constants'; diff --git a/src/testing/testing.module.ts b/src/testing/testing.module.ts index 9d78a5aa1..bc9171833 100644 --- a/src/testing/testing.module.ts +++ b/src/testing/testing.module.ts @@ -14,23 +14,33 @@ import { APP_INITIALIZER, NgModule } from '@angular/core'; import { CoreAppProvider } from '@services/app'; +import moment from 'moment-timezone'; import { TestingBehatRuntime, TestingBehatRuntimeService } from './services/behat-runtime'; type AutomatedTestsWindow = Window & { behat?: TestingBehatRuntimeService; }; -function initializeAutomatedTestsWindow(window: AutomatedTestsWindow) { +function initializeAutomatedTests(window: AutomatedTestsWindow) { if (!CoreAppProvider.isAutomated()) { return; } window.behat = TestingBehatRuntime.instance; + + // Force timezone for automated tests. Use the same timezone forced for LMS in tests. + moment.tz.setDefault('Australia/Perth'); } @NgModule({ providers: [ - { provide: APP_INITIALIZER, multi: true, useValue: () => initializeAutomatedTestsWindow(window) }, + { + provide: APP_INITIALIZER, + multi: true, + useValue: () => { + initializeAutomatedTests(window); + }, + }, ], }) export class TestingModule {} From 844c14b8cb09a29d5400f1e949f422e2fc2358d1 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 27 Jul 2022 10:13:21 +0200 Subject: [PATCH 2/2] MOBILE-4116 core: Avoid using Date instances if possible When the date can be affected by time zone, always use moment --- .../calendar/components/calendar/calendar.ts | 2 +- .../calendar/services/calendar-helper.ts | 8 +++---- .../calendar/services/handlers/view-link.ts | 15 ++++++------ .../pages/discussion/discussion.page.ts | 2 +- .../messages/services/messages-offline.ts | 2 +- .../mod/data/fields/date/component/date.ts | 23 +++++++++---------- src/addons/mod/data/pages/edit/edit.ts | 2 +- src/addons/mod/data/services/data-offline.ts | 2 +- .../mod/forum/services/forum-offline.ts | 4 ++-- .../mod/survey/services/survey-offline.ts | 2 +- .../components/user-avatar/user-avatar.ts | 2 +- src/core/features/compile/services/compile.ts | 2 ++ src/core/features/course/services/course.ts | 2 +- 13 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/addons/calendar/components/calendar/calendar.ts b/src/addons/calendar/components/calendar/calendar.ts index f273bdf41..0c8ef0178 100644 --- a/src/addons/calendar/components/calendar/calendar.ts +++ b/src/addons/calendar/components/calendar/calendar.ts @@ -509,7 +509,7 @@ class AddonCalendarMonthSlidesItemsManagerSource extends CoreSwipeSlidesDynamicI const weekDays = AddonCalendar.getWeekDays(result.daynames[0].dayno); const weeks = result.weeks as AddonCalendarWeek[]; - const currentDay = new Date().getDate(); + const currentDay = moment().date(); const currentTime = CoreTimeUtils.timestamp(); const preloadedMonth: PreloadedMonth = { diff --git a/src/addons/calendar/services/calendar-helper.ts b/src/addons/calendar/services/calendar-helper.ts index 07201765f..8e263037d 100644 --- a/src/addons/calendar/services/calendar-helper.ts +++ b/src/addons/calendar/services/calendar-helper.ts @@ -136,8 +136,8 @@ export class AddonCalendarHelperProvider { const result = {}; events.forEach((event) => { - const treatedDay = moment(new Date(event.timestart * 1000)); - const endDay = moment(new Date((event.timestart + event.timeduration) * 1000)); + const treatedDay = moment(event.timestart * 1000); + const endDay = moment((event.timestart + event.timeduration) * 1000); // Add the event to all the days it lasts. while (!treatedDay.isAfter(endDay, 'day')) { @@ -661,7 +661,7 @@ export class AddonCalendarHelperProvider { // Fetch months and days. fetchTimestarts.map((fetchTime) => { - const day = moment(new Date(fetchTime * 1000)); + const day = moment(fetchTime * 1000); const monthId = this.getMonthId(day); if (!treatedMonths[monthId]) { @@ -697,7 +697,7 @@ export class AddonCalendarHelperProvider { // Invalidate months and days. invalidateTimestarts.map((fetchTime) => { - const day = moment(new Date(fetchTime * 1000)); + const day = moment(fetchTime * 1000); const monthId = this.getMonthId(day); if (!treatedMonths[monthId]) { diff --git a/src/addons/calendar/services/handlers/view-link.ts b/src/addons/calendar/services/handlers/view-link.ts index 45914e697..fe7f1c0d2 100644 --- a/src/addons/calendar/services/handlers/view-link.ts +++ b/src/addons/calendar/services/handlers/view-link.ts @@ -20,6 +20,7 @@ import { CoreContentLinksAction } from '@features/contentlinks/services/contentl import { CoreNavigator } from '@services/navigator'; import { makeSingleton } from '@singletons'; import { AddonCalendar } from '../calendar'; +import moment from 'moment-timezone'; const SUPPORTED_VIEWS = ['month', 'mini', 'minithree', 'day', 'upcoming', 'upcoming_mini']; @@ -54,9 +55,9 @@ export class AddonCalendarViewLinkHandlerService extends CoreContentLinksHandler }; const timestamp = params.time ? Number(params.time) * 1000 : Date.now(); - const date = new Date(timestamp); - stateParams.year = date.getFullYear(); - stateParams.month = date.getMonth() + 1; + const momentInstance = moment(timestamp); + stateParams.year = momentInstance.year(); + stateParams.month = momentInstance.month() + 1; CoreNavigator.navigateToSitePath('/calendar/index', { params: stateParams, @@ -71,10 +72,10 @@ export class AddonCalendarViewLinkHandlerService extends CoreContentLinksHandler }; const timestamp = params.time ? Number(params.time) * 1000 : Date.now(); - const date = new Date(timestamp); - stateParams.year = date.getFullYear(); - stateParams.month = date.getMonth() + 1; - stateParams.day = date.getDate(); + const momentInstance = moment(timestamp); + stateParams.year = momentInstance.year(); + stateParams.month = momentInstance.month() + 1; + stateParams.day = momentInstance.date(); CoreNavigator.navigateToSitePath('/calendar/day', { params: stateParams, siteId }); diff --git a/src/addons/messages/pages/discussion/discussion.page.ts b/src/addons/messages/pages/discussion/discussion.page.ts index f1ad3d33b..325c226bb 100644 --- a/src/addons/messages/pages/discussion/discussion.page.ts +++ b/src/addons/messages/pages/discussion/discussion.page.ts @@ -1125,7 +1125,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView useridfrom: this.currentUserId, smallmessage: text, text: text, - timecreated: new Date().getTime(), + timecreated: Date.now(), }; message.showDate = this.showDate(message, this.messages[this.messages.length - 1]); this.addMessage(message, false); diff --git a/src/addons/messages/services/messages-offline.ts b/src/addons/messages/services/messages-offline.ts index 1e0942776..38ab67f3b 100644 --- a/src/addons/messages/services/messages-offline.ts +++ b/src/addons/messages/services/messages-offline.ts @@ -313,7 +313,7 @@ export class AddonMessagesOfflineProvider { touserid: toUserId, useridfrom: site.getUserId(), smallmessage: message, - timecreated: new Date().getTime(), + timecreated: Date.now(), deviceoffline: CoreNetwork.isOnline() ? 0 : 1, }; diff --git a/src/addons/mod/data/fields/date/component/date.ts b/src/addons/mod/data/fields/date/component/date.ts index 3ee554577..761953f89 100644 --- a/src/addons/mod/data/fields/date/component/date.ts +++ b/src/addons/mod/data/fields/date/component/date.ts @@ -15,6 +15,7 @@ import { Component } from '@angular/core'; import { CoreTimeUtils } from '@services/utils/time'; import { Translate } from '@singletons'; +import moment, { Moment } from 'moment-timezone'; import { AddonModDataFieldPluginBaseComponent } from '../../../classes/base-field-plugin-component'; /** @@ -43,7 +44,7 @@ export class AddonModDataFieldDateComponent extends AddonModDataFieldPluginBaseC return; } - let date: Date; + let momentInstance: Moment; // Calculate format to use. this.format = CoreTimeUtils.fixFormatForDatetime(CoreTimeUtils.convertPHPToMoment( @@ -55,27 +56,25 @@ export class AddonModDataFieldDateComponent extends AddonModDataFieldPluginBaseC if (this.searchMode) { this.addControl('f_' + this.field.id + '_z'); - date = this.searchFields!['f_' + this.field.id + '_y'] - ? new Date(this.searchFields!['f_' + this.field.id + '_y'] + '-' + + momentInstance = this.searchFields!['f_' + this.field.id + '_y'] + ? moment(this.searchFields!['f_' + this.field.id + '_y'] + '-' + this.searchFields!['f_' + this.field.id + '_m'] + '-' + this.searchFields!['f_' + this.field.id + '_d']) - : new Date(); + : moment(); - this.searchFields!['f_' + this.field.id] = CoreTimeUtils.toDatetimeFormat(date.getTime()); + this.searchFields!['f_' + this.field.id] = CoreTimeUtils.toDatetimeFormat(momentInstance.unix() * 1000); } else { - date = this.value?.content - ? new Date(parseInt(this.value.content, 10) * 1000) - : new Date(); + momentInstance = this.value?.content + ? moment(parseInt(this.value.content, 10) * 1000) + : moment(); } - const seconds = Math.floor(date.getTime() / 1000); - - this.addControl('f_' + this.field.id, CoreTimeUtils.toDatetimeFormat(seconds * 1000)); + this.addControl('f_' + this.field.id, CoreTimeUtils.toDatetimeFormat(momentInstance.unix() * 1000)); if (!this.searchMode && !this.value?.content) { this.onFieldInit.emit({ fieldid: this.field.id, - content: String(seconds), + content: String(momentInstance.unix()), }); } } diff --git a/src/addons/mod/data/pages/edit/edit.ts b/src/addons/mod/data/pages/edit/edit.ts index 094623ace..17cfb151d 100644 --- a/src/addons/mod/data/pages/edit/edit.ts +++ b/src/addons/mod/data/pages/edit/edit.ts @@ -270,7 +270,7 @@ export class AddonModDataEditPage implements OnInit { const modal = await CoreDomUtils.showModalLoading('core.sending', true); // Create an ID to assign files. - const entryTemp = this.entryId ? this.entryId : - (new Date().getTime()); + const entryTemp = this.entryId ? this.entryId : - (Date.now()); let editData: AddonModDataEntryWSField[] = []; try { diff --git a/src/addons/mod/data/services/data-offline.ts b/src/addons/mod/data/services/data-offline.ts index 4fa5d30b9..09884cde1 100644 --- a/src/addons/mod/data/services/data-offline.ts +++ b/src/addons/mod/data/services/data-offline.ts @@ -262,7 +262,7 @@ export class AddonModDataOfflineProvider { ): Promise { const site = await CoreSites.getSite(siteId); - timemodified = timemodified || new Date().getTime(); + timemodified = timemodified || Date.now(); entryId = entryId === undefined || entryId === null ? -timemodified : entryId; const entry: AddonModDataEntryDBRecord = { diff --git a/src/addons/mod/forum/services/forum-offline.ts b/src/addons/mod/forum/services/forum-offline.ts index 01d91e606..cbc4767a4 100644 --- a/src/addons/mod/forum/services/forum-offline.ts +++ b/src/addons/mod/forum/services/forum-offline.ts @@ -170,7 +170,7 @@ export class AddonModForumOfflineProvider { options: JSON.stringify(options || {}), groupid: groupId || AddonModForumProvider.ALL_PARTICIPANTS, userid: userId || site.getUserId(), - timecreated: timeCreated || new Date().getTime(), + timecreated: timeCreated || Date.now(), }; await site.getDb().insertRecord(DISCUSSIONS_TABLE, data); @@ -325,7 +325,7 @@ export class AddonModForumOfflineProvider { message: message, options: JSON.stringify(options || {}), userid: userId || site.getUserId(), - timecreated: new Date().getTime(), + timecreated: Date.now(), }; await site.getDb().insertRecord(REPLIES_TABLE, data); diff --git a/src/addons/mod/survey/services/survey-offline.ts b/src/addons/mod/survey/services/survey-offline.ts index 0a0f468eb..e13e61c70 100644 --- a/src/addons/mod/survey/services/survey-offline.ts +++ b/src/addons/mod/survey/services/survey-offline.ts @@ -137,7 +137,7 @@ export class AddonModSurveyOfflineProvider { courseid: courseId, userid: userId, answers: JSON.stringify(answers), - timecreated: new Date().getTime(), + timecreated: Date.now(), }; await site.getDb().insertRecord(SURVEY_TABLE, entry); diff --git a/src/core/components/user-avatar/user-avatar.ts b/src/core/components/user-avatar/user-avatar.ts index 85a087c19..3c6923e7a 100644 --- a/src/core/components/user-avatar/user-avatar.ts +++ b/src/core/components/user-avatar/user-avatar.ts @@ -114,7 +114,7 @@ export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy { if (this.user.lastaccess) { // If the time has passed, don't show the online status. - const time = new Date().getTime() - this.timetoshowusers; + const time = Date.now() - this.timetoshowusers; return this.user.lastaccess * 1000 >= time; } else { diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index b18fa7a8a..ff2d9cb8e 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -82,6 +82,7 @@ import { CoreComponentsRegistry } from '@singletons/components-registry'; import { CoreDom } from '@singletons/dom'; import { CoreForms } from '@singletons/form'; import { CoreText } from '@singletons/text'; +import { CoreTime } from '@singletons/time'; import { CoreUrl } from '@singletons/url'; import { CoreWindow } from '@singletons/window'; import { CoreCache } from '@classes/cache'; @@ -354,6 +355,7 @@ export class CoreCompileProvider { instance['CoreDom'] = CoreDom; instance['CoreForms'] = CoreForms; instance['CoreText'] = CoreText; + instance['CoreTime'] = CoreTime; instance['CoreUrl'] = CoreUrl; instance['CoreWindow'] = CoreWindow; instance['CoreCache'] = CoreCache; diff --git a/src/core/features/course/services/course.ts b/src/core/features/course/services/course.ts index 0e987dc12..4ad01748f 100644 --- a/src/core/features/course/services/course.ts +++ b/src/core/features/course/services/course.ts @@ -1418,7 +1418,7 @@ export class CoreCourseProvider { id: courseId, status: status, previous: previousStatus, - updated: new Date().getTime(), + updated: Date.now(), downloadTime: downloadTime, previousDownloadTime: previousDownloadTime, });