Skip to content

Instantly share code, notes, and snippets.

@bonnici
Created November 25, 2012 23:01
Show Gist options
  • Save bonnici/4145794 to your computer and use it in GitHub Desktop.
Save bonnici/4145794 to your computer and use it in GitHub Desktop.
Util class for formatting dates in specific timezones (TypeScript)
/// <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