Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Last active May 26, 2016 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robksawyer/98df7bb13d5efeac5dde to your computer and use it in GitHub Desktop.
Save robksawyer/98df7bb13d5efeac5dde to your computer and use it in GitHub Desktop.
An Angular service that helps with Timezone related things.
/**
* Bower dependencies:
* timezone-js
* moment
* moment-timezone
* angular-moment
* angular-tz-extensions
*
* index.html:
* <script src="lib/timezone-js/src/date.js"></script>
* <script src="http://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.js"></script>
* <script src="lib/moment/min/moment-with-locales.min.js"></script>
* <script src="lib/moment-timezone/builds/moment-timezone-with-data.min.js"></script>
* <script src="lib/moment-timezone/moment-timezone-utils.js"></script>
* <script src="lib/angular-moment/angular-moment.min.js"></script>
* <script src="lib/angular-tz-extensions/lib/angular-tz-extensions.min.js"></script>
*/
angular.module('curdcollective.services', [
'angularMoment', 'Timezones'
])
/**
* TimezoneService
* Helper for Timezone related things
* @url https://github.com/chouseknecht/angular-tz-extensions
* @url https://github.com/urish/angular-moment
* @url http://momentjs.com/timezone/
*/
.service('TimezoneService', function(moment, $timezones, $filter, angularMomentConfig) {
//Get a list of timezones (via moment-timezone)
var timezones = [];
var autoDetectedTimezone = $timezones.getLocal() || 'UTC';
function pad(value) {
return value < 10 ? '0' + value : value;
}
function setDefaults(timezone){
if(!this.autoDetectedTimezone && !timezone){
return 'You need to detect the timezone first.';
}
//Set the default timezone
moment.tz.setDefault(this.autoDetectedTimezone);
angularMomentConfig.timezone = this.autoDetectedTimezone;
}
return {
//
//Initializes the timezone methods and loads required variables
//
initTimezones: function(){
this.timezones = [];
angular.forEach(moment.tz.names(), function (zone, key) {
this.push({
name: zone,
abbr: moment.tz(zone).zoneAbbr(),
offset: moment.tz(zone).format('Z')
});
}, this.timezones);
},
getTimezoneOffset: function(tz){
if(!tz){
tz = this.autoDetectedTimezone;
}
var rightNow = new Date();
var tzAlign = $timezones.align(rightNow, tz);
return tzAlign.getTimezoneOffset();
},
getHours: function(tz){
if(!tz){
tz = this.autoDetectedTimezone;
}
var rightNow = new Date();
var tzAlign = $timezones.align(rightNow, tz);
return tzAlign.getHours();
},
getGMTOffset: function(tz){
if(!tz){
tz = this.autoDetectedTimezone;
}
var rightNow = new Date();
var tzAlign = $timezones.align(rightNow, tz);
return $filter('date')(tzAlign,'Z');
},
//See https://github.com/chouseknecht/angular-tz-extensions
getLocale: function(tz){
if(!tz){
tz = this.autoDetectedTimezone;
}
return tz.locality;
},
//Handles setting the default timezone for the app.
setDefaults: function(timezone){
if(!this.autoDetectedTimezone && !timezone){
return 'You need to detect the timezone first.';
}
//Set the default timezone
moment.tz.setDefault(this.autoDetectedTimezone);
angularMomentConfig.timezone = this.autoDetectedTimezone;
},
//Handles auto-detecting the user's timezone
detect: function(){
this.autoDetectedTimezone = $timezones.getLocal() || 'UTC';
setDefaults(this.autoDetectedTimezone.name);
return this.autoDetectedTimezone.name;
},
//Retrieves a list of all timezones known
getTimezones: function(){
return this.timezones;
},
//DEPRECATED
/*createOffset: function(date) {
var sign = (date.getTimezoneOffset() > 0) ? '-' : '+';
var offset = Math.abs(date.getTimezoneOffset());
var hours = pad(Math.floor(offset / 60));
var minutes = pad(offset % 60);
return sign + hours + ':' + minutes;
}*/
};
});
@jdnichollsc
Copy link

Any example? 👯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment