Created
November 25, 2012 23:01
-
-
Save bonnici/4145794 to your computer and use it in GitHub Desktop.
Util class for formatting dates in specific timezones (TypeScript)
This file contains 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
/// <reference path="./node-definitions/moment.d.ts" /> | |
/// <reference path="./node-definitions/timezone-js.d.ts" /> | |
import moment = module("moment") | |
import tzjs = module("timezone-js") | |
import fs = module('fs'); | |
export function init() { | |
tzjs.timezone.zoneFileBasePath = './tz'; | |
tzjs.timezone.loadingScheme = tzjs.timezone.loadingSchemes.PRELOAD_ALL; | |
tzjs.timezone.transport = function (opts) { | |
if (opts.async) { | |
if (typeof opts.success !== 'function') return; | |
opts.error = opts.error || console.error; | |
return fs.readFile(opts.url, 'utf8', function (err, data) { | |
return err ? opts.error(err) : opts.success(data); | |
}); | |
} | |
return fs.readFileSync(opts.url, 'utf8'); | |
}; | |
tzjs.timezone.init({ async: false }); | |
} | |
export function momentInTimezone(timezoneName: string): moment.Moment { | |
var timezone = new tzjs.Date(timezoneName); | |
return moment.utc().subtract('minutes', timezone.getTimezoneOffset()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment