Last active
August 17, 2017 00:12
-
-
Save sandcastle/5bf236729fb08b91261d24f3ef56904a to your computer and use it in GitHub Desktop.
Date and time service to help with UTC handling and formatting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ISecurityService } from './security'; | |
| import * as moment from 'moment-timezone'; | |
| const UTC_DATE_PARSE: string = 'YYYY-MM-DD'; | |
| export const DEFAULT_TIMEZONE: string = 'UTC'; | |
| /** | |
| * Provides time zone, date conversion and formatting support | |
| * based on the user's preferences. | |
| */ | |
| export interface IDateService { | |
| /** | |
| * Sets the current moment to the user's current timezone peference. | |
| * @param {String} [timezone] The timezone to set as the default. | |
| */ | |
| setDefaultTimezone(timezone?: string): void; | |
| /** | |
| * Gets the users current timezone. Defaults to UTC if not set. | |
| * @returns {moment.MomentZone} | |
| */ | |
| getUserTimezone(): moment.MomentZone; | |
| /** | |
| * Gets the users current timezone name. Defaults to UTC if not set. | |
| * @returns {String} | |
| */ | |
| getUserTimezoneName(): string; | |
| /** | |
| * Attempts to guess the users timezone based on the browser. | |
| * @returns {moment.MomentZone} | |
| */ | |
| guessTimezone(): moment.MomentZone; | |
| /** | |
| * Returns the user's current date as the UTC date. | |
| * | |
| * We always get the start of user's current local date and then | |
| * represent this as the UTC date. If we just got the current UTC | |
| * date, that might not match the user's local date. | |
| * | |
| * @returns {moment.MomentZone} | |
| */ | |
| getUtcDate(): moment.Moment; | |
| /** | |
| * Gets the current UTC date time. | |
| * @returns {moment.MomentZone} | |
| */ | |
| getUtcDateTime(): moment.Moment; | |
| /** | |
| * Formats the specified moment using the user's date preference. | |
| * @param {moment.Moment} value The moment to format. | |
| * @param {String} format The format to use. | |
| * @returns {String} | |
| */ | |
| format(value: moment.Moment, format: string): string; | |
| /** | |
| * Formats the specified moment using the user's date preference. | |
| * @param {moment.Moment} value The moment to format. | |
| * @returns {String} | |
| */ | |
| formatDate(value: moment.Moment): string; | |
| /** | |
| * Formats the specified moment using the user's time preference. | |
| * @param {moment.Moment} value The moment to format. | |
| * @returns {String} | |
| */ | |
| formatTime(value: moment.Moment): string; | |
| /** | |
| * Formats the specified moment using the user's date time preference. | |
| * @param {moment.Moment} value The moment to format. | |
| * @returns {String} | |
| */ | |
| formatDateTime(value: moment.Moment): string; | |
| /** | |
| * Parses the specified string as a UTC moment. | |
| * @param {String} value The date time to parse. | |
| * @param {String} [format] The format of the date to parse. | |
| * @returns {moment.Moment} | |
| */ | |
| parseUtc(value: string, format?: string): moment.Moment; | |
| /** | |
| * Parses a ISO 8601 date time string as a UTC moment. | |
| * @param {String} value The ISO 8601 string to parse. | |
| * @returns {moment.Moment} | |
| */ | |
| parseIso8601(value: string): moment.Moment; | |
| } | |
| export class DateService implements IDateService { | |
| constructor(private securityService: ISecurityService) { } | |
| setDefaultTimezone(timezone?: string): void { | |
| moment.tz.setDefault(timezone || this.getUserTimezoneName()); | |
| } | |
| getUserTimezone(): moment.MomentZone { | |
| return moment.tz.zone(this.getUserTimezoneName()); | |
| } | |
| getUserTimezoneName(): string { | |
| return this.securityService.getCurrentUser().timezone || DEFAULT_TIMEZONE; | |
| } | |
| guessTimezone(): moment.MomentZone { | |
| return moment.tz.zone(moment.tz.guess() || DEFAULT_TIMEZONE); | |
| } | |
| getUtcDate(): moment.Moment { | |
| return moment.utc(moment().local().startOf('day').format(UTC_DATE_PARSE), UTC_DATE_PARSE); | |
| } | |
| getUtcDateTime(): moment.Moment { | |
| return moment.utc(); | |
| } | |
| format(value: moment.Moment, format: string): string { | |
| return value.format(format); | |
| } | |
| formatDate(value: moment.Moment): string { | |
| // The date is stored as date time in UTC, there | |
| // is no need to convert to a local timezone as | |
| // its a date only component | |
| return value.format(this.securityService.getDateFormat()); | |
| } | |
| formatTime(value: moment.Moment): string { | |
| return value.tz(this.getUserTimezoneName()).format(this.securityService.getTimeFormat()); | |
| } | |
| formatDateTime(value: moment.Moment): string { | |
| return value.tz(this.getUserTimezoneName()).format(this.securityService.getDateTimeFormat()); | |
| } | |
| parseUtc(value: string, format?: string): moment.Moment { | |
| return moment.utc(value, format); | |
| } | |
| parseIso8601(value: string): moment.Moment { | |
| return moment.utc(value, moment.ISO_8601); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { User } from './user'; | |
| export interface ISecurityService { | |
| getCurrentUser(): User; | |
| getDateFormat(): string; | |
| getTimeFormat(): string; | |
| getDateTimeFormat(): string; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface User { | |
| timezone: string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment