corrade-nucleus-nucleons – Blame information for rev 38

Subversion Repositories:
Rev:
Rev Author Line No. Line
38 office 1 import zeroFill from '../utils/zero-fill';
2 import { createDuration } from '../duration/create';
3 import { addSubtract } from '../moment/add-subtract';
4 import { isMoment, copyConfig } from '../moment/constructor';
5 import { addFormatToken } from '../format/format';
6 import { addRegexToken, matchOffset, matchShortOffset } from '../parse/regex';
7 import { addParseToken } from '../parse/token';
8 import { createLocal } from '../create/local';
9 import { prepareConfig } from '../create/from-anything';
10 import { createUTC } from '../create/utc';
11 import isDate from '../utils/is-date';
12 import toInt from '../utils/to-int';
13 import isUndefined from '../utils/is-undefined';
14 import compareArrays from '../utils/compare-arrays';
15 import { hooks } from '../utils/hooks';
16  
17 // FORMATTING
18  
19 function offset (token, separator) {
20 addFormatToken(token, 0, 0, function () {
21 var offset = this.utcOffset();
22 var sign = '+';
23 if (offset < 0) {
24 offset = -offset;
25 sign = '-';
26 }
27 return sign + zeroFill(~~(offset / 60), 2) + separator + zeroFill(~~(offset) % 60, 2);
28 });
29 }
30  
31 offset('Z', ':');
32 offset('ZZ', '');
33  
34 // PARSING
35  
36 addRegexToken('Z', matchShortOffset);
37 addRegexToken('ZZ', matchShortOffset);
38 addParseToken(['Z', 'ZZ'], function (input, array, config) {
39 config._useUTC = true;
40 config._tzm = offsetFromString(matchShortOffset, input);
41 });
42  
43 // HELPERS
44  
45 // timezone chunker
46 // '+10:00' > ['10', '00']
47 // '-1530' > ['-15', '30']
48 var chunkOffset = /([\+\-]|\d\d)/gi;
49  
50 function offsetFromString(matcher, string) {
51 var matches = (string || '').match(matcher);
52  
53 if (matches === null) {
54 return null;
55 }
56  
57 var chunk = matches[matches.length - 1] || [];
58 var parts = (chunk + '').match(chunkOffset) || ['-', 0, 0];
59 var minutes = +(parts[1] * 60) + toInt(parts[2]);
60  
61 return minutes === 0 ?
62  
63 parts[0] === '+' ? minutes : -minutes;
64 }
65  
66 // Return a moment from input, that is local/utc/zone equivalent to model.
67 export function cloneWithOffset(input, model) {
68 var res, diff;
69 if (model._isUTC) {
70 res = model.clone();
71 diff = (isMoment(input) || isDate(input) ? input.valueOf() : createLocal(input).valueOf()) - res.valueOf();
72 // Use low-level api, because this fn is low-level api.
73 res._d.setTime(res._d.valueOf() + diff);
74 hooks.updateOffset(res, false);
75 return res;
76 } else {
77 return createLocal(input).local();
78 }
79 }
80  
81 function getDateOffset (m) {
82 // On Firefox.24 Date#getTimezoneOffset returns a floating point.
83 // https://github.com/moment/moment/pull/1871
84 return -Math.round(m._d.getTimezoneOffset() / 15) * 15;
85 }
86  
87 // HOOKS
88  
89 // This function will be called whenever a moment is mutated.
90 // It is intended to keep the offset in sync with the timezone.
91 hooks.updateOffset = function () {};
92  
93 // MOMENTS
94  
95 // keepLocalTime = true means only change the timezone, without
96 // affecting the local hour. So 5:31:26 +0300 --[utcOffset(2, true)]-->
97 // 5:31:26 +0200 It is possible that 5:31:26 doesn't exist with offset
98 // +0200, so we adjust the time as needed, to be valid.
99 //
100 // Keeping the time actually adds/subtracts (one hour)
101 // from the actual represented time. That is why we call updateOffset
102 // a second time. In case it wants us to change the offset again
103 // _changeInProgress == true case, then we have to adjust, because
104 // there is no such time in the given timezone.
105 export function getSetOffset (input, keepLocalTime, keepMinutes) {
106 var offset = this._offset || 0,
107 localAdjust;
108 if (!this.isValid()) {
109 return input != null ? this : NaN;
110 }
111 if (input != null) {
112 if (typeof input === 'string') {
113 input = offsetFromString(matchShortOffset, input);
114 if (input === null) {
115 return this;
116 }
117 } else if (Math.abs(input) < 16 && !keepMinutes) {
118 input = input * 60;
119 }
120 if (!this._isUTC && keepLocalTime) {
121 localAdjust = getDateOffset(this);
122 }
123 this._offset = input;
124 this._isUTC = true;
125 if (localAdjust != null) {
126 this.add(localAdjust, 'm');
127 }
128 if (offset !== input) {
129 if (!keepLocalTime || this._changeInProgress) {
130 addSubtract(this, createDuration(input - offset, 'm'), 1, false);
131 } else if (!this._changeInProgress) {
132 this._changeInProgress = true;
133 hooks.updateOffset(this, true);
134 this._changeInProgress = null;
135 }
136 }
137 return this;
138 } else {
139 return this._isUTC ? offset : getDateOffset(this);
140 }
141 }
142  
143 export function getSetZone (input, keepLocalTime) {
144 if (input != null) {
145 if (typeof input !== 'string') {
146 input = -input;
147 }
148  
149 this.utcOffset(input, keepLocalTime);
150  
151 return this;
152 } else {
153 return -this.utcOffset();
154 }
155 }
156  
157 export function setOffsetToUTC (keepLocalTime) {
158 return this.utcOffset(0, keepLocalTime);
159 }
160  
161 export function setOffsetToLocal (keepLocalTime) {
162 if (this._isUTC) {
163 this.utcOffset(0, keepLocalTime);
164 this._isUTC = false;
165  
166 if (keepLocalTime) {
167 this.subtract(getDateOffset(this), 'm');
168 }
169 }
170 return this;
171 }
172  
173 export function setOffsetToParsedOffset () {
174 if (this._tzm != null) {
175 this.utcOffset(this._tzm, false, true);
176 } else if (typeof this._i === 'string') {
177 var tZone = offsetFromString(matchOffset, this._i);
178 if (tZone != null) {
179 this.utcOffset(tZone);
180 }
181 else {
182 this.utcOffset(0, true);
183 }
184 }
185 return this;
186 }
187  
188 export function hasAlignedHourOffset (input) {
189 if (!this.isValid()) {
190 return false;
191 }
192 input = input ? createLocal(input).utcOffset() : 0;
193  
194 return (this.utcOffset() - input) % 60 === 0;
195 }
196  
197 export function isDaylightSavingTime () {
198 return (
199 this.utcOffset() > this.clone().month(0).utcOffset() ||
200 this.utcOffset() > this.clone().month(5).utcOffset()
201 );
202 }
203  
204 export function isDaylightSavingTimeShifted () {
205 if (!isUndefined(this._isDSTShifted)) {
206 return this._isDSTShifted;
207 }
208  
209 var c = {};
210  
211 copyConfig(c, this);
212 c = prepareConfig(c);
213  
214 if (c._a) {
215 var other = c._isUTC ? createUTC(c._a) : createLocal(c._a);
216 this._isDSTShifted = this.isValid() &&
217 compareArrays(c._a, other.toArray()) > 0;
218 } else {
219 this._isDSTShifted = false;
220 }
221  
222 return this._isDSTShifted;
223 }
224  
225 export function isLocal () {
226 return this.isValid() ? !this._isUTC : false;
227 }
228  
229 export function isUtcOffset () {
230 return this.isValid() ? this._isUTC : false;
231 }
232  
233 export function isUtc () {
234 return this.isValid() ? this._isUTC && this._offset === 0 : false;
235 }