diff options
Diffstat (limited to 'library/moment/src/lib/units')
-rw-r--r-- | library/moment/src/lib/units/aliases.js | 29 | ||||
-rw-r--r-- | library/moment/src/lib/units/constants.js | 7 | ||||
-rw-r--r-- | library/moment/src/lib/units/day-of-month.js | 32 | ||||
-rw-r--r-- | library/moment/src/lib/units/day-of-week.js | 134 | ||||
-rw-r--r-- | library/moment/src/lib/units/day-of-year.js | 49 | ||||
-rw-r--r-- | library/moment/src/lib/units/hour.js | 77 | ||||
-rw-r--r-- | library/moment/src/lib/units/millisecond.js | 64 | ||||
-rw-r--r-- | library/moment/src/lib/units/minute.js | 24 | ||||
-rw-r--r-- | library/moment/src/lib/units/month.js | 129 | ||||
-rw-r--r-- | library/moment/src/lib/units/offset.js | 213 | ||||
-rw-r--r-- | library/moment/src/lib/units/quarter.js | 27 | ||||
-rw-r--r-- | library/moment/src/lib/units/second.js | 24 | ||||
-rw-r--r-- | library/moment/src/lib/units/timestamp.js | 20 | ||||
-rw-r--r-- | library/moment/src/lib/units/timezone.js | 16 | ||||
-rw-r--r-- | library/moment/src/lib/units/units.js | 20 | ||||
-rw-r--r-- | library/moment/src/lib/units/week-year.js | 78 | ||||
-rw-r--r-- | library/moment/src/lib/units/week.js | 88 | ||||
-rw-r--r-- | library/moment/src/lib/units/year.js | 62 |
18 files changed, 1093 insertions, 0 deletions
diff --git a/library/moment/src/lib/units/aliases.js b/library/moment/src/lib/units/aliases.js new file mode 100644 index 000000000..4ad513384 --- /dev/null +++ b/library/moment/src/lib/units/aliases.js @@ -0,0 +1,29 @@ +import hasOwnProp from '../utils/has-own-prop'; + +var aliases = {}; + +export function addUnitAlias (unit, shorthand) { + var lowerCase = unit.toLowerCase(); + aliases[lowerCase] = aliases[lowerCase + 's'] = aliases[shorthand] = unit; +} + +export function normalizeUnits(units) { + return typeof units === 'string' ? aliases[units] || aliases[units.toLowerCase()] : undefined; +} + +export function normalizeObjectUnits(inputObject) { + var normalizedInput = {}, + normalizedProp, + prop; + + for (prop in inputObject) { + if (hasOwnProp(inputObject, prop)) { + normalizedProp = normalizeUnits(prop); + if (normalizedProp) { + normalizedInput[normalizedProp] = inputObject[prop]; + } + } + } + + return normalizedInput; +} diff --git a/library/moment/src/lib/units/constants.js b/library/moment/src/lib/units/constants.js new file mode 100644 index 000000000..588e70db5 --- /dev/null +++ b/library/moment/src/lib/units/constants.js @@ -0,0 +1,7 @@ +export var YEAR = 0; +export var MONTH = 1; +export var DATE = 2; +export var HOUR = 3; +export var MINUTE = 4; +export var SECOND = 5; +export var MILLISECOND = 6; diff --git a/library/moment/src/lib/units/day-of-month.js b/library/moment/src/lib/units/day-of-month.js new file mode 100644 index 000000000..dc32acc24 --- /dev/null +++ b/library/moment/src/lib/units/day-of-month.js @@ -0,0 +1,32 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2 } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { DATE } from './constants'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken('D', ['DD', 2], 'Do', 'date'); + +// ALIASES + +addUnitAlias('date', 'D'); + +// PARSING + +addRegexToken('D', match1to2); +addRegexToken('DD', match1to2, match2); +addRegexToken('Do', function (isStrict, locale) { + return isStrict ? locale._ordinalParse : locale._ordinalParseLenient; +}); + +addParseToken(['D', 'DD'], DATE); +addParseToken('Do', function (input, array) { + array[DATE] = toInt(input.match(match1to2)[0], 10); +}); + +// MOMENTS + +export var getSetDayOfMonth = makeGetSet('Date', true); diff --git a/library/moment/src/lib/units/day-of-week.js b/library/moment/src/lib/units/day-of-week.js new file mode 100644 index 000000000..a82f12630 --- /dev/null +++ b/library/moment/src/lib/units/day-of-week.js @@ -0,0 +1,134 @@ +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, matchWord } from '../parse/regex'; +import { addWeekParseToken } from '../parse/token'; +import toInt from '../utils/to-int'; +import { createLocal } from '../create/local'; +import getParsingFlags from '../create/parsing-flags'; + +// FORMATTING + +addFormatToken('d', 0, 'do', 'day'); + +addFormatToken('dd', 0, 0, function (format) { + return this.localeData().weekdaysMin(this, format); +}); + +addFormatToken('ddd', 0, 0, function (format) { + return this.localeData().weekdaysShort(this, format); +}); + +addFormatToken('dddd', 0, 0, function (format) { + return this.localeData().weekdays(this, format); +}); + +addFormatToken('e', 0, 0, 'weekday'); +addFormatToken('E', 0, 0, 'isoWeekday'); + +// ALIASES + +addUnitAlias('day', 'd'); +addUnitAlias('weekday', 'e'); +addUnitAlias('isoWeekday', 'E'); + +// PARSING + +addRegexToken('d', match1to2); +addRegexToken('e', match1to2); +addRegexToken('E', match1to2); +addRegexToken('dd', matchWord); +addRegexToken('ddd', matchWord); +addRegexToken('dddd', matchWord); + +addWeekParseToken(['dd', 'ddd', 'dddd'], function (input, week, config) { + var weekday = config._locale.weekdaysParse(input); + // if we didn't get a weekday name, mark the date as invalid + if (weekday != null) { + week.d = weekday; + } else { + getParsingFlags(config).invalidWeekday = input; + } +}); + +addWeekParseToken(['d', 'e', 'E'], function (input, week, config, token) { + week[token] = toInt(input); +}); + +// HELPERS + +function parseWeekday(input, locale) { + if (typeof input !== 'string') { + return input; + } + + if (!isNaN(input)) { + return parseInt(input, 10); + } + + input = locale.weekdaysParse(input); + if (typeof input === 'number') { + return input; + } + + return null; +} + +// LOCALES + +export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'); +export function localeWeekdays (m) { + return this._weekdays[m.day()]; +} + +export var defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'); +export function localeWeekdaysShort (m) { + return this._weekdaysShort[m.day()]; +} + +export var defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'); +export function localeWeekdaysMin (m) { + return this._weekdaysMin[m.day()]; +} + +export function localeWeekdaysParse (weekdayName) { + var i, mom, regex; + + this._weekdaysParse = this._weekdaysParse || []; + + for (i = 0; i < 7; i++) { + // make the regex if we don't have it already + if (!this._weekdaysParse[i]) { + mom = createLocal([2000, 1]).day(i); + regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, ''); + this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i'); + } + // test the regex + if (this._weekdaysParse[i].test(weekdayName)) { + return i; + } + } +} + +// MOMENTS + +export function getSetDayOfWeek (input) { + var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay(); + if (input != null) { + input = parseWeekday(input, this.localeData()); + return this.add(input - day, 'd'); + } else { + return day; + } +} + +export function getSetLocaleDayOfWeek (input) { + var weekday = (this.day() + 7 - this.localeData()._week.dow) % 7; + return input == null ? weekday : this.add(input - weekday, 'd'); +} + +export function getSetISODayOfWeek (input) { + // behaves the same as moment#day except + // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6) + // as a setter, sunday should belong to the previous week. + return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7); +} diff --git a/library/moment/src/lib/units/day-of-year.js b/library/moment/src/lib/units/day-of-year.js new file mode 100644 index 000000000..0c34fa395 --- /dev/null +++ b/library/moment/src/lib/units/day-of-year.js @@ -0,0 +1,49 @@ +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match3, match1to3 } from '../parse/regex'; +import { daysInYear } from './year'; +import { createUTCDate } from '../create/date-from-array'; +import { addParseToken } from '../parse/token'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken('DDD', ['DDDD', 3], 'DDDo', 'dayOfYear'); + +// ALIASES + +addUnitAlias('dayOfYear', 'DDD'); + +// PARSING + +addRegexToken('DDD', match1to3); +addRegexToken('DDDD', match3); +addParseToken(['DDD', 'DDDD'], function (input, array, config) { + config._dayOfYear = toInt(input); +}); + +// HELPERS + +//http://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday +export function dayOfYearFromWeeks(year, week, weekday, firstDayOfWeekOfYear, firstDayOfWeek) { + var week1Jan = 6 + firstDayOfWeek - firstDayOfWeekOfYear, janX = createUTCDate(year, 0, 1 + week1Jan), d = janX.getUTCDay(), dayOfYear; + if (d < firstDayOfWeek) { + d += 7; + } + + weekday = weekday != null ? 1 * weekday : firstDayOfWeek; + + dayOfYear = 1 + week1Jan + 7 * (week - 1) - d + weekday; + + return { + year: dayOfYear > 0 ? year : year - 1, + dayOfYear: dayOfYear > 0 ? dayOfYear : daysInYear(year - 1) + dayOfYear + }; +} + +// MOMENTS + +export function getSetDayOfYear (input) { + var dayOfYear = Math.round((this.clone().startOf('day') - this.clone().startOf('year')) / 864e5) + 1; + return input == null ? dayOfYear : this.add((input - dayOfYear), 'd'); +} diff --git a/library/moment/src/lib/units/hour.js b/library/moment/src/lib/units/hour.js new file mode 100644 index 000000000..356c74d23 --- /dev/null +++ b/library/moment/src/lib/units/hour.js @@ -0,0 +1,77 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2 } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { HOUR } from './constants'; +import toInt from '../utils/to-int'; +import getParsingFlags from '../create/parsing-flags'; + +// FORMATTING + +addFormatToken('H', ['HH', 2], 0, 'hour'); +addFormatToken('h', ['hh', 2], 0, function () { + return this.hours() % 12 || 12; +}); + +function meridiem (token, lowercase) { + addFormatToken(token, 0, 0, function () { + return this.localeData().meridiem(this.hours(), this.minutes(), lowercase); + }); +} + +meridiem('a', true); +meridiem('A', false); + +// ALIASES + +addUnitAlias('hour', 'h'); + +// PARSING + +function matchMeridiem (isStrict, locale) { + return locale._meridiemParse; +} + +addRegexToken('a', matchMeridiem); +addRegexToken('A', matchMeridiem); +addRegexToken('H', match1to2); +addRegexToken('h', match1to2); +addRegexToken('HH', match1to2, match2); +addRegexToken('hh', match1to2, match2); + +addParseToken(['H', 'HH'], HOUR); +addParseToken(['a', 'A'], function (input, array, config) { + config._isPm = config._locale.isPM(input); + config._meridiem = input; +}); +addParseToken(['h', 'hh'], function (input, array, config) { + array[HOUR] = toInt(input); + getParsingFlags(config).bigHour = true; +}); + +// LOCALES + +export function localeIsPM (input) { + // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays + // Using charAt should be more compatible. + return ((input + '').toLowerCase().charAt(0) === 'p'); +} + +export var defaultLocaleMeridiemParse = /[ap]\.?m?\.?/i; +export function localeMeridiem (hours, minutes, isLower) { + if (hours > 11) { + return isLower ? 'pm' : 'PM'; + } else { + return isLower ? 'am' : 'AM'; + } +} + + +// MOMENTS + +// Setting the hour should keep the time, because the user explicitly +// specified which hour he wants. So trying to maintain the same hour (in +// a new timezone) makes sense. Adding/subtracting hours does not follow +// this rule. +export var getSetHour = makeGetSet('Hours', true); diff --git a/library/moment/src/lib/units/millisecond.js b/library/moment/src/lib/units/millisecond.js new file mode 100644 index 000000000..134d88ee9 --- /dev/null +++ b/library/moment/src/lib/units/millisecond.js @@ -0,0 +1,64 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1, match2, match3, match1to3, matchUnsigned } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { MILLISECOND } from './constants'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken('S', 0, 0, function () { + return ~~(this.millisecond() / 100); +}); + +addFormatToken(0, ['SS', 2], 0, function () { + return ~~(this.millisecond() / 10); +}); + +addFormatToken(0, ['SSS', 3], 0, 'millisecond'); +addFormatToken(0, ['SSSS', 4], 0, function () { + return this.millisecond() * 10; +}); +addFormatToken(0, ['SSSSS', 5], 0, function () { + return this.millisecond() * 100; +}); +addFormatToken(0, ['SSSSSS', 6], 0, function () { + return this.millisecond() * 1000; +}); +addFormatToken(0, ['SSSSSSS', 7], 0, function () { + return this.millisecond() * 10000; +}); +addFormatToken(0, ['SSSSSSSS', 8], 0, function () { + return this.millisecond() * 100000; +}); +addFormatToken(0, ['SSSSSSSSS', 9], 0, function () { + return this.millisecond() * 1000000; +}); + + +// ALIASES + +addUnitAlias('millisecond', 'ms'); + +// PARSING + +addRegexToken('S', match1to3, match1); +addRegexToken('SS', match1to3, match2); +addRegexToken('SSS', match1to3, match3); + +var token; +for (token = 'SSSS'; token.length <= 9; token += 'S') { + addRegexToken(token, matchUnsigned); +} + +function parseMs(input, array) { + array[MILLISECOND] = toInt(('0.' + input) * 1000); +} + +for (token = 'S'; token.length <= 9; token += 'S') { + addParseToken(token, parseMs); +} +// MOMENTS + +export var getSetMillisecond = makeGetSet('Milliseconds', false); diff --git a/library/moment/src/lib/units/minute.js b/library/moment/src/lib/units/minute.js new file mode 100644 index 000000000..e9473b863 --- /dev/null +++ b/library/moment/src/lib/units/minute.js @@ -0,0 +1,24 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2 } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { MINUTE } from './constants'; + +// FORMATTING + +addFormatToken('m', ['mm', 2], 0, 'minute'); + +// ALIASES + +addUnitAlias('minute', 'm'); + +// PARSING + +addRegexToken('m', match1to2); +addRegexToken('mm', match1to2, match2); +addParseToken(['m', 'mm'], MINUTE); + +// MOMENTS + +export var getSetMinute = makeGetSet('Minutes', false); diff --git a/library/moment/src/lib/units/month.js b/library/moment/src/lib/units/month.js new file mode 100644 index 000000000..63d18b47f --- /dev/null +++ b/library/moment/src/lib/units/month.js @@ -0,0 +1,129 @@ +import { get } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2, matchWord } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { hooks } from '../utils/hooks'; +import { MONTH } from './constants'; +import toInt from '../utils/to-int'; +import { createUTC } from '../create/utc'; +import getParsingFlags from '../create/parsing-flags'; + +export function daysInMonth(year, month) { + return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); +} + +// FORMATTING + +addFormatToken('M', ['MM', 2], 'Mo', function () { + return this.month() + 1; +}); + +addFormatToken('MMM', 0, 0, function (format) { + return this.localeData().monthsShort(this, format); +}); + +addFormatToken('MMMM', 0, 0, function (format) { + return this.localeData().months(this, format); +}); + +// ALIASES + +addUnitAlias('month', 'M'); + +// PARSING + +addRegexToken('M', match1to2); +addRegexToken('MM', match1to2, match2); +addRegexToken('MMM', matchWord); +addRegexToken('MMMM', matchWord); + +addParseToken(['M', 'MM'], function (input, array) { + array[MONTH] = toInt(input) - 1; +}); + +addParseToken(['MMM', 'MMMM'], function (input, array, config, token) { + var month = config._locale.monthsParse(input, token, config._strict); + // if we didn't find a month name, mark the date as invalid. + if (month != null) { + array[MONTH] = month; + } else { + getParsingFlags(config).invalidMonth = input; + } +}); + +// LOCALES + +export var defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'); +export function localeMonths (m) { + return this._months[m.month()]; +} + +export var defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'); +export function localeMonthsShort (m) { + return this._monthsShort[m.month()]; +} + +export function localeMonthsParse (monthName, format, strict) { + var i, mom, regex; + + if (!this._monthsParse) { + this._monthsParse = []; + this._longMonthsParse = []; + this._shortMonthsParse = []; + } + + for (i = 0; i < 12; i++) { + // make the regex if we don't have it already + mom = createUTC([2000, i]); + if (strict && !this._longMonthsParse[i]) { + this._longMonthsParse[i] = new RegExp('^' + this.months(mom, '').replace('.', '') + '$', 'i'); + this._shortMonthsParse[i] = new RegExp('^' + this.monthsShort(mom, '').replace('.', '') + '$', 'i'); + } + if (!strict && !this._monthsParse[i]) { + regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, ''); + this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i'); + } + // test the regex + if (strict && format === 'MMMM' && this._longMonthsParse[i].test(monthName)) { + return i; + } else if (strict && format === 'MMM' && this._shortMonthsParse[i].test(monthName)) { + return i; + } else if (!strict && this._monthsParse[i].test(monthName)) { + return i; + } + } +} + +// MOMENTS + +export function setMonth (mom, value) { + var dayOfMonth; + + // TODO: Move this out of here! + if (typeof value === 'string') { + value = mom.localeData().monthsParse(value); + // TODO: Another silent failure? + if (typeof value !== 'number') { + return mom; + } + } + + dayOfMonth = Math.min(mom.date(), daysInMonth(mom.year(), value)); + mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth); + return mom; +} + +export function getSetMonth (value) { + if (value != null) { + setMonth(this, value); + hooks.updateOffset(this, true); + return this; + } else { + return get(this, 'Month'); + } +} + +export function getDaysInMonth () { + return daysInMonth(this.year(), this.month()); +} diff --git a/library/moment/src/lib/units/offset.js b/library/moment/src/lib/units/offset.js new file mode 100644 index 000000000..33288c0bf --- /dev/null +++ b/library/moment/src/lib/units/offset.js @@ -0,0 +1,213 @@ +import zeroFill from '../utils/zero-fill'; +import { createDuration } from '../duration/create'; +import { addSubtract } from '../moment/add-subtract'; +import { isMoment, copyConfig } from '../moment/constructor'; +import { addFormatToken } from '../format/format'; +import { addRegexToken, matchOffset } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { createLocal } from '../create/local'; +import { prepareConfig } from '../create/from-anything'; +import { createUTC } from '../create/utc'; +import isDate from '../utils/is-date'; +import toInt from '../utils/to-int'; +import compareArrays from '../utils/compare-arrays'; +import { hooks } from '../utils/hooks'; + +// FORMATTING + +function offset (token, separator) { + addFormatToken(token, 0, 0, function () { + var offset = this.utcOffset(); + var sign = '+'; + if (offset < 0) { + offset = -offset; + sign = '-'; + } + return sign + zeroFill(~~(offset / 60), 2) + separator + zeroFill(~~(offset) % 60, 2); + }); +} + +offset('Z', ':'); +offset('ZZ', ''); + +// PARSING + +addRegexToken('Z', matchOffset); +addRegexToken('ZZ', matchOffset); +addParseToken(['Z', 'ZZ'], function (input, array, config) { + config._useUTC = true; + config._tzm = offsetFromString(input); +}); + +// HELPERS + +// timezone chunker +// '+10:00' > ['10', '00'] +// '-1530' > ['-15', '30'] +var chunkOffset = /([\+\-]|\d\d)/gi; + +function offsetFromString(string) { + var matches = ((string || '').match(matchOffset) || []); + var chunk = matches[matches.length - 1] || []; + var parts = (chunk + '').match(chunkOffset) || ['-', 0, 0]; + var minutes = +(parts[1] * 60) + toInt(parts[2]); + + return parts[0] === '+' ? minutes : -minutes; +} + +// Return a moment from input, that is local/utc/zone equivalent to model. +export function cloneWithOffset(input, model) { + var res, diff; + if (model._isUTC) { + res = model.clone(); + diff = (isMoment(input) || isDate(input) ? +input : +createLocal(input)) - (+res); + // Use low-level api, because this fn is low-level api. + res._d.setTime(+res._d + diff); + hooks.updateOffset(res, false); + return res; + } else { + return createLocal(input).local(); + } +} + +function getDateOffset (m) { + // On Firefox.24 Date#getTimezoneOffset returns a floating point. + // https://github.com/moment/moment/pull/1871 + return -Math.round(m._d.getTimezoneOffset() / 15) * 15; +} + +// HOOKS + +// This function will be called whenever a moment is mutated. +// It is intended to keep the offset in sync with the timezone. +hooks.updateOffset = function () {}; + +// MOMENTS + +// keepLocalTime = true means only change the timezone, without +// affecting the local hour. So 5:31:26 +0300 --[utcOffset(2, true)]--> +// 5:31:26 +0200 It is possible that 5:31:26 doesn't exist with offset +// +0200, so we adjust the time as needed, to be valid. +// +// Keeping the time actually adds/subtracts (one hour) +// from the actual represented time. That is why we call updateOffset +// a second time. In case it wants us to change the offset again +// _changeInProgress == true case, then we have to adjust, because +// there is no such time in the given timezone. +export function getSetOffset (input, keepLocalTime) { + var offset = this._offset || 0, + localAdjust; + if (input != null) { + if (typeof input === 'string') { + input = offsetFromString(input); + } + if (Math.abs(input) < 16) { + input = input * 60; + } + if (!this._isUTC && keepLocalTime) { + localAdjust = getDateOffset(this); + } + this._offset = input; + this._isUTC = true; + if (localAdjust != null) { + this.add(localAdjust, 'm'); + } + if (offset !== input) { + if (!keepLocalTime || this._changeInProgress) { + addSubtract(this, createDuration(input - offset, 'm'), 1, false); + } else if (!this._changeInProgress) { + this._changeInProgress = true; + hooks.updateOffset(this, true); + this._changeInProgress = null; + } + } + return this; + } else { + return this._isUTC ? offset : getDateOffset(this); + } +} + +export function getSetZone (input, keepLocalTime) { + if (input != null) { + if (typeof input !== 'string') { + input = -input; + } + + this.utcOffset(input, keepLocalTime); + + return this; + } else { + return -this.utcOffset(); + } +} + +export function setOffsetToUTC (keepLocalTime) { + return this.utcOffset(0, keepLocalTime); +} + +export function setOffsetToLocal (keepLocalTime) { + if (this._isUTC) { + this.utcOffset(0, keepLocalTime); + this._isUTC = false; + + if (keepLocalTime) { + this.subtract(getDateOffset(this), 'm'); + } + } + return this; +} + +export function setOffsetToParsedOffset () { + if (this._tzm) { + this.utcOffset(this._tzm); + } else if (typeof this._i === 'string') { + this.utcOffset(offsetFromString(this._i)); + } + return this; +} + +export function hasAlignedHourOffset (input) { + input = input ? createLocal(input).utcOffset() : 0; + + return (this.utcOffset() - input) % 60 === 0; +} + +export function isDaylightSavingTime () { + return ( + this.utcOffset() > this.clone().month(0).utcOffset() || + this.utcOffset() > this.clone().month(5).utcOffset() + ); +} + +export function isDaylightSavingTimeShifted () { + if (typeof this._isDSTShifted !== 'undefined') { + return this._isDSTShifted; + } + + var c = {}; + + copyConfig(c, this); + c = prepareConfig(c); + + if (c._a) { + var other = c._isUTC ? createUTC(c._a) : createLocal(c._a); + this._isDSTShifted = this.isValid() && + compareArrays(c._a, other.toArray()) > 0; + } else { + this._isDSTShifted = false; + } + + return this._isDSTShifted; +} + +export function isLocal () { + return !this._isUTC; +} + +export function isUtcOffset () { + return this._isUTC; +} + +export function isUtc () { + return this._isUTC && this._offset === 0; +} diff --git a/library/moment/src/lib/units/quarter.js b/library/moment/src/lib/units/quarter.js new file mode 100644 index 000000000..d383f561e --- /dev/null +++ b/library/moment/src/lib/units/quarter.js @@ -0,0 +1,27 @@ +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1 } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { MONTH } from './constants'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken('Q', 0, 0, 'quarter'); + +// ALIASES + +addUnitAlias('quarter', 'Q'); + +// PARSING + +addRegexToken('Q', match1); +addParseToken('Q', function (input, array) { + array[MONTH] = (toInt(input) - 1) * 3; +}); + +// MOMENTS + +export function getSetQuarter (input) { + return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3); +} diff --git a/library/moment/src/lib/units/second.js b/library/moment/src/lib/units/second.js new file mode 100644 index 000000000..118fe3083 --- /dev/null +++ b/library/moment/src/lib/units/second.js @@ -0,0 +1,24 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2 } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { SECOND } from './constants'; + +// FORMATTING + +addFormatToken('s', ['ss', 2], 0, 'second'); + +// ALIASES + +addUnitAlias('second', 's'); + +// PARSING + +addRegexToken('s', match1to2); +addRegexToken('ss', match1to2, match2); +addParseToken(['s', 'ss'], SECOND); + +// MOMENTS + +export var getSetSecond = makeGetSet('Seconds', false); diff --git a/library/moment/src/lib/units/timestamp.js b/library/moment/src/lib/units/timestamp.js new file mode 100644 index 000000000..a49e1e4b8 --- /dev/null +++ b/library/moment/src/lib/units/timestamp.js @@ -0,0 +1,20 @@ +import { addFormatToken } from '../format/format'; +import { addRegexToken, matchTimestamp, matchSigned } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken('X', 0, 0, 'unix'); +addFormatToken('x', 0, 0, 'valueOf'); + +// PARSING + +addRegexToken('x', matchSigned); +addRegexToken('X', matchTimestamp); +addParseToken('X', function (input, array, config) { + config._d = new Date(parseFloat(input, 10) * 1000); +}); +addParseToken('x', function (input, array, config) { + config._d = new Date(toInt(input)); +}); diff --git a/library/moment/src/lib/units/timezone.js b/library/moment/src/lib/units/timezone.js new file mode 100644 index 000000000..20c81cd2c --- /dev/null +++ b/library/moment/src/lib/units/timezone.js @@ -0,0 +1,16 @@ +import { addFormatToken } from '../format/format'; + +// FORMATTING + +addFormatToken('z', 0, 0, 'zoneAbbr'); +addFormatToken('zz', 0, 0, 'zoneName'); + +// MOMENTS + +export function getZoneAbbr () { + return this._isUTC ? 'UTC' : ''; +} + +export function getZoneName () { + return this._isUTC ? 'Coordinated Universal Time' : ''; +} diff --git a/library/moment/src/lib/units/units.js b/library/moment/src/lib/units/units.js new file mode 100644 index 000000000..6f45f1c04 --- /dev/null +++ b/library/moment/src/lib/units/units.js @@ -0,0 +1,20 @@ +// Side effect imports +import './day-of-month'; +import './day-of-week'; +import './day-of-year'; +import './hour'; +import './millisecond'; +import './minute'; +import './month'; +import './offset'; +import './quarter'; +import './second'; +import './timestamp'; +import './timezone'; +import './week-year'; +import './week'; +import './year'; + +import { normalizeUnits } from './aliases'; + +export { normalizeUnits }; diff --git a/library/moment/src/lib/units/week-year.js b/library/moment/src/lib/units/week-year.js new file mode 100644 index 000000000..8ecfce754 --- /dev/null +++ b/library/moment/src/lib/units/week-year.js @@ -0,0 +1,78 @@ +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match1to4, match1to6, match2, match4, match6, matchSigned } from '../parse/regex'; +import { addWeekParseToken } from '../parse/token'; +import { weekOfYear } from './week'; +import toInt from '../utils/to-int'; +import { hooks } from '../utils/hooks'; +import { createLocal } from '../create/local'; + +// FORMATTING + +addFormatToken(0, ['gg', 2], 0, function () { + return this.weekYear() % 100; +}); + +addFormatToken(0, ['GG', 2], 0, function () { + return this.isoWeekYear() % 100; +}); + +function addWeekYearFormatToken (token, getter) { + addFormatToken(0, [token, token.length], 0, getter); +} + +addWeekYearFormatToken('gggg', 'weekYear'); +addWeekYearFormatToken('ggggg', 'weekYear'); +addWeekYearFormatToken('GGGG', 'isoWeekYear'); +addWeekYearFormatToken('GGGGG', 'isoWeekYear'); + +// ALIASES + +addUnitAlias('weekYear', 'gg'); +addUnitAlias('isoWeekYear', 'GG'); + +// PARSING + +addRegexToken('G', matchSigned); +addRegexToken('g', matchSigned); +addRegexToken('GG', match1to2, match2); +addRegexToken('gg', match1to2, match2); +addRegexToken('GGGG', match1to4, match4); +addRegexToken('gggg', match1to4, match4); +addRegexToken('GGGGG', match1to6, match6); +addRegexToken('ggggg', match1to6, match6); + +addWeekParseToken(['gggg', 'ggggg', 'GGGG', 'GGGGG'], function (input, week, config, token) { + week[token.substr(0, 2)] = toInt(input); +}); + +addWeekParseToken(['gg', 'GG'], function (input, week, config, token) { + week[token] = hooks.parseTwoDigitYear(input); +}); + +// HELPERS + +function weeksInYear(year, dow, doy) { + return weekOfYear(createLocal([year, 11, 31 + dow - doy]), dow, doy).week; +} + +// MOMENTS + +export function getSetWeekYear (input) { + var year = weekOfYear(this, this.localeData()._week.dow, this.localeData()._week.doy).year; + return input == null ? year : this.add((input - year), 'y'); +} + +export function getSetISOWeekYear (input) { + var year = weekOfYear(this, 1, 4).year; + return input == null ? year : this.add((input - year), 'y'); +} + +export function getISOWeeksInYear () { + return weeksInYear(this.year(), 1, 4); +} + +export function getWeeksInYear () { + var weekInfo = this.localeData()._week; + return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy); +} diff --git a/library/moment/src/lib/units/week.js b/library/moment/src/lib/units/week.js new file mode 100644 index 000000000..178f6b551 --- /dev/null +++ b/library/moment/src/lib/units/week.js @@ -0,0 +1,88 @@ +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match2 } from '../parse/regex'; +import { addWeekParseToken } from '../parse/token'; +import toInt from '../utils/to-int'; +import { createLocal } from '../create/local'; + +// FORMATTING + +addFormatToken('w', ['ww', 2], 'wo', 'week'); +addFormatToken('W', ['WW', 2], 'Wo', 'isoWeek'); + +// ALIASES + +addUnitAlias('week', 'w'); +addUnitAlias('isoWeek', 'W'); + +// PARSING + +addRegexToken('w', match1to2); +addRegexToken('ww', match1to2, match2); +addRegexToken('W', match1to2); +addRegexToken('WW', match1to2, match2); + +addWeekParseToken(['w', 'ww', 'W', 'WW'], function (input, week, config, token) { + week[token.substr(0, 1)] = toInt(input); +}); + +// HELPERS + +// firstDayOfWeek 0 = sun, 6 = sat +// the day of the week that starts the week +// (usually sunday or monday) +// firstDayOfWeekOfYear 0 = sun, 6 = sat +// the first week is the week that contains the first +// of this day of the week +// (eg. ISO weeks use thursday (4)) +export function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) { + var end = firstDayOfWeekOfYear - firstDayOfWeek, + daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(), + adjustedMoment; + + + if (daysToDayOfWeek > end) { + daysToDayOfWeek -= 7; + } + + if (daysToDayOfWeek < end - 7) { + daysToDayOfWeek += 7; + } + + adjustedMoment = createLocal(mom).add(daysToDayOfWeek, 'd'); + return { + week: Math.ceil(adjustedMoment.dayOfYear() / 7), + year: adjustedMoment.year() + }; +} + +// LOCALES + +export function localeWeek (mom) { + return weekOfYear(mom, this._week.dow, this._week.doy).week; +} + +export var defaultLocaleWeek = { + dow : 0, // Sunday is the first day of the week. + doy : 6 // The week that contains Jan 1st is the first week of the year. +}; + +export function localeFirstDayOfWeek () { + return this._week.dow; +} + +export function localeFirstDayOfYear () { + return this._week.doy; +} + +// MOMENTS + +export function getSetWeek (input) { + var week = this.localeData().week(this); + return input == null ? week : this.add((input - week) * 7, 'd'); +} + +export function getSetISOWeek (input) { + var week = weekOfYear(this, 1, 4).week; + return input == null ? week : this.add((input - week) * 7, 'd'); +} diff --git a/library/moment/src/lib/units/year.js b/library/moment/src/lib/units/year.js new file mode 100644 index 000000000..cc32b55b6 --- /dev/null +++ b/library/moment/src/lib/units/year.js @@ -0,0 +1,62 @@ +import { makeGetSet } from '../moment/get-set'; +import { addFormatToken } from '../format/format'; +import { addUnitAlias } from './aliases'; +import { addRegexToken, match1to2, match1to4, match1to6, match2, match4, match6, matchSigned } from '../parse/regex'; +import { addParseToken } from '../parse/token'; +import { hooks } from '../utils/hooks'; +import { YEAR } from './constants'; +import toInt from '../utils/to-int'; + +// FORMATTING + +addFormatToken(0, ['YY', 2], 0, function () { + return this.year() % 100; +}); + +addFormatToken(0, ['YYYY', 4], 0, 'year'); +addFormatToken(0, ['YYYYY', 5], 0, 'year'); +addFormatToken(0, ['YYYYYY', 6, true], 0, 'year'); + +// ALIASES + +addUnitAlias('year', 'y'); + +// PARSING + +addRegexToken('Y', matchSigned); +addRegexToken('YY', match1to2, match2); +addRegexToken('YYYY', match1to4, match4); +addRegexToken('YYYYY', match1to6, match6); +addRegexToken('YYYYYY', match1to6, match6); + +addParseToken(['YYYYY', 'YYYYYY'], YEAR); +addParseToken('YYYY', function (input, array) { + array[YEAR] = input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input); +}); +addParseToken('YY', function (input, array) { + array[YEAR] = hooks.parseTwoDigitYear(input); +}); + +// HELPERS + +export function daysInYear(year) { + return isLeapYear(year) ? 366 : 365; +} + +function isLeapYear(year) { + return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; +} + +// HOOKS + +hooks.parseTwoDigitYear = function (input) { + return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); +}; + +// MOMENTS + +export var getSetYear = makeGetSet('FullYear', false); + +export function getIsLeapYear () { + return isLeapYear(this.year()); +} |