-
Notifications
You must be signed in to change notification settings - Fork 17
/
timezone.js
83 lines (66 loc) · 2 KB
/
timezone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Signals = imports.signals;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const People = Me.imports.people;
Math.trunc = Math.trunc || function(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
}
function _generateNiceOffset(offset) {
let absOffset = Math.abs(offset);
let h = Math.trunc(absOffset);
let m = 60 * (absOffset - h);
if (h < 10)
h = '0' + h;
if (m < 10)
m = '0' + m;
let r = h + ':' + m;
if (offset < 0)
r = '-' + r;
else
r = '+' + r;
return r;
}
var Timezone = new Lang.Class({
Name: 'Timezone',
_init: function(params) {
this._people = [];
this.topCity = '';
this.tz = params.tz;
this.tz1 = params.tz1;
this.offset = params.offset;
this.niceOffset= _generateNiceOffset(this.offset);
let localOffset = GLib.DateTime.new_now_local().get_utc_offset() / (3600*1000*1000);
this.sameAsSystem = this.offset == localOffset;
},
_updateTopCity: function() {
let count = {};
this._people.forEach(function(person) {
if (count[person.city] === undefined)
count[person.city] = 1;
else
count[person.city]++;
});
let result = '', m = 0;
for (let city in count) {
if ((count[city] > m) || (count[city] == m && city.length > 0 && city.length < result.length)) {
m = count[city];
result = city;
}
}
this.topCity = result;
},
addPerson: function(person) {
this._people.push(person);
this._updateTopCity();
person.connect('changed', Lang.bind(this, function() {
this._updateTopCity();
this.emit('changed');
}));
},
getPeople: function() {
return this._people;
}
});
Signals.addSignalMethods(Timezone.prototype);