From db0aba6cf8fbbccaa27edeb49be8f00d3efd93e8 Mon Sep 17 00:00:00 2001 From: Georgi Prodanov Date: Thu, 23 Feb 2017 17:23:28 +0200 Subject: [PATCH] #328128 Format dates according to event or user timezone. Fallback is UTC. --- app/services/events.service.ts | 2 ++ app/services/users.service.ts | 3 ++- cloud-code/notifyUsers.js | 25 ++++++++++--------------- package.json | 1 + 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/services/events.service.ts b/app/services/events.service.ts index 186f9c4..17b9d19 100644 --- a/app/services/events.service.ts +++ b/app/services/events.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { Data } from '../../node_modules/everlive-sdk/dist/declarations/everlive/types/Data'; import { Query } from '../../node_modules/everlive-sdk/dist/declarations/everlive/query/Query'; +import * as jstz from 'jstz'; import { EverliveProvider } from './everlive-provider.service'; import { EventRegistrationsService } from './event-registrations.service'; @@ -45,6 +46,7 @@ export class EventsService { create(event: Event) { this._clearExpandedFields(event); + (event as any).Timezone = jstz.determine().name(); return this._data.create(event); } diff --git a/app/services/users.service.ts b/app/services/users.service.ts index cdecb5b..d812c61 100644 --- a/app/services/users.service.ts +++ b/app/services/users.service.ts @@ -4,6 +4,7 @@ import { Users } from '../../node_modules/everlive-sdk/dist/declarations/everliv import { User as ServerUser } from '../../node_modules/everlive-sdk/dist/declarations/everlive/interfaces/User'; import { User } from '../shared' import { Observable, BehaviorSubject } from 'rxjs'; +import * as jstz from 'jstz'; import { utilities } from '../shared'; @@ -38,7 +39,7 @@ export class UsersService { register(username: string, password: string, displayName: string) { let attrs: any = { Email: username, DisplayName: displayName }; - attrs.TimezoneOffset = (new Date()).getTimezoneOffset(); + attrs.Timezone = jstz.determine().name(); return this._users.register(username, password, attrs); } diff --git a/cloud-code/notifyUsers.js b/cloud-code/notifyUsers.js index 9a381b8..7d60af1 100644 --- a/cloud-code/notifyUsers.js +++ b/cloud-code/notifyUsers.js @@ -125,18 +125,13 @@ function getDataForUserRegisteredForEvent (context) { }); } -function _offsetDate (dateIsoString, offset) { - var oneHour = 3600000; - var msInMinute = 60000; - var d = new Date(dateIsoString); - var offsetDateInMs = d.getTime() + (d.getTimezoneOffset() * msInMinute); - return new Date(offsetDateInMs + (oneHour * (offset || 0))); -} - -function _formatDate (dateIsoString, offset) { - var dateStr = _offsetDate(dateIsoString, offset).toString(); - var ind = dateStr.indexOf(' GMT'); - return dateStr.substring(0, ind); +function _formatDate (dateIsoString, timezoneName) { + var moment = require('moment-timezone'); + var result; + if (dateIsoString) { + result = moment.tz(dateIsoString, timezoneName).format('dddd, MMMM Do YYYY, h:mm:ss A'); + } + return result; } function getDataForEventRelated (templateName, context) { @@ -151,12 +146,12 @@ function getDataForEventRelated (templateName, context) { .then(function(organizerRes) { var organizer = organizerRes.result; event.Organizer = organizer.DisplayName; - var offset = organizer.TimezoneOffset; + var timezoneName = event.Timezone || organizer.Timezone; if (event.EventDate) { - event.EventDate = _formatDate(event.EventDate, offset); + event.EventDate = _formatDate(event.EventDate, timezoneName); } else { event.EventDateChoices = event.EventDateChoices.map(function(dateOption) { - return _formatDate(dateOption, offset); + return _formatDate(dateOption, timezoneName); }); } diff --git a/package.json b/package.json index e5ecfa4..92c7385 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@angular/platform-browser-dynamic": "2.4.6", "@angular/router": "3.4.6", "everlive-sdk": "1.9.2", + "jstz": "1.0.9", "nativescript-angular": "1.4.0", "nativescript-imagepicker": "~2.5.1", "nativescript-permissions": "^1.2.0",