gregorian-calendar
Advanced tools
Comparing version 1.0.5 to 2.0.0
@@ -37,81 +37,2 @@ var gulp = require('gulp'); | ||
cp.exec('git tag ' + version + ' | git push origin ' + version + ':' + version + ' | git push origin master:master', done); | ||
}); | ||
var wrapper = require('gulp-wrapper'); | ||
var date = new Date(); | ||
var header = ['/*', | ||
'Copyright ' + date.getFullYear() + ', ' + packageInfo.name + '@' + packageInfo.version, | ||
packageInfo.license + ' Licensed', | ||
'build time: ' + (date.toGMTString()), | ||
'*/', ''].join('\n'); | ||
gulp.task('build-locale', ['lint'], function () { | ||
var tag = 'gregorian-calendar'; | ||
gulp.src('./lib/' + tag + '/i18n/*.js') | ||
.pipe(replace(/@VERSION@/g, packageInfo.version)) | ||
.pipe(through2.obj(function (s, encoding, callback) { | ||
var name = tag + '/i18n/' + path.basename(s.path, '.js'); | ||
var contents = 'modulex.add("' + name + '",[], function(require, exports, module) {' + s.contents.toString('utf-8') + '});'; | ||
s.contents = new Buffer(contents, 'utf-8'); | ||
this.push(s); | ||
callback(); | ||
})) | ||
.pipe(rename(function (path) { | ||
path.basename += '-debug'; | ||
})) | ||
.pipe(wrapper({ | ||
header: header | ||
})) | ||
.pipe(gulp.dest(path.resolve(build, tag + '/i18n/'))) | ||
.pipe(replace(/@DEBUG@/g, '')) | ||
.pipe(uglify()) | ||
.pipe(rename(function (path) { | ||
path.basename = path.basename.replace(/-debug/, ''); | ||
})) | ||
.pipe(gulp.dest(path.resolve(build, tag + '/i18n/'))); | ||
}); | ||
gulp.task('build', ['lint', 'build-locale'], function () { | ||
var tag = 'gregorian-calendar'; | ||
var packages = {}; | ||
packages[tag] = { | ||
base: path.resolve(src, tag) | ||
}; | ||
return gulp.src('./lib/' + tag + '.js') | ||
.pipe(modulex({ | ||
modulex: { | ||
packages: packages | ||
}, | ||
excludeModules: [tag + '/i18n/zh-cn'] | ||
})) | ||
.pipe(kclean({ | ||
files: [ | ||
{ | ||
src: './lib/' + tag + '-debug.js', | ||
outputModule: tag | ||
} | ||
] | ||
})) | ||
.pipe(replace(/@VERSION@/g, packageInfo.version)) | ||
.pipe(wrapper({ | ||
header: header | ||
})) | ||
.pipe(gulp.dest(path.resolve(build))) | ||
.pipe(filter(tag + '-debug.js')) | ||
.pipe(replace(/@DEBUG@/g, '')) | ||
.pipe(uglify()) | ||
.pipe(rename(tag + '.js')) | ||
.pipe(gulp.dest(path.resolve(build))); | ||
}); | ||
gulp.task('mx', function () { | ||
var aggregateBower = require('aggregate-bower'); | ||
aggregateBower('bower_components/', 'mx_modules/'); | ||
}); | ||
gulp.task('auto-d', function () { | ||
require('auto-deps')(cwd); | ||
}); | ||
gulp.task('default', ['build']); | ||
}); |
@@ -1,4 +0,1 @@ | ||
module.exports = require('./lib/gregorian-calendar'); | ||
module.exports.locales['en-us'] = require('./lib/gregorian-calendar/i18n/en-us'); | ||
module.exports.locales['zh-cn'] = require('./lib/gregorian-calendar/i18n/zh-cn'); | ||
module.exports.version = require('./package.json').version; | ||
module.exports = require('./lib/gregorian-calendar'); |
@@ -7,11 +7,6 @@ /** | ||
var toInt = parseInt; | ||
var Utils = require('./gregorian-calendar/utils'); | ||
var requireFn = require; | ||
var Utils = require('./utils'); | ||
var defaultLocale; | ||
if (typeof process !== 'undefined') { | ||
defaultLocale = requireFn('./gregorian-calendar/i18n/en-us'); | ||
} else { | ||
defaultLocale = require('i18n!gregorian-calendar'); | ||
} | ||
var Const = require('./gregorian-calendar/const'); | ||
defaultLocale = require('./locale/en-us'); | ||
var Const = require('./const'); | ||
@@ -50,53 +45,53 @@ /** | ||
var args = [].slice.call(arguments, 0); | ||
var args = [].slice.call(arguments, 0); | ||
if (typeof timezoneOffset === 'object') { | ||
locale = timezoneOffset; | ||
timezoneOffset = locale.timezoneOffset; | ||
} else if (args.length >= 3) { | ||
timezoneOffset = locale = null; | ||
} | ||
if (typeof timezoneOffset === 'object') { | ||
locale = timezoneOffset; | ||
timezoneOffset = locale.timezoneOffset; | ||
} else if (args.length >= 3) { | ||
timezoneOffset = locale = null; | ||
} | ||
locale = locale || defaultLocale; | ||
locale = locale || defaultLocale; | ||
this.locale = locale; | ||
this.locale = locale; | ||
this.fields = []; | ||
this.fields = []; | ||
/** | ||
* The currently set time for this date. | ||
* @protected | ||
* @type Number|undefined | ||
*/ | ||
this.time = undefined; | ||
/** | ||
* The timezoneOffset in minutes used by this date. | ||
* @type Number | ||
* @protected | ||
*/ | ||
if (typeof timezoneOffset !== 'number') { | ||
timezoneOffset = locale.timezoneOffset; | ||
} | ||
this.timezoneOffset = timezoneOffset; | ||
/** | ||
* The currently set time for this date. | ||
* @protected | ||
* @type Number|undefined | ||
*/ | ||
this.time = undefined; | ||
/** | ||
* The timezoneOffset in minutes used by this date. | ||
* @type Number | ||
* @protected | ||
*/ | ||
if (typeof timezoneOffset !== 'number') { | ||
timezoneOffset = locale.timezoneOffset; | ||
} | ||
this.timezoneOffset = timezoneOffset; | ||
/** | ||
* The first day of the week | ||
* @type Number | ||
* @protected | ||
*/ | ||
this.firstDayOfWeek = locale.firstDayOfWeek; | ||
/** | ||
* The first day of the week | ||
* @type Number | ||
* @protected | ||
*/ | ||
this.firstDayOfWeek = locale.firstDayOfWeek; | ||
/** | ||
* The number of days required for the first week in a month or year, | ||
* with possible values from 1 to 7. | ||
* @@protected | ||
* @type Number | ||
*/ | ||
this.minimalDaysInFirstWeek = locale.minimalDaysInFirstWeek; | ||
/** | ||
* The number of days required for the first week in a month or year, | ||
* with possible values from 1 to 7. | ||
* @@protected | ||
* @type Number | ||
*/ | ||
this.minimalDaysInFirstWeek = locale.minimalDaysInFirstWeek; | ||
this.fieldsComputed = false; | ||
this.fieldsComputed = false; | ||
if (arguments.length >= 3) { | ||
this.set.apply(this, args); | ||
} | ||
if (arguments.length >= 3) { | ||
this.set.apply(this, args); | ||
} | ||
} | ||
@@ -107,95 +102,95 @@ | ||
Utils.mix(GregorianCalendar, { | ||
Utils: Utils, | ||
Utils: Utils, | ||
/** | ||
* Determines if the given year is a leap year. | ||
* Returns true if the given year is a leap year. To specify BC year numbers, | ||
* 1 - year number must be given. For example, year BC 4 is specified as -3. | ||
* @param {Number} year the given year. | ||
* @returns {Boolean} true if the given year is a leap year; false otherwise. | ||
* @static | ||
* @method | ||
*/ | ||
isLeapYear: Utils.isLeapYear, | ||
/** | ||
* Determines if the given year is a leap year. | ||
* Returns true if the given year is a leap year. To specify BC year numbers, | ||
* 1 - year number must be given. For example, year BC 4 is specified as -3. | ||
* @param {Number} year the given year. | ||
* @returns {Boolean} true if the given year is a leap year; false otherwise. | ||
* @static | ||
* @method | ||
*/ | ||
isLeapYear: Utils.isLeapYear, | ||
/** | ||
* Enum indicating year field of date | ||
* @type Number | ||
*/ | ||
YEAR: 1, | ||
/** | ||
* Enum indicating month field of date | ||
* @type Number | ||
*/ | ||
MONTH: 2, | ||
/** | ||
* Enum indicating the day of the month | ||
* @type Number | ||
*/ | ||
DAY_OF_MONTH: 3, | ||
/** | ||
* Enum indicating the hour (24). | ||
* @type Number | ||
*/ | ||
HOUR_OF_DAY: 4, | ||
/** | ||
* Enum indicating the minute of the day | ||
* @type Number | ||
*/ | ||
MINUTES: 5, | ||
/** | ||
* Enum indicating the second of the day | ||
* @type Number | ||
*/ | ||
SECONDS: 6, | ||
/** | ||
* Enum indicating the millisecond of the day | ||
* @type Number | ||
*/ | ||
MILLISECONDS: 7, | ||
/** | ||
* Enum indicating the week number within the current year | ||
* @type Number | ||
*/ | ||
WEEK_OF_YEAR: 8, | ||
/** | ||
* Enum indicating the week number within the current month | ||
* @type Number | ||
*/ | ||
WEEK_OF_MONTH: 9, | ||
/** | ||
* Enum indicating year field of date | ||
* @type Number | ||
*/ | ||
YEAR: 1, | ||
/** | ||
* Enum indicating month field of date | ||
* @type Number | ||
*/ | ||
MONTH: 2, | ||
/** | ||
* Enum indicating the day of the month | ||
* @type Number | ||
*/ | ||
DAY_OF_MONTH: 3, | ||
/** | ||
* Enum indicating the hour (24). | ||
* @type Number | ||
*/ | ||
HOUR_OF_DAY: 4, | ||
/** | ||
* Enum indicating the minute of the day | ||
* @type Number | ||
*/ | ||
MINUTES: 5, | ||
/** | ||
* Enum indicating the second of the day | ||
* @type Number | ||
*/ | ||
SECONDS: 6, | ||
/** | ||
* Enum indicating the millisecond of the day | ||
* @type Number | ||
*/ | ||
MILLISECONDS: 7, | ||
/** | ||
* Enum indicating the week number within the current year | ||
* @type Number | ||
*/ | ||
WEEK_OF_YEAR: 8, | ||
/** | ||
* Enum indicating the week number within the current month | ||
* @type Number | ||
*/ | ||
WEEK_OF_MONTH: 9, | ||
/** | ||
* Enum indicating the day of the day number within the current year | ||
* @type Number | ||
*/ | ||
DAY_OF_YEAR: 10, | ||
/** | ||
* Enum indicating the day of the week | ||
* @type Number | ||
*/ | ||
DAY_OF_WEEK: 11, | ||
/** | ||
* Enum indicating the day of the ordinal number of the day of the week | ||
* @type Number | ||
*/ | ||
DAY_OF_WEEK_IN_MONTH: 12, | ||
/** | ||
* Enum indicating the day of the day number within the current year | ||
* @type Number | ||
*/ | ||
DAY_OF_YEAR: 10, | ||
/** | ||
* Enum indicating the day of the week | ||
* @type Number | ||
*/ | ||
DAY_OF_WEEK: 11, | ||
/** | ||
* Enum indicating the day of the ordinal number of the day of the week | ||
* @type Number | ||
*/ | ||
DAY_OF_WEEK_IN_MONTH: 12, | ||
/** | ||
* Enum indicating am | ||
* @type Number | ||
*/ | ||
AM: 0, | ||
/** | ||
* Enum indicating pm | ||
* @type Number | ||
*/ | ||
PM: 1 | ||
/** | ||
* Enum indicating am | ||
* @type Number | ||
*/ | ||
AM: 0, | ||
/** | ||
* Enum indicating pm | ||
* @type Number | ||
*/ | ||
PM: 1 | ||
}); | ||
var fields = ['', | ||
'Year', 'Month', 'DayOfMonth', | ||
'HourOfDay', | ||
'Minutes', 'Seconds', 'Milliseconds', 'WeekOfYear', | ||
'WeekOfMonth', 'DayOfYear', 'DayOfWeek', | ||
'DayOfWeekInMonth' | ||
'Year', 'Month', 'DayOfMonth', | ||
'HourOfDay', | ||
'Minutes', 'Seconds', 'Milliseconds', 'WeekOfYear', | ||
'WeekOfMonth', 'DayOfYear', 'DayOfWeek', | ||
'DayOfWeekInMonth' | ||
]; | ||
@@ -230,577 +225,577 @@ | ||
var mod = Utils.mod, | ||
isLeapYear = Utils.isLeapYear, | ||
floorDivide = Math.floor; | ||
isLeapYear = Utils.isLeapYear, | ||
floorDivide = Math.floor; | ||
var MIN_VALUES = [ | ||
undefined, | ||
1, // YEAR | ||
GregorianCalendar.JANUARY, // MONTH | ||
1, // DAY_OF_MONTH | ||
0, // HOUR_OF_DAY | ||
0, // MINUTE | ||
0, // SECONDS | ||
0, // MILLISECONDS | ||
undefined, | ||
1, // YEAR | ||
GregorianCalendar.JANUARY, // MONTH | ||
1, // DAY_OF_MONTH | ||
0, // HOUR_OF_DAY | ||
0, // MINUTE | ||
0, // SECONDS | ||
0, // MILLISECONDS | ||
1, // WEEK_OF_YEAR | ||
undefined, // WEEK_OF_MONTH | ||
1, // WEEK_OF_YEAR | ||
undefined, // WEEK_OF_MONTH | ||
1, // DAY_OF_YEAR | ||
GregorianCalendar.SUNDAY, // DAY_OF_WEEK | ||
1 // DAY_OF_WEEK_IN_MONTH | ||
1, // DAY_OF_YEAR | ||
GregorianCalendar.SUNDAY, // DAY_OF_WEEK | ||
1 // DAY_OF_WEEK_IN_MONTH | ||
]; | ||
var MAX_VALUES = [ | ||
undefined, | ||
292278994, // YEAR | ||
GregorianCalendar.DECEMBER, // MONTH | ||
undefined, // DAY_OF_MONTH | ||
23, // HOUR_OF_DAY | ||
59, // MINUTE | ||
59, // SECONDS | ||
999, // MILLISECONDS | ||
undefined, // WEEK_OF_YEAR | ||
undefined, // WEEK_OF_MONTH | ||
undefined, // DAY_OF_YEAR | ||
GregorianCalendar.SATURDAY, // DAY_OF_WEEK | ||
undefined // DAY_OF_WEEK_IN_MONTH | ||
undefined, | ||
292278994, // YEAR | ||
GregorianCalendar.DECEMBER, // MONTH | ||
undefined, // DAY_OF_MONTH | ||
23, // HOUR_OF_DAY | ||
59, // MINUTE | ||
59, // SECONDS | ||
999, // MILLISECONDS | ||
undefined, // WEEK_OF_YEAR | ||
undefined, // WEEK_OF_MONTH | ||
undefined, // DAY_OF_YEAR | ||
GregorianCalendar.SATURDAY, // DAY_OF_WEEK | ||
undefined // DAY_OF_WEEK_IN_MONTH | ||
]; | ||
GregorianCalendar.prototype = { | ||
constructor: GregorianCalendar, | ||
constructor: GregorianCalendar, | ||
/** | ||
* Determines if current year is a leap year. | ||
* Returns true if the given year is a leap year. To specify BC year numbers, | ||
* 1 - year number must be given. For example, year BC 4 is specified as -3. | ||
* @returns {Boolean} true if the given year is a leap year; false otherwise. | ||
* @method | ||
* @member Date.Gregorian | ||
*/ | ||
isLeapYear: function () { | ||
return isLeapYear(this.getYear()); | ||
}, | ||
/** | ||
* Determines if current year is a leap year. | ||
* Returns true if the given year is a leap year. To specify BC year numbers, | ||
* 1 - year number must be given. For example, year BC 4 is specified as -3. | ||
* @returns {Boolean} true if the given year is a leap year; false otherwise. | ||
* @method | ||
* @member Date.Gregorian | ||
*/ | ||
isLeapYear: function () { | ||
return isLeapYear(this.getYear()); | ||
}, | ||
/** | ||
* Return local info for current date instance | ||
* @returns {Object} | ||
*/ | ||
getLocale: function () { | ||
return this.locale; | ||
}, | ||
/** | ||
* Return local info for current date instance | ||
* @returns {Object} | ||
*/ | ||
getLocale: function () { | ||
return this.locale; | ||
}, | ||
/** | ||
* Returns the minimum value for | ||
* the given calendar field of this GregorianCalendar instance. | ||
* The minimum value is defined as the smallest value | ||
* returned by the get method for any possible time value, | ||
* taking into consideration the current values of the getFirstDayOfWeek, | ||
* getMinimalDaysInFirstWeek. | ||
* @param field the calendar field. | ||
* @returns {Number} the minimum value for the given calendar field. | ||
*/ | ||
getActualMinimum: function (field) { | ||
if (MIN_VALUES[field] !== undefined) { | ||
return MIN_VALUES[field]; | ||
} | ||
/** | ||
* Returns the minimum value for | ||
* the given calendar field of this GregorianCalendar instance. | ||
* The minimum value is defined as the smallest value | ||
* returned by the get method for any possible time value, | ||
* taking into consideration the current values of the getFirstDayOfWeek, | ||
* getMinimalDaysInFirstWeek. | ||
* @param field the calendar field. | ||
* @returns {Number} the minimum value for the given calendar field. | ||
*/ | ||
getActualMinimum: function (field) { | ||
if (MIN_VALUES[field] !== undefined) { | ||
return MIN_VALUES[field]; | ||
} | ||
var fields = this.fields; | ||
if (field === WEEK_OF_MONTH) { | ||
var cal = new GregorianCalendar(fields[YEAR], fields[MONTH], 1); | ||
return cal.get(WEEK_OF_MONTH); | ||
} | ||
var fields = this.fields; | ||
if (field === WEEK_OF_MONTH) { | ||
var cal = new GregorianCalendar(fields[YEAR], fields[MONTH], 1); | ||
return cal.get(WEEK_OF_MONTH); | ||
} | ||
throw new Error('minimum value not defined!'); | ||
}, | ||
throw new Error('minimum value not defined!'); | ||
}, | ||
/** | ||
* Returns the maximum value for the given calendar field | ||
* of this GregorianCalendar instance. | ||
* The maximum value is defined as the largest value returned | ||
* by the get method for any possible time value, taking into consideration | ||
* the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek methods. | ||
* @param field the calendar field. | ||
* @returns {Number} the maximum value for the given calendar field. | ||
*/ | ||
getActualMaximum: function (field) { | ||
if (MAX_VALUES[field] !== undefined) { | ||
return MAX_VALUES[field]; | ||
/** | ||
* Returns the maximum value for the given calendar field | ||
* of this GregorianCalendar instance. | ||
* The maximum value is defined as the largest value returned | ||
* by the get method for any possible time value, taking into consideration | ||
* the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek methods. | ||
* @param field the calendar field. | ||
* @returns {Number} the maximum value for the given calendar field. | ||
*/ | ||
getActualMaximum: function (field) { | ||
if (MAX_VALUES[field] !== undefined) { | ||
return MAX_VALUES[field]; | ||
} | ||
var value, | ||
fields = this.fields; | ||
switch (field) { | ||
case DAY_OF_MONTH: | ||
value = getMonthLength(fields[YEAR], fields[MONTH]); | ||
break; | ||
case WEEK_OF_YEAR: | ||
var endOfYear = new GregorianCalendar(fields[YEAR], GregorianCalendar.DECEMBER, 31); | ||
value = endOfYear.get(WEEK_OF_YEAR); | ||
if (value === 1) { | ||
value = 52; | ||
} | ||
var value, | ||
fields = this.fields; | ||
switch (field) { | ||
case DAY_OF_MONTH: | ||
value = getMonthLength(fields[YEAR], fields[MONTH]); | ||
break; | ||
break; | ||
case WEEK_OF_YEAR: | ||
var endOfYear = new GregorianCalendar(fields[YEAR], GregorianCalendar.DECEMBER, 31); | ||
value = endOfYear.get(WEEK_OF_YEAR); | ||
if (value === 1) { | ||
value = 52; | ||
} | ||
break; | ||
case WEEK_OF_MONTH: | ||
var endOfMonth = new GregorianCalendar(fields[YEAR], fields[MONTH], getMonthLength(fields[YEAR], fields[MONTH])); | ||
value = endOfMonth.get(WEEK_OF_MONTH); | ||
break; | ||
case WEEK_OF_MONTH: | ||
var endOfMonth = new GregorianCalendar(fields[YEAR], fields[MONTH], getMonthLength(fields[YEAR], fields[MONTH])); | ||
value = endOfMonth.get(WEEK_OF_MONTH); | ||
break; | ||
case DAY_OF_YEAR: | ||
value = getYearLength(fields[YEAR]); | ||
break; | ||
case DAY_OF_YEAR: | ||
value = getYearLength(fields[YEAR]); | ||
break; | ||
case DAY_OF_WEEK_IN_MONTH: | ||
value = toInt((getMonthLength(fields[YEAR], fields[MONTH]) - 1) / 7) + 1; | ||
break; | ||
} | ||
if (value === undefined) { | ||
throw new Error('maximum value not defined!'); | ||
} | ||
return value; | ||
}, | ||
case DAY_OF_WEEK_IN_MONTH: | ||
value = toInt((getMonthLength(fields[YEAR], fields[MONTH]) - 1) / 7) + 1; | ||
break; | ||
} | ||
if (value === undefined) { | ||
throw new Error('maximum value not defined!'); | ||
} | ||
return value; | ||
}, | ||
/** | ||
* Determines if the given calendar field has a value set, | ||
* including cases that the value has been set by internal fields calculations | ||
* triggered by a get method call. | ||
* @param field the calendar field to be cleared. | ||
* @returns {boolean} true if the given calendar field has a value set; false otherwise. | ||
*/ | ||
isSet: function (field) { | ||
return this.fields[field] !== undefined; | ||
}, | ||
/** | ||
* Determines if the given calendar field has a value set, | ||
* including cases that the value has been set by internal fields calculations | ||
* triggered by a get method call. | ||
* @param field the calendar field to be cleared. | ||
* @returns {boolean} true if the given calendar field has a value set; false otherwise. | ||
*/ | ||
isSet: function (field) { | ||
return this.fields[field] !== undefined; | ||
}, | ||
/** | ||
* Converts the time value (millisecond offset from the Epoch) | ||
* to calendar field values. | ||
* @protected | ||
*/ | ||
computeFields: function () { | ||
var time = this.time; | ||
var timezoneOffset = this.timezoneOffset * ONE_MINUTE; | ||
var fixedDate = toInt(timezoneOffset / ONE_DAY); | ||
var timeOfDay = timezoneOffset % ONE_DAY; | ||
fixedDate += toInt(time / ONE_DAY); | ||
timeOfDay += time % ONE_DAY; | ||
if (timeOfDay >= ONE_DAY) { | ||
timeOfDay -= ONE_DAY; | ||
fixedDate++; | ||
} else { | ||
while (timeOfDay < 0) { | ||
timeOfDay += ONE_DAY; | ||
fixedDate--; | ||
} | ||
} | ||
/** | ||
* Converts the time value (millisecond offset from the Epoch) | ||
* to calendar field values. | ||
* @protected | ||
*/ | ||
computeFields: function () { | ||
var time = this.time; | ||
var timezoneOffset = this.timezoneOffset * ONE_MINUTE; | ||
var fixedDate = toInt(timezoneOffset / ONE_DAY); | ||
var timeOfDay = timezoneOffset % ONE_DAY; | ||
fixedDate += toInt(time / ONE_DAY); | ||
timeOfDay += time % ONE_DAY; | ||
if (timeOfDay >= ONE_DAY) { | ||
timeOfDay -= ONE_DAY; | ||
fixedDate++; | ||
} else { | ||
while (timeOfDay < 0) { | ||
timeOfDay += ONE_DAY; | ||
fixedDate--; | ||
} | ||
} | ||
fixedDate += EPOCH_OFFSET; | ||
fixedDate += EPOCH_OFFSET; | ||
var date = Utils.getGregorianDateFromFixedDate(fixedDate); | ||
var date = Utils.getGregorianDateFromFixedDate(fixedDate); | ||
var year = date.year; | ||
var year = date.year; | ||
var fields = this.fields; | ||
fields[YEAR] = year; | ||
fields[MONTH] = date.month; | ||
fields[DAY_OF_MONTH] = date.dayOfMonth; | ||
fields[DAY_OF_WEEK] = date.dayOfWeek; | ||
var fields = this.fields; | ||
fields[YEAR] = year; | ||
fields[MONTH] = date.month; | ||
fields[DAY_OF_MONTH] = date.dayOfMonth; | ||
fields[DAY_OF_WEEK] = date.dayOfWeek; | ||
if (timeOfDay !== 0) { | ||
fields[HOUR_OF_DAY] = toInt(timeOfDay / ONE_HOUR); | ||
var r = timeOfDay % ONE_HOUR; | ||
fields[MINUTE] = toInt(r / ONE_MINUTE); | ||
r %= ONE_MINUTE; | ||
fields[SECONDS] = toInt(r / ONE_SECOND); | ||
fields[MILLISECONDS] = r % ONE_SECOND; | ||
} else { | ||
fields[HOUR_OF_DAY] = | ||
fields[MINUTE] = | ||
fields[SECONDS] = | ||
fields[MILLISECONDS] = 0; | ||
} | ||
if (timeOfDay !== 0) { | ||
fields[HOUR_OF_DAY] = toInt(timeOfDay / ONE_HOUR); | ||
var r = timeOfDay % ONE_HOUR; | ||
fields[MINUTE] = toInt(r / ONE_MINUTE); | ||
r %= ONE_MINUTE; | ||
fields[SECONDS] = toInt(r / ONE_SECOND); | ||
fields[MILLISECONDS] = r % ONE_SECOND; | ||
} else { | ||
fields[HOUR_OF_DAY] = | ||
fields[MINUTE] = | ||
fields[SECONDS] = | ||
fields[MILLISECONDS] = 0; | ||
} | ||
var fixedDateJan1 = Utils.getFixedDate(year, GregorianCalendar.JANUARY, 1); | ||
var dayOfYear = fixedDate - fixedDateJan1 + 1; | ||
var fixDateMonth1 = fixedDate - date.dayOfMonth + 1; | ||
var fixedDateJan1 = Utils.getFixedDate(year, GregorianCalendar.JANUARY, 1); | ||
var dayOfYear = fixedDate - fixedDateJan1 + 1; | ||
var fixDateMonth1 = fixedDate - date.dayOfMonth + 1; | ||
fields[DAY_OF_YEAR] = dayOfYear; | ||
fields[DAY_OF_WEEK_IN_MONTH] = toInt((date.dayOfMonth - 1) / 7) + 1; | ||
fields[DAY_OF_YEAR] = dayOfYear; | ||
fields[DAY_OF_WEEK_IN_MONTH] = toInt((date.dayOfMonth - 1) / 7) + 1; | ||
var weekOfYear = getWeekNumber(this, fixedDateJan1, fixedDate); | ||
var weekOfYear = getWeekNumber(this, fixedDateJan1, fixedDate); | ||
// 本周没有足够的时间在当前年 | ||
if (weekOfYear === 0) { | ||
// If the date belongs to the last week of the | ||
// previous year, use the week number of "12/31" of | ||
// the "previous" year. | ||
var fixedDec31 = fixedDateJan1 - 1; | ||
var prevJan1 = fixedDateJan1 - getYearLength(year - 1); | ||
weekOfYear = getWeekNumber(this, prevJan1, fixedDec31); | ||
} else | ||
// 本周是年末最后一周,可能有足够的时间在新的一年 | ||
if (weekOfYear >= 52) { | ||
var nextJan1 = fixedDateJan1 + getYearLength(year); | ||
var nextJan1st = getDayOfWeekDateOnOrBefore(nextJan1 + 6, this.firstDayOfWeek); | ||
var nDays = nextJan1st - nextJan1; | ||
// 本周有足够天数在新的一年 | ||
if (nDays >= this.minimalDaysInFirstWeek && | ||
// 当天确实在本周,weekOfYear === 53 时是不需要这个判断 | ||
fixedDate >= (nextJan1st - 7) | ||
) { | ||
weekOfYear = 1; | ||
} | ||
} | ||
// 本周没有足够的时间在当前年 | ||
if (weekOfYear === 0) { | ||
// If the date belongs to the last week of the | ||
// previous year, use the week number of "12/31" of | ||
// the "previous" year. | ||
var fixedDec31 = fixedDateJan1 - 1; | ||
var prevJan1 = fixedDateJan1 - getYearLength(year - 1); | ||
weekOfYear = getWeekNumber(this, prevJan1, fixedDec31); | ||
} else | ||
// 本周是年末最后一周,可能有足够的时间在新的一年 | ||
if (weekOfYear >= 52) { | ||
var nextJan1 = fixedDateJan1 + getYearLength(year); | ||
var nextJan1st = getDayOfWeekDateOnOrBefore(nextJan1 + 6, this.firstDayOfWeek); | ||
var nDays = nextJan1st - nextJan1; | ||
// 本周有足够天数在新的一年 | ||
if (nDays >= this.minimalDaysInFirstWeek && | ||
// 当天确实在本周,weekOfYear === 53 时是不需要这个判断 | ||
fixedDate >= (nextJan1st - 7) | ||
) { | ||
weekOfYear = 1; | ||
} | ||
} | ||
fields[WEEK_OF_YEAR] = weekOfYear; | ||
fields[WEEK_OF_MONTH] = getWeekNumber(this, fixDateMonth1, fixedDate); | ||
fields[WEEK_OF_YEAR] = weekOfYear; | ||
fields[WEEK_OF_MONTH] = getWeekNumber(this, fixDateMonth1, fixedDate); | ||
this.fieldsComputed = true; | ||
}, | ||
this.fieldsComputed = true; | ||
}, | ||
/** | ||
* Converts calendar field values to the time value | ||
* (millisecond offset from the Epoch). | ||
* @protected | ||
*/ | ||
computeTime: function () { | ||
if (!this.isSet(YEAR)) { | ||
throw new Error('year must be set for KISSY GregorianCalendar'); | ||
} | ||
/** | ||
* Converts calendar field values to the time value | ||
* (millisecond offset from the Epoch). | ||
* @protected | ||
*/ | ||
computeTime: function () { | ||
if (!this.isSet(YEAR)) { | ||
throw new Error('year must be set for KISSY GregorianCalendar'); | ||
} | ||
var fields = this.fields; | ||
var fields = this.fields; | ||
var year = fields[YEAR]; | ||
var timeOfDay = 0; | ||
if (this.isSet(HOUR_OF_DAY)) { | ||
timeOfDay += fields[HOUR_OF_DAY]; | ||
} | ||
timeOfDay *= 60; | ||
timeOfDay += fields[MINUTE] || 0; | ||
timeOfDay *= 60; | ||
timeOfDay += fields[SECONDS] || 0; | ||
timeOfDay *= 1000; | ||
timeOfDay += fields[MILLISECONDS] || 0; | ||
var year = fields[YEAR]; | ||
var timeOfDay = 0; | ||
if (this.isSet(HOUR_OF_DAY)) { | ||
timeOfDay += fields[HOUR_OF_DAY]; | ||
} | ||
timeOfDay *= 60; | ||
timeOfDay += fields[MINUTE] || 0; | ||
timeOfDay *= 60; | ||
timeOfDay += fields[SECONDS] || 0; | ||
timeOfDay *= 1000; | ||
timeOfDay += fields[MILLISECONDS] || 0; | ||
var fixedDate = 0; | ||
var fixedDate = 0; | ||
fields[YEAR] = year; | ||
fields[YEAR] = year; | ||
fixedDate = fixedDate + this.getFixedDate(); | ||
fixedDate = fixedDate + this.getFixedDate(); | ||
// millis represents local wall-clock time in milliseconds. | ||
var millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay; | ||
// millis represents local wall-clock time in milliseconds. | ||
var millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay; | ||
millis -= this.timezoneOffset * ONE_MINUTE; | ||
millis -= this.timezoneOffset * ONE_MINUTE; | ||
this.time = millis; | ||
this.time = millis; | ||
this.computeFields(); | ||
}, | ||
this.computeFields(); | ||
}, | ||
/** | ||
* Fills in any unset fields in the calendar fields. First, | ||
* the computeTime() method is called if the time value (millisecond offset from the Epoch) | ||
* has not been calculated from calendar field values. | ||
* Then, the computeFields() method is called to calculate all calendar field values. | ||
* @protected | ||
*/ | ||
complete: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
if (!this.fieldsComputed) { | ||
this.computeFields(); | ||
} | ||
}, | ||
/** | ||
* Fills in any unset fields in the calendar fields. First, | ||
* the computeTime() method is called if the time value (millisecond offset from the Epoch) | ||
* has not been calculated from calendar field values. | ||
* Then, the computeFields() method is called to calculate all calendar field values. | ||
* @protected | ||
*/ | ||
complete: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
if (!this.fieldsComputed) { | ||
this.computeFields(); | ||
} | ||
}, | ||
getFixedDate: function () { | ||
getFixedDate: function () { | ||
var self = this; | ||
var self = this; | ||
var fields = self.fields; | ||
var fields = self.fields; | ||
var firstDayOfWeekCfg = self.firstDayOfWeek; | ||
var firstDayOfWeekCfg = self.firstDayOfWeek; | ||
var year = fields[YEAR]; | ||
var year = fields[YEAR]; | ||
var month = GregorianCalendar.JANUARY; | ||
var month = GregorianCalendar.JANUARY; | ||
if (self.isSet(MONTH)) { | ||
month = fields[MONTH]; | ||
if (month > GregorianCalendar.DECEMBER) { | ||
year += toInt(month / 12); | ||
month %= 12; | ||
} else if (month < GregorianCalendar.JANUARY) { | ||
year += floorDivide(month / 12); | ||
month = mod(month, 12); | ||
} | ||
} | ||
if (self.isSet(MONTH)) { | ||
month = fields[MONTH]; | ||
if (month > GregorianCalendar.DECEMBER) { | ||
year += toInt(month / 12); | ||
month %= 12; | ||
} else if (month < GregorianCalendar.JANUARY) { | ||
year += floorDivide(month / 12); | ||
month = mod(month, 12); | ||
} | ||
} | ||
// Get the fixed date since Jan 1, 1 (Gregorian). We are on | ||
// the first day of either `month' or January in 'year'. | ||
var fixedDate = Utils.getFixedDate(year, month, 1); | ||
var firstDayOfWeek; | ||
var dayOfWeek = self.firstDayOfWeek; | ||
// Get the fixed date since Jan 1, 1 (Gregorian). We are on | ||
// the first day of either `month' or January in 'year'. | ||
var fixedDate = Utils.getFixedDate(year, month, 1); | ||
var firstDayOfWeek; | ||
var dayOfWeek = self.firstDayOfWeek; | ||
if (self.isSet(DAY_OF_WEEK)) { | ||
dayOfWeek = fields[DAY_OF_WEEK]; | ||
} | ||
if (self.isSet(DAY_OF_WEEK)) { | ||
dayOfWeek = fields[DAY_OF_WEEK]; | ||
} | ||
if (self.isSet(MONTH)) { | ||
if (self.isSet(DAY_OF_MONTH)) { | ||
fixedDate += fields[DAY_OF_MONTH] - 1; | ||
} else { | ||
if (self.isSet(WEEK_OF_MONTH)) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); | ||
if (self.isSet(MONTH)) { | ||
if (self.isSet(DAY_OF_MONTH)) { | ||
fixedDate += fields[DAY_OF_MONTH] - 1; | ||
} else { | ||
if (self.isSet(WEEK_OF_MONTH)) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); | ||
// If we have enough days in the first week, then | ||
// move to the previous week. | ||
if ((firstDayOfWeek - fixedDate) >= self.minimalDaysInFirstWeek) { | ||
firstDayOfWeek -= 7; | ||
} | ||
// If we have enough days in the first week, then | ||
// move to the previous week. | ||
if ((firstDayOfWeek - fixedDate) >= self.minimalDaysInFirstWeek) { | ||
firstDayOfWeek -= 7; | ||
} | ||
if (dayOfWeek !== firstDayOfWeekCfg) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); | ||
} | ||
if (dayOfWeek !== firstDayOfWeekCfg) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); | ||
} | ||
fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_MONTH] - 1); | ||
} else { | ||
var dowim; | ||
if (self.isSet(DAY_OF_WEEK_IN_MONTH)) { | ||
dowim = fields[DAY_OF_WEEK_IN_MONTH]; | ||
} else { | ||
dowim = 1; | ||
} | ||
var lastDate = (7 * dowim); | ||
if (dowim < 0) { | ||
lastDate = getMonthLength(year, month) + (7 * (dowim + 1)); | ||
} | ||
fixedDate = getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, dayOfWeek); | ||
} | ||
} | ||
fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_MONTH] - 1); | ||
} else { | ||
// We are on the first day of the year. | ||
if (self.isSet(DAY_OF_YEAR)) { | ||
fixedDate += fields[DAY_OF_YEAR] - 1; | ||
} else { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); | ||
// If we have enough days in the first week, then move | ||
// to the previous week. | ||
if ((firstDayOfWeek - fixedDate) >= self.minimalDaysInFirstWeek) { | ||
firstDayOfWeek -= 7; | ||
} | ||
if (dayOfWeek !== firstDayOfWeekCfg) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); | ||
} | ||
fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_YEAR] - 1); | ||
} | ||
var dowim; | ||
if (self.isSet(DAY_OF_WEEK_IN_MONTH)) { | ||
dowim = fields[DAY_OF_WEEK_IN_MONTH]; | ||
} else { | ||
dowim = 1; | ||
} | ||
var lastDate = (7 * dowim); | ||
if (dowim < 0) { | ||
lastDate = getMonthLength(year, month) + (7 * (dowim + 1)); | ||
} | ||
fixedDate = getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, dayOfWeek); | ||
} | ||
} | ||
} else { | ||
// We are on the first day of the year. | ||
if (self.isSet(DAY_OF_YEAR)) { | ||
fixedDate += fields[DAY_OF_YEAR] - 1; | ||
} else { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); | ||
// If we have enough days in the first week, then move | ||
// to the previous week. | ||
if ((firstDayOfWeek - fixedDate) >= self.minimalDaysInFirstWeek) { | ||
firstDayOfWeek -= 7; | ||
} | ||
if (dayOfWeek !== firstDayOfWeekCfg) { | ||
firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); | ||
} | ||
fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_YEAR] - 1); | ||
} | ||
} | ||
return fixedDate; | ||
}, | ||
return fixedDate; | ||
}, | ||
/** | ||
* Returns this Calendar's time value in milliseconds | ||
* @member Date.Gregorian | ||
* @returns {Number} the current time as UTC milliseconds from the epoch. | ||
*/ | ||
getTime: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
return this.time; | ||
}, | ||
/** | ||
* Returns this Calendar's time value in milliseconds | ||
* @member Date.Gregorian | ||
* @returns {Number} the current time as UTC milliseconds from the epoch. | ||
*/ | ||
getTime: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
return this.time; | ||
}, | ||
/** | ||
* Sets this Calendar's current time from the given long value. | ||
* @param time the new time in UTC milliseconds from the epoch. | ||
*/ | ||
setTime: function (time) { | ||
this.time = time; | ||
this.fieldsComputed = false; | ||
this.complete(); | ||
}, | ||
/** | ||
* Sets this Calendar's current time from the given long value. | ||
* @param time the new time in UTC milliseconds from the epoch. | ||
*/ | ||
setTime: function (time) { | ||
this.time = time; | ||
this.fieldsComputed = false; | ||
this.complete(); | ||
}, | ||
/** | ||
* Returns the value of the given calendar field. | ||
* @param field the given calendar field. | ||
* @returns {Number} the value for the given calendar field. | ||
*/ | ||
get: function (field) { | ||
this.complete(); | ||
return this.fields[field]; | ||
}, | ||
/** | ||
* Returns the value of the given calendar field. | ||
* @param field the given calendar field. | ||
* @returns {Number} the value for the given calendar field. | ||
*/ | ||
get: function (field) { | ||
this.complete(); | ||
return this.fields[field]; | ||
}, | ||
/** | ||
* Returns the year of the given calendar field. | ||
* @method getYear | ||
* @returns {Number} the year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the year of the given calendar field. | ||
* @method getYear | ||
* @returns {Number} the year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the month of the given calendar field. | ||
* @method getMonth | ||
* @returns {Number} the month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the month of the given calendar field. | ||
* @method getMonth | ||
* @returns {Number} the month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of month of the given calendar field. | ||
* @method getDayOfMonth | ||
* @returns {Number} the day of month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of month of the given calendar field. | ||
* @method getDayOfMonth | ||
* @returns {Number} the day of month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the hour of day of the given calendar field. | ||
* @method getHourOfDay | ||
* @returns {Number} the hour of day for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the hour of day of the given calendar field. | ||
* @method getHourOfDay | ||
* @returns {Number} the hour of day for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the minute of the given calendar field. | ||
* @method getMinute | ||
* @returns {Number} the minute for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the minute of the given calendar field. | ||
* @method getMinute | ||
* @returns {Number} the minute for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the second of the given calendar field. | ||
* @method getSecond | ||
* @returns {Number} the second for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the second of the given calendar field. | ||
* @method getSecond | ||
* @returns {Number} the second for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the millisecond of the given calendar field. | ||
* @method getMilliSecond | ||
* @returns {Number} the millisecond for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the millisecond of the given calendar field. | ||
* @method getMilliSecond | ||
* @returns {Number} the millisecond for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the week of year of the given calendar field. | ||
* @method getWeekOfYear | ||
* @returns {Number} the week of year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the week of year of the given calendar field. | ||
* @method getWeekOfYear | ||
* @returns {Number} the week of year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the week of month of the given calendar field. | ||
* @method getWeekOfMonth | ||
* @returns {Number} the week of month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the week of month of the given calendar field. | ||
* @method getWeekOfMonth | ||
* @returns {Number} the week of month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of year of the given calendar field. | ||
* @method getDayOfYear | ||
* @returns {Number} the day of year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of year of the given calendar field. | ||
* @method getDayOfYear | ||
* @returns {Number} the day of year for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of week of the given calendar field. | ||
* @method getDayOfWeek | ||
* @returns {Number} the day of week for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of week of the given calendar field. | ||
* @method getDayOfWeek | ||
* @returns {Number} the day of week for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of week in month of the given calendar field. | ||
* @method getDayOfWeekInMonth | ||
* @returns {Number} the day of week in month for the given calendar field. | ||
*/ | ||
/** | ||
* Returns the day of week in month of the given calendar field. | ||
* @method getDayOfWeekInMonth | ||
* @returns {Number} the day of week in month for the given calendar field. | ||
*/ | ||
/** | ||
* Sets the given calendar field to the given value. | ||
* @param field the given calendar field. | ||
* @param v the value to be set for the given calendar field. | ||
*/ | ||
set: function (field, v) { | ||
var len = arguments.length; | ||
if (len === 2) { | ||
this.fields[field] = v; | ||
} else if (len < MILLISECONDS + 1) { | ||
for (var i = 0; i < len; i++) { | ||
this.fields[YEAR + i] = arguments[i]; | ||
} | ||
} else { | ||
throw new Error('illegal arguments for KISSY GregorianCalendar set'); | ||
} | ||
this.time = undefined; | ||
}, | ||
/** | ||
* Sets the given calendar field to the given value. | ||
* @param field the given calendar field. | ||
* @param v the value to be set for the given calendar field. | ||
*/ | ||
set: function (field, v) { | ||
var len = arguments.length; | ||
if (len === 2) { | ||
this.fields[field] = v; | ||
} else if (len < MILLISECONDS + 1) { | ||
for (var i = 0; i < len; i++) { | ||
this.fields[YEAR + i] = arguments[i]; | ||
} | ||
} else { | ||
throw new Error('illegal arguments for KISSY GregorianCalendar set'); | ||
} | ||
this.time = undefined; | ||
}, | ||
/** | ||
* Set the year of the given calendar field. | ||
* @method setYear | ||
*/ | ||
/** | ||
* Set the year of the given calendar field. | ||
* @method setYear | ||
*/ | ||
/** | ||
* Set the month of the given calendar field. | ||
* @method setMonth | ||
*/ | ||
/** | ||
* Set the month of the given calendar field. | ||
* @method setMonth | ||
*/ | ||
/** | ||
* Set the day of month of the given calendar field. | ||
* @method setDayOfMonth | ||
*/ | ||
/** | ||
* Set the day of month of the given calendar field. | ||
* @method setDayOfMonth | ||
*/ | ||
/** | ||
* Set the hour of day of the given calendar field. | ||
* @method setHourOfDay | ||
*/ | ||
/** | ||
* Set the hour of day of the given calendar field. | ||
* @method setHourOfDay | ||
*/ | ||
/** | ||
* Set the minute of the given calendar field. | ||
* @method setMinute | ||
*/ | ||
/** | ||
* Set the minute of the given calendar field. | ||
* @method setMinute | ||
*/ | ||
/** | ||
* Set the second of the given calendar field. | ||
* @method setSecond | ||
*/ | ||
/** | ||
* Set the second of the given calendar field. | ||
* @method setSecond | ||
*/ | ||
/** | ||
* Set the millisecond of the given calendar field. | ||
* @method setMilliSecond | ||
*/ | ||
/** | ||
* Set the millisecond of the given calendar field. | ||
* @method setMilliSecond | ||
*/ | ||
/** | ||
* Set the week of year of the given calendar field. | ||
* @method setWeekOfYear | ||
*/ | ||
/** | ||
* Set the week of year of the given calendar field. | ||
* @method setWeekOfYear | ||
*/ | ||
/** | ||
* Set the week of month of the given calendar field. | ||
* @method setWeekOfMonth | ||
*/ | ||
/** | ||
* Set the week of month of the given calendar field. | ||
* @method setWeekOfMonth | ||
*/ | ||
/** | ||
* Set the day of year of the given calendar field. | ||
* @method setDayOfYear | ||
*/ | ||
/** | ||
* Set the day of year of the given calendar field. | ||
* @method setDayOfYear | ||
*/ | ||
/** | ||
* Set the day of week of the given calendar field. | ||
* @method setDayOfWeek | ||
*/ | ||
/** | ||
* Set the day of week of the given calendar field. | ||
* @method setDayOfWeek | ||
*/ | ||
/** | ||
* Set the day of week in month of the given calendar field. | ||
* @method setDayOfWeekInMonth | ||
*/ | ||
/** | ||
* Set the day of week in month of the given calendar field. | ||
* @method setDayOfWeekInMonth | ||
*/ | ||
/** | ||
* add for specified field based on two rules: | ||
* | ||
* - Add rule 1. The value of field after the call minus the value of field before the | ||
* call is amount, modulo any overflow that has occurred in field | ||
* Overflow occurs when a field value exceeds its range and, | ||
* as a result, the next larger field is incremented or | ||
* decremented and the field value is adjusted back into its range. | ||
* | ||
* - Add rule 2. If a smaller field is expected to be invariant, | ||
* but it is impossible for it to be equal to its | ||
* prior value because of changes in its minimum or maximum after | ||
* field is changed, then its value is adjusted to be as close | ||
* as possible to its expected value. A smaller field represents a | ||
* smaller unit of time. HOUR_OF_DAY is a smaller field than | ||
* DAY_OF_MONTH. No adjustment is made to smaller fields | ||
* that are not expected to be invariant. The calendar system | ||
* determines what fields are expected to be invariant. | ||
* | ||
* | ||
* @example | ||
* use('date/gregorian',function(S, GregorianCalendar){ | ||
/** | ||
* add for specified field based on two rules: | ||
* | ||
* - Add rule 1. The value of field after the call minus the value of field before the | ||
* call is amount, modulo any overflow that has occurred in field | ||
* Overflow occurs when a field value exceeds its range and, | ||
* as a result, the next larger field is incremented or | ||
* decremented and the field value is adjusted back into its range. | ||
* | ||
* - Add rule 2. If a smaller field is expected to be invariant, | ||
* but it is impossible for it to be equal to its | ||
* prior value because of changes in its minimum or maximum after | ||
* field is changed, then its value is adjusted to be as close | ||
* as possible to its expected value. A smaller field represents a | ||
* smaller unit of time. HOUR_OF_DAY is a smaller field than | ||
* DAY_OF_MONTH. No adjustment is made to smaller fields | ||
* that are not expected to be invariant. The calendar system | ||
* determines what fields are expected to be invariant. | ||
* | ||
* | ||
* @example | ||
* use('date/gregorian',function(S, GregorianCalendar){ | ||
* var d = new GregorianCalendar(); | ||
@@ -815,466 +810,466 @@ * d.set(2012, GregorianCalendar.JANUARY, 31); | ||
* }); | ||
* | ||
* @param field the calendar field. | ||
* @param {Number} amount he amount of date or time to be added to the field. | ||
*/ | ||
add: function (field, amount) { | ||
if (!amount) { | ||
return; | ||
} | ||
var self = this; | ||
var fields = self.fields; | ||
// computer and retrieve original value | ||
var value = self.get(field); | ||
if (field === YEAR) { | ||
value += amount; | ||
self.set(YEAR, value); | ||
adjustDayOfMonth(self); | ||
} else if (field === MONTH) { | ||
value += amount; | ||
var yearAmount = floorDivide(value / 12); | ||
value = mod(value, 12); | ||
if (yearAmount) { | ||
self.set(YEAR, fields[YEAR] + yearAmount); | ||
} | ||
self.set(MONTH, value); | ||
adjustDayOfMonth(self); | ||
} else { | ||
switch (field) { | ||
case HOUR_OF_DAY: | ||
amount *= ONE_HOUR; | ||
break; | ||
case MINUTE: | ||
amount *= ONE_MINUTE; | ||
break; | ||
case SECONDS: | ||
amount *= ONE_SECOND; | ||
break; | ||
case MILLISECONDS: | ||
break; | ||
case WEEK_OF_MONTH: | ||
case WEEK_OF_YEAR: | ||
case DAY_OF_WEEK_IN_MONTH: | ||
amount *= ONE_WEEK; | ||
break; | ||
case DAY_OF_WEEK: | ||
case DAY_OF_YEAR: | ||
case DAY_OF_MONTH: | ||
amount *= ONE_DAY; | ||
break; | ||
default: | ||
throw new Error('illegal field for add'); | ||
} | ||
self.setTime(self.time + amount); | ||
} | ||
* | ||
* @param field the calendar field. | ||
* @param {Number} amount he amount of date or time to be added to the field. | ||
*/ | ||
add: function (field, amount) { | ||
if (!amount) { | ||
return; | ||
} | ||
var self = this; | ||
var fields = self.fields; | ||
// computer and retrieve original value | ||
var value = self.get(field); | ||
if (field === YEAR) { | ||
value += amount; | ||
self.set(YEAR, value); | ||
adjustDayOfMonth(self); | ||
} else if (field === MONTH) { | ||
value += amount; | ||
var yearAmount = floorDivide(value / 12); | ||
value = mod(value, 12); | ||
if (yearAmount) { | ||
self.set(YEAR, fields[YEAR] + yearAmount); | ||
} | ||
self.set(MONTH, value); | ||
adjustDayOfMonth(self); | ||
} else { | ||
switch (field) { | ||
case HOUR_OF_DAY: | ||
amount *= ONE_HOUR; | ||
break; | ||
case MINUTE: | ||
amount *= ONE_MINUTE; | ||
break; | ||
case SECONDS: | ||
amount *= ONE_SECOND; | ||
break; | ||
case MILLISECONDS: | ||
break; | ||
case WEEK_OF_MONTH: | ||
case WEEK_OF_YEAR: | ||
case DAY_OF_WEEK_IN_MONTH: | ||
amount *= ONE_WEEK; | ||
break; | ||
case DAY_OF_WEEK: | ||
case DAY_OF_YEAR: | ||
case DAY_OF_MONTH: | ||
amount *= ONE_DAY; | ||
break; | ||
default: | ||
throw new Error('illegal field for add'); | ||
} | ||
self.setTime(self.time + amount); | ||
} | ||
}, | ||
}, | ||
/** | ||
* add the year of the given calendar field. | ||
* @method addYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the year of the given calendar field. | ||
* @method addYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the month of the given calendar field. | ||
* @method addMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the month of the given calendar field. | ||
* @method addMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of month of the given calendar field. | ||
* @method addDayOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of month of the given calendar field. | ||
* @method addDayOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the hour of day of the given calendar field. | ||
* @method addHourOfDay | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the hour of day of the given calendar field. | ||
* @method addHourOfDay | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the minute of the given calendar field. | ||
* @method addMinute | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the minute of the given calendar field. | ||
* @method addMinute | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the second of the given calendar field. | ||
* @method addSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the second of the given calendar field. | ||
* @method addSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the millisecond of the given calendar field. | ||
* @method addMilliSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the millisecond of the given calendar field. | ||
* @method addMilliSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the week of year of the given calendar field. | ||
* @method addWeekOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the week of year of the given calendar field. | ||
* @method addWeekOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the week of month of the given calendar field. | ||
* @method addWeekOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the week of month of the given calendar field. | ||
* @method addWeekOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of year of the given calendar field. | ||
* @method addDayOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of year of the given calendar field. | ||
* @method addDayOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of week of the given calendar field. | ||
* @method addDayOfWeek | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of week of the given calendar field. | ||
* @method addDayOfWeek | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of week in month of the given calendar field. | ||
* @method addDayOfWeekInMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* add the day of week in month of the given calendar field. | ||
* @method addDayOfWeekInMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* Get rolled value for the field | ||
* @protected | ||
*/ | ||
getRolledValue: function (value, amount, min, max) { | ||
var diff = value - min; | ||
var range = max - min + 1; | ||
amount %= range; | ||
return min + (diff + amount + range) % range; | ||
}, | ||
/** | ||
* Get rolled value for the field | ||
* @protected | ||
*/ | ||
getRolledValue: function (value, amount, min, max) { | ||
var diff = value - min; | ||
var range = max - min + 1; | ||
amount %= range; | ||
return min + (diff + amount + range) % range; | ||
}, | ||
/** | ||
* Adds a signed amount to the specified calendar field without changing larger fields. | ||
* A negative roll amount means to subtract from field without changing | ||
* larger fields. If the specified amount is 0, this method performs nothing. | ||
* | ||
* | ||
* | ||
* @example | ||
* var d = new GregorianCalendar(); | ||
* d.set(1999, GregorianCalendar.AUGUST, 31); | ||
* // 1999-4-30 | ||
* // Tuesday June 1, 1999 | ||
* d.set(1999, GregorianCalendar.JUNE, 1); | ||
* d.add(Gregorian.WEEK_OF_MONTH,-1); // === d.add(Gregorian.WEEK_OF_MONTH, | ||
* d.get(Gregorian.WEEK_OF_MONTH)); | ||
* // 1999-06-29 | ||
* | ||
* | ||
* @param field the calendar field. | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
roll: function (field, amount) { | ||
if (!amount) { | ||
return; | ||
} | ||
var self = this; | ||
// computer and retrieve original value | ||
var value = self.get(field); | ||
var min = self.getActualMinimum(field); | ||
var max = self.getActualMaximum(field); | ||
value = self.getRolledValue(value, amount, min, max); | ||
/** | ||
* Adds a signed amount to the specified calendar field without changing larger fields. | ||
* A negative roll amount means to subtract from field without changing | ||
* larger fields. If the specified amount is 0, this method performs nothing. | ||
* | ||
* | ||
* | ||
* @example | ||
* var d = new GregorianCalendar(); | ||
* d.set(1999, GregorianCalendar.AUGUST, 31); | ||
* // 1999-4-30 | ||
* // Tuesday June 1, 1999 | ||
* d.set(1999, GregorianCalendar.JUNE, 1); | ||
* d.add(Gregorian.WEEK_OF_MONTH,-1); // === d.add(Gregorian.WEEK_OF_MONTH, | ||
* d.get(Gregorian.WEEK_OF_MONTH)); | ||
* // 1999-06-29 | ||
* | ||
* | ||
* @param field the calendar field. | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
roll: function (field, amount) { | ||
if (!amount) { | ||
return; | ||
} | ||
var self = this; | ||
// computer and retrieve original value | ||
var value = self.get(field); | ||
var min = self.getActualMinimum(field); | ||
var max = self.getActualMaximum(field); | ||
value = self.getRolledValue(value, amount, min, max); | ||
self.set(field, value); | ||
self.set(field, value); | ||
// consider compute time priority | ||
switch (field) { | ||
case MONTH: | ||
adjustDayOfMonth(self); | ||
break; | ||
default: | ||
// other fields are set already when get | ||
self.updateFieldsBySet(field); | ||
break; | ||
} | ||
}, | ||
// consider compute time priority | ||
switch (field) { | ||
case MONTH: | ||
adjustDayOfMonth(self); | ||
break; | ||
default: | ||
// other fields are set already when get | ||
self.updateFieldsBySet(field); | ||
break; | ||
} | ||
}, | ||
/** | ||
* roll the year of the given calendar field. | ||
* @method rollYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the year of the given calendar field. | ||
* @method rollYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the month of the given calendar field. | ||
* @param {Number} amount the signed amount to add to field. | ||
* @method rollMonth | ||
*/ | ||
/** | ||
* roll the month of the given calendar field. | ||
* @param {Number} amount the signed amount to add to field. | ||
* @method rollMonth | ||
*/ | ||
/** | ||
* roll the day of month of the given calendar field. | ||
* @method rollDayOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the day of month of the given calendar field. | ||
* @method rollDayOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the hour of day of the given calendar field. | ||
* @method rollHourOfDay | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the hour of day of the given calendar field. | ||
* @method rollHourOfDay | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the minute of the given calendar field. | ||
* @method rollMinute | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the minute of the given calendar field. | ||
* @method rollMinute | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the second of the given calendar field. | ||
* @method rollSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the second of the given calendar field. | ||
* @method rollSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the millisecond of the given calendar field. | ||
* @method rollMilliSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the millisecond of the given calendar field. | ||
* @method rollMilliSecond | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the week of year of the given calendar field. | ||
* @method rollWeekOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the week of year of the given calendar field. | ||
* @method rollWeekOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the week of month of the given calendar field. | ||
* @method rollWeekOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the week of month of the given calendar field. | ||
* @method rollWeekOfMonth | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the day of year of the given calendar field. | ||
* @method rollDayOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the day of year of the given calendar field. | ||
* @method rollDayOfYear | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the day of week of the given calendar field. | ||
* @method rollDayOfWeek | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* roll the day of week of the given calendar field. | ||
* @method rollDayOfWeek | ||
* @param {Number} amount the signed amount to add to field. | ||
*/ | ||
/** | ||
* remove other priority fields when call getFixedDate | ||
* precondition: other fields are all set or computed | ||
* @protected | ||
*/ | ||
updateFieldsBySet: function (field) { | ||
var fields = this.fields; | ||
switch (field) { | ||
case WEEK_OF_MONTH: | ||
fields[DAY_OF_MONTH] = undefined; | ||
break; | ||
case DAY_OF_YEAR: | ||
fields[MONTH] = undefined; | ||
break; | ||
case DAY_OF_WEEK: | ||
fields[DAY_OF_MONTH] = undefined; | ||
break; | ||
case WEEK_OF_YEAR: | ||
fields[DAY_OF_YEAR] = undefined; | ||
fields[MONTH] = undefined; | ||
break; | ||
} | ||
}, | ||
/** | ||
* remove other priority fields when call getFixedDate | ||
* precondition: other fields are all set or computed | ||
* @protected | ||
*/ | ||
updateFieldsBySet: function (field) { | ||
var fields = this.fields; | ||
switch (field) { | ||
case WEEK_OF_MONTH: | ||
fields[DAY_OF_MONTH] = undefined; | ||
break; | ||
case DAY_OF_YEAR: | ||
fields[MONTH] = undefined; | ||
break; | ||
case DAY_OF_WEEK: | ||
fields[DAY_OF_MONTH] = undefined; | ||
break; | ||
case WEEK_OF_YEAR: | ||
fields[DAY_OF_YEAR] = undefined; | ||
fields[MONTH] = undefined; | ||
break; | ||
} | ||
}, | ||
/** | ||
* get current date instance's timezone offset | ||
* @returns {Number} | ||
*/ | ||
getTimezoneOffset: function () { | ||
return this.timezoneOffset; | ||
}, | ||
/** | ||
* get current date instance's timezone offset | ||
* @returns {Number} | ||
*/ | ||
getTimezoneOffset: function () { | ||
return this.timezoneOffset; | ||
}, | ||
/** | ||
* set current date instance's timezone offset | ||
*/ | ||
setTimezoneOffset: function (timezoneOffset) { | ||
if (this.timezoneOffset !== timezoneOffset) { | ||
this.fieldsComputed = undefined; | ||
this.timezoneOffset = timezoneOffset; | ||
} | ||
}, | ||
/** | ||
* set current date instance's timezone offset | ||
*/ | ||
setTimezoneOffset: function (timezoneOffset) { | ||
if (this.timezoneOffset !== timezoneOffset) { | ||
this.fieldsComputed = undefined; | ||
this.timezoneOffset = timezoneOffset; | ||
} | ||
}, | ||
/** | ||
* set first day of week for current date instance | ||
*/ | ||
setFirstDayOfWeek: function (firstDayOfWeek) { | ||
if (this.firstDayOfWeek !== firstDayOfWeek) { | ||
this.firstDayOfWeek = firstDayOfWeek; | ||
this.fieldsComputed = false; | ||
} | ||
}, | ||
/** | ||
* set first day of week for current date instance | ||
*/ | ||
setFirstDayOfWeek: function (firstDayOfWeek) { | ||
if (this.firstDayOfWeek !== firstDayOfWeek) { | ||
this.firstDayOfWeek = firstDayOfWeek; | ||
this.fieldsComputed = false; | ||
} | ||
}, | ||
/** | ||
* Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France. | ||
* @returns {Number} the first day of the week. | ||
*/ | ||
getFirstDayOfWeek: function () { | ||
return this.firstDayOfWeek; | ||
}, | ||
/** | ||
* Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France. | ||
* @returns {Number} the first day of the week. | ||
*/ | ||
getFirstDayOfWeek: function () { | ||
return this.firstDayOfWeek; | ||
}, | ||
/** | ||
* Sets what the minimal days required in the first week of the year are; For example, | ||
* if the first week is defined as one that contains the first day of the first month of a year, | ||
* call this method with value 1. | ||
* If it must be a full week, use value 7. | ||
* @param minimalDaysInFirstWeek the given minimal days required in the first week of the year. | ||
*/ | ||
setMinimalDaysInFirstWeek: function (minimalDaysInFirstWeek) { | ||
if (this.minimalDaysInFirstWeek !== minimalDaysInFirstWeek) { | ||
this.minimalDaysInFirstWeek = minimalDaysInFirstWeek; | ||
this.fieldsComputed = false; | ||
} | ||
}, | ||
/** | ||
* Sets what the minimal days required in the first week of the year are; For example, | ||
* if the first week is defined as one that contains the first day of the first month of a year, | ||
* call this method with value 1. | ||
* If it must be a full week, use value 7. | ||
* @param minimalDaysInFirstWeek the given minimal days required in the first week of the year. | ||
*/ | ||
setMinimalDaysInFirstWeek: function (minimalDaysInFirstWeek) { | ||
if (this.minimalDaysInFirstWeek !== minimalDaysInFirstWeek) { | ||
this.minimalDaysInFirstWeek = minimalDaysInFirstWeek; | ||
this.fieldsComputed = false; | ||
} | ||
}, | ||
/** | ||
* Gets what the minimal days required in the first week of the year are; e.g., | ||
* if the first week is defined as one that contains the first day of the first month of a year, | ||
* this method returns 1. | ||
* If the minimal days required must be a full week, this method returns 7. | ||
* @returns {Number} the minimal days required in the first week of the year. | ||
*/ | ||
getMinimalDaysInFirstWeek: function () { | ||
return this.minimalDaysInFirstWeek; | ||
}, | ||
/** | ||
* Gets what the minimal days required in the first week of the year are; e.g., | ||
* if the first week is defined as one that contains the first day of the first month of a year, | ||
* this method returns 1. | ||
* If the minimal days required must be a full week, this method returns 7. | ||
* @returns {Number} the minimal days required in the first week of the year. | ||
*/ | ||
getMinimalDaysInFirstWeek: function () { | ||
return this.minimalDaysInFirstWeek; | ||
}, | ||
/** | ||
* Returns the number of weeks in the week year | ||
* represented by this GregorianCalendar. | ||
* | ||
* For example, if this GregorianCalendar's date is | ||
* December 31, 2008 with the ISO | ||
* 8601 compatible setting, this method will return 53 for the | ||
* period: December 29, 2008 to January 3, 2010 | ||
* while getActualMaximum(WEEK_OF_YEAR) will return | ||
* 52 for the period: December 31, 2007 to December 28, 2008. | ||
* | ||
* @return {Number} the number of weeks in the week year. | ||
*/ | ||
getWeeksInWeekYear: function () { | ||
var weekYear = this.getWeekYear(); | ||
if (weekYear === this.get(YEAR)) { | ||
return this.getActualMaximum(WEEK_OF_YEAR); | ||
} | ||
// Use the 2nd week for calculating the max of WEEK_OF_YEAR | ||
var gc = this.clone(); | ||
gc.setWeekDate(weekYear, 2, this.get(DAY_OF_WEEK)); | ||
return gc.getActualMaximum(WEEK_OF_YEAR); | ||
}, | ||
/** | ||
* Returns the number of weeks in the week year | ||
* represented by this GregorianCalendar. | ||
* | ||
* For example, if this GregorianCalendar's date is | ||
* December 31, 2008 with the ISO | ||
* 8601 compatible setting, this method will return 53 for the | ||
* period: December 29, 2008 to January 3, 2010 | ||
* while getActualMaximum(WEEK_OF_YEAR) will return | ||
* 52 for the period: December 31, 2007 to December 28, 2008. | ||
* | ||
* @return {Number} the number of weeks in the week year. | ||
*/ | ||
getWeeksInWeekYear: function () { | ||
var weekYear = this.getWeekYear(); | ||
if (weekYear === this.get(YEAR)) { | ||
return this.getActualMaximum(WEEK_OF_YEAR); | ||
} | ||
// Use the 2nd week for calculating the max of WEEK_OF_YEAR | ||
var gc = this.clone(); | ||
gc.setWeekDate(weekYear, 2, this.get(DAY_OF_WEEK)); | ||
return gc.getActualMaximum(WEEK_OF_YEAR); | ||
}, | ||
/** | ||
* Returns the week year represented by this GregorianCalendar. | ||
* The dates in the weeks between 1 and the | ||
* maximum week number of the week year have the same week year value | ||
* that may be one year before or after the calendar year value. | ||
* | ||
* @return {Number} the week year represented by this GregorianCalendar. | ||
*/ | ||
getWeekYear: function () { | ||
var year = this.get(YEAR); // implicitly complete | ||
var weekOfYear = this.get(WEEK_OF_YEAR); | ||
var month = this.get(MONTH); | ||
if (month === GregorianCalendar.JANUARY) { | ||
if (weekOfYear >= 52) { | ||
--year; | ||
} | ||
} else if (month === GregorianCalendar.DECEMBER) { | ||
if (weekOfYear === 1) { | ||
++year; | ||
} | ||
} | ||
return year; | ||
}, | ||
/** | ||
* Sets this GregorianCalendar to the date given by the date specifiers - weekYear, | ||
* weekOfYear, and dayOfWeek. weekOfYear follows the WEEK_OF_YEAR numbering. | ||
* The dayOfWeek value must be one of the DAY_OF_WEEK values: SUNDAY to SATURDAY. | ||
* | ||
* @param weekYear the week year | ||
* @param weekOfYear the week number based on weekYear | ||
* @param dayOfWeek the day of week value | ||
*/ | ||
setWeekDate: function (weekYear, weekOfYear, dayOfWeek) { | ||
if (dayOfWeek < GregorianCalendar.SUNDAY || dayOfWeek > GregorianCalendar.SATURDAY) { | ||
throw new Error('invalid dayOfWeek: ' + dayOfWeek); | ||
} | ||
var fields = this.fields; | ||
// To avoid changing the time of day fields by date | ||
// calculations, use a clone with the GMT time zone. | ||
var gc = this.clone(); | ||
gc.clear(); | ||
gc.setTimezoneOffset(0); | ||
gc.set(YEAR, weekYear); | ||
gc.set(WEEK_OF_YEAR, 1); | ||
gc.set(DAY_OF_WEEK, this.getFirstDayOfWeek()); | ||
var days = dayOfWeek - this.getFirstDayOfWeek(); | ||
if (days < 0) { | ||
days += 7; | ||
} | ||
days += 7 * (weekOfYear - 1); | ||
if (days !== 0) { | ||
gc.add(DAY_OF_YEAR, days); | ||
} else { | ||
gc.complete(); | ||
} | ||
fields[YEAR] = gc.get(YEAR); | ||
fields[MONTH] = gc.get(MONTH); | ||
fields[DAY_OF_MONTH] = gc.get(DAY_OF_MONTH); | ||
this.complete(); | ||
}, | ||
/** | ||
* Creates and returns a copy of this object. | ||
* @returns {Date.Gregorian} | ||
*/ | ||
clone: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
var cal = new GregorianCalendar(this.timezoneOffset, this.locale); | ||
cal.setTime(this.time); | ||
return cal; | ||
}, | ||
/** | ||
* Returns the week year represented by this GregorianCalendar. | ||
* The dates in the weeks between 1 and the | ||
* maximum week number of the week year have the same week year value | ||
* that may be one year before or after the calendar year value. | ||
* | ||
* @return {Number} the week year represented by this GregorianCalendar. | ||
*/ | ||
getWeekYear: function () { | ||
var year = this.get(YEAR); // implicitly complete | ||
var weekOfYear = this.get(WEEK_OF_YEAR); | ||
var month = this.get(MONTH); | ||
if (month === GregorianCalendar.JANUARY) { | ||
if (weekOfYear >= 52) { | ||
--year; | ||
} | ||
} else if (month === GregorianCalendar.DECEMBER) { | ||
if (weekOfYear === 1) { | ||
++year; | ||
} | ||
} | ||
return year; | ||
}, | ||
/** | ||
* Sets this GregorianCalendar to the date given by the date specifiers - weekYear, | ||
* weekOfYear, and dayOfWeek. weekOfYear follows the WEEK_OF_YEAR numbering. | ||
* The dayOfWeek value must be one of the DAY_OF_WEEK values: SUNDAY to SATURDAY. | ||
* | ||
* @param weekYear the week year | ||
* @param weekOfYear the week number based on weekYear | ||
* @param dayOfWeek the day of week value | ||
*/ | ||
setWeekDate: function (weekYear, weekOfYear, dayOfWeek) { | ||
if (dayOfWeek < GregorianCalendar.SUNDAY || dayOfWeek > GregorianCalendar.SATURDAY) { | ||
throw new Error('invalid dayOfWeek: ' + dayOfWeek); | ||
} | ||
var fields = this.fields; | ||
// To avoid changing the time of day fields by date | ||
// calculations, use a clone with the GMT time zone. | ||
var gc = this.clone(); | ||
gc.clear(); | ||
gc.setTimezoneOffset(0); | ||
gc.set(YEAR, weekYear); | ||
gc.set(WEEK_OF_YEAR, 1); | ||
gc.set(DAY_OF_WEEK, this.getFirstDayOfWeek()); | ||
var days = dayOfWeek - this.getFirstDayOfWeek(); | ||
if (days < 0) { | ||
days += 7; | ||
} | ||
days += 7 * (weekOfYear - 1); | ||
if (days !== 0) { | ||
gc.add(DAY_OF_YEAR, days); | ||
} else { | ||
gc.complete(); | ||
} | ||
fields[YEAR] = gc.get(YEAR); | ||
fields[MONTH] = gc.get(MONTH); | ||
fields[DAY_OF_MONTH] = gc.get(DAY_OF_MONTH); | ||
this.complete(); | ||
}, | ||
/** | ||
* Creates and returns a copy of this object. | ||
* @returns {Date.Gregorian} | ||
*/ | ||
clone: function () { | ||
if (this.time === undefined) { | ||
this.computeTime(); | ||
} | ||
var cal = new GregorianCalendar(this.timezoneOffset, this.locale); | ||
cal.setTime(this.time); | ||
return cal; | ||
}, | ||
/** | ||
* Compares this GregorianCalendar to the specified Object. | ||
* The result is true if and only if the argument is a GregorianCalendar object | ||
* that represents the same time value (millisecond offset from the Epoch) | ||
* under the same Calendar parameters and Gregorian change date as this object. | ||
* @param {Date.Gregorian} obj the object to compare with. | ||
* @returns {boolean} true if this object is equal to obj; false otherwise. | ||
*/ | ||
equals: function (obj) { | ||
return this.getTime() === obj.getTime() && | ||
this.firstDayOfWeek === obj.firstDayOfWeek && | ||
this.timezoneOffset === obj.timezoneOffset && | ||
this.minimalDaysInFirstWeek === obj.minimalDaysInFirstWeek; | ||
}, | ||
/** | ||
* Compares this GregorianCalendar to the specified Object. | ||
* The result is true if and only if the argument is a GregorianCalendar object | ||
* that represents the same time value (millisecond offset from the Epoch) | ||
* under the same Calendar parameters and Gregorian change date as this object. | ||
* @param {Date.Gregorian} obj the object to compare with. | ||
* @returns {boolean} true if this object is equal to obj; false otherwise. | ||
*/ | ||
equals: function (obj) { | ||
return this.getTime() === obj.getTime() && | ||
this.firstDayOfWeek === obj.firstDayOfWeek && | ||
this.timezoneOffset === obj.timezoneOffset && | ||
this.minimalDaysInFirstWeek === obj.minimalDaysInFirstWeek; | ||
}, | ||
/** | ||
* Sets all the calendar field values or specified field and the time value | ||
* (millisecond offset from the Epoch) of this Calendar undefined. | ||
* This means that isSet() will return false for all the calendar fields, | ||
* and the date and time calculations will treat the fields as if they had never been set. | ||
* @param [field] the calendar field to be cleared. | ||
*/ | ||
clear: function (field) { | ||
if (field === undefined) { | ||
this.field = []; | ||
} else { | ||
this.fields[field] = undefined; | ||
} | ||
this.time = undefined; | ||
this.fieldsComputed = false; | ||
/** | ||
* Sets all the calendar field values or specified field and the time value | ||
* (millisecond offset from the Epoch) of this Calendar undefined. | ||
* This means that isSet() will return false for all the calendar fields, | ||
* and the date and time calculations will treat the fields as if they had never been set. | ||
* @param [field] the calendar field to be cleared. | ||
*/ | ||
clear: function (field) { | ||
if (field === undefined) { | ||
this.field = []; | ||
} else { | ||
this.fields[field] = undefined; | ||
} | ||
this.time = undefined; | ||
this.fieldsComputed = false; | ||
} | ||
}; | ||
@@ -1285,23 +1280,23 @@ | ||
Utils.each(fields, function (f, index) { | ||
if (f) { | ||
GregorianCalendarProto['get' + f] = function () { | ||
return this.get(index); | ||
}; | ||
if (f) { | ||
GregorianCalendarProto['get' + f] = function () { | ||
return this.get(index); | ||
}; | ||
GregorianCalendarProto['isSet' + f] = function () { | ||
return this.isSet(index); | ||
}; | ||
GregorianCalendarProto['isSet' + f] = function () { | ||
return this.isSet(index); | ||
}; | ||
GregorianCalendarProto['set' + f] = function (v) { | ||
return this.set(index, v); | ||
}; | ||
GregorianCalendarProto['set' + f] = function (v) { | ||
return this.set(index, v); | ||
}; | ||
GregorianCalendarProto['add' + f] = function (v) { | ||
return this.add(index, v); | ||
}; | ||
GregorianCalendarProto['add' + f] = function (v) { | ||
return this.add(index, v); | ||
}; | ||
GregorianCalendarProto['roll' + f] = function (v) { | ||
return this.roll(index, v); | ||
}; | ||
} | ||
GregorianCalendarProto['roll' + f] = function (v) { | ||
return this.roll(index, v); | ||
}; | ||
} | ||
}); | ||
@@ -1312,34 +1307,34 @@ | ||
function adjustDayOfMonth(self) { | ||
var fields = self.fields; | ||
var year = fields[YEAR]; | ||
var month = fields[MONTH]; | ||
var monthLen = getMonthLength(year, month); | ||
var dayOfMonth = fields[DAY_OF_MONTH]; | ||
if (dayOfMonth > monthLen) { | ||
self.set(DAY_OF_MONTH, monthLen); | ||
} | ||
var fields = self.fields; | ||
var year = fields[YEAR]; | ||
var month = fields[MONTH]; | ||
var monthLen = getMonthLength(year, month); | ||
var dayOfMonth = fields[DAY_OF_MONTH]; | ||
if (dayOfMonth > monthLen) { | ||
self.set(DAY_OF_MONTH, monthLen); | ||
} | ||
} | ||
function getMonthLength(year, month) { | ||
return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month]; | ||
return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month]; | ||
} | ||
function getYearLength(year) { | ||
return isLeapYear(year) ? 366 : 365; | ||
return isLeapYear(year) ? 366 : 365; | ||
} | ||
function getWeekNumber(self, fixedDay1, fixedDate) { | ||
var fixedDay1st = getDayOfWeekDateOnOrBefore(fixedDay1 + 6, self.firstDayOfWeek); | ||
var nDays = (fixedDay1st - fixedDay1); | ||
if (nDays >= self.minimalDaysInFirstWeek) { | ||
fixedDay1st -= 7; | ||
} | ||
var normalizedDayOfPeriod = (fixedDate - fixedDay1st); | ||
return floorDivide(normalizedDayOfPeriod / 7) + 1; | ||
var fixedDay1st = getDayOfWeekDateOnOrBefore(fixedDay1 + 6, self.firstDayOfWeek); | ||
var nDays = (fixedDay1st - fixedDay1); | ||
if (nDays >= self.minimalDaysInFirstWeek) { | ||
fixedDay1st -= 7; | ||
} | ||
var normalizedDayOfPeriod = (fixedDate - fixedDay1st); | ||
return floorDivide(normalizedDayOfPeriod / 7) + 1; | ||
} | ||
function getDayOfWeekDateOnOrBefore(fixedDate, dayOfWeek) { | ||
// 1.1.1 is monday | ||
// one week has 7 days | ||
return fixedDate - mod(fixedDate - dayOfWeek, 7); | ||
// 1.1.1 is monday | ||
// one week has 7 days | ||
return fixedDate - mod(fixedDate - dayOfWeek, 7); | ||
} | ||
@@ -1350,6 +1345,2 @@ | ||
module.exports = GregorianCalendar; | ||
GregorianCalendar.version = '@VERSION@'; | ||
GregorianCalendar.locales = { | ||
'default': defaultLocale | ||
}; | ||
/* | ||
@@ -1356,0 +1347,0 @@ http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html |
{ | ||
"name": "gregorian-calendar", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"author": "yiminghe <yiminghe@gmail.com>", | ||
@@ -44,2 +44,3 @@ "engines": { | ||
"gulp-util": "^3.0.0", | ||
"gulp-wrapper": "^0.1.5", | ||
"jshint": "^2.5.5", | ||
@@ -49,6 +50,9 @@ "jshint-stylish": "^0.4.0", | ||
"koa-body": "^0.2.0", | ||
"koa-modularize": "^3.0.3", | ||
"koa-mount": "^1.3.0", | ||
"koa-node-jscover": "^1.1.1", | ||
"koa-serve-index": "^1.0.1", | ||
"koa-static": "^1.4.7", | ||
"mocha": "^1.21.4", | ||
"modulex": "^1.7.4", | ||
"node-jscover": "^0.6.8", | ||
@@ -58,3 +62,2 @@ "node-jscover-coveralls": "^1.0.10", | ||
"precommit-hook": "^1.0.7", | ||
"gulp-wrapper": "^0.1.5", | ||
"through2": "^0.6.2" | ||
@@ -65,2 +68,5 @@ }, | ||
], | ||
"config": { | ||
"port": 8011 | ||
}, | ||
"scripts": { | ||
@@ -71,6 +77,5 @@ "start": "node --harmony server", | ||
"lint": "gulp lint", | ||
"browser-test": "mocha-phantomjs http://localhost:8025/tests/browser/runner.html", | ||
"browser-test-build": "mocha-phantomjs http://localhost:8025/tests/browser/runner.html?build", | ||
"browser-test-cover": "mocha-phantomjs -R node_modules/node-jscover/lib/reporters/mocha/console http://localhost:8025/tests/browser/runner.html?coverage" | ||
"browser-test": "mocha-phantomjs http://localhost:$npm_package_config_port/tests/browser/runner.html", | ||
"browser-test-cover": "mocha-phantomjs -R node_modules/node-jscover/lib/reporters/mocha/console http://localhost:$npm_package_config_port/tests/browser/runner.html?coverage" | ||
} | ||
} |
@@ -18,5 +18,5 @@ # gregorian-calendar | ||
var GregorianCalendar = require('gregorian-calendar'); | ||
var date = new GregorianCalendar(); | ||
var date = new GregorianCalendar(require('gregorian-calendar/lib/locale/zh-cn')); // defaults to en-us | ||
date.setTime(+new Date()); | ||
console.log(date.getDayOfWeek()); | ||
``` |
@@ -1,65 +0,45 @@ | ||
#!/usr/bin/env node --harmony | ||
var gutil = require('gulp-util'); | ||
var koa = require('koa'); | ||
var koaBody = require('koa-body'); | ||
var serve = require('koa-static'); | ||
var app = require('koa')(); | ||
var path = require('path'); | ||
var jscoverHandler = require('node-jscover-handler/lib/koa'); | ||
var jscoverCoveralls = require('node-jscover-coveralls/lib/koa'); | ||
var serve = require('koa-static'); | ||
var fs = require('fs'); | ||
var app = koa(); | ||
var root = path.resolve(__dirname, './'); | ||
var serveIndex = require('koa-serve-index'); | ||
var modularize = require('koa-modularize'); | ||
var mount = require('koa-mount'); | ||
var cwd = process.cwd(); | ||
var serveIndex = require('koa-serve-index'); | ||
var cwd = __dirname; | ||
var koaBody = require('koa-body'); | ||
var jscoverHandler = require('koa-node-jscover'); | ||
var jscoverCoveralls = require('node-jscover-coveralls/lib/koa'); | ||
function *modularize(next) { | ||
var req = this.request; | ||
var res = this.response; | ||
if (path.extname(this.path) !== '.js') { | ||
yield *next; | ||
return; | ||
} | ||
var filePath = path.resolve(cwd, req.originalUrl.substring(1)).replace(/-coverage\.js/, '.js'); | ||
var stats = fs.statSync(filePath); | ||
if (!stats.isFile()) { | ||
yield *next; | ||
return; | ||
} | ||
var code = fs.readFileSync(filePath, 'utf-8'); | ||
code = 'modulex.add(function(require,exports,module){' + code + '});'; | ||
if (req.path.indexOf('-coverage.js') !== -1) { | ||
req.nodeJsCoverCode = code; | ||
yield *next; | ||
return; | ||
} | ||
res.set('content-type', 'application/javascript;charset=utf-8'); | ||
this.body = code; | ||
} | ||
// parse application/x-www-form-urlencoded | ||
app.use(koaBody({formidable: {uploadDir: __dirname}, multipart: true})); | ||
app.use(function *(next) { | ||
if (path.extname(this.path) === '.jss') { | ||
var func = require(path.resolve(__dirname, this.path.substring(1))).call(this); | ||
yield *func; | ||
} else { | ||
yield *next; | ||
} | ||
}); | ||
app.use(mount('/lib/', modularize)); | ||
app.use(mount('/tests/browser/specs/', modularize)); | ||
app.use(koaBody()); | ||
app.use(jscoverHandler({ | ||
onlyLoad: function () { | ||
return 1; | ||
}, | ||
next: function () { | ||
return 1; | ||
} | ||
})); | ||
app.use(jscoverHandler({ | ||
jscover: require('node-jscover'), | ||
next: function () { | ||
return 1; | ||
} | ||
})); | ||
app.use(mount('/', modularize(root, { | ||
nowrap: function () { | ||
return this.url.indexOf('nowrap') != -1 || this.url.indexOf('/node_modules/node-jscover/') != -1; | ||
} | ||
}))); | ||
app.use(jscoverCoveralls()); | ||
app.use(jscoverHandler()); | ||
app.use(serveIndex(cwd, { | ||
hidden: true, | ||
view: 'details' | ||
app.use(serveIndex(root, { | ||
hidden: true, | ||
view: 'details' | ||
})); | ||
app.use(serve(cwd, { | ||
hidden: true | ||
app.use(serve(root, { | ||
hidden: true | ||
})); | ||
var port = process.env.PORT || parseInt('8025', 10); | ||
var port = process.env.npm_package_config_port; | ||
app.listen(port); | ||
app.listen(8022); | ||
gutil.log('server start at ' + port); | ||
console.log('listen at ' + port); |
@@ -6,3 +6,3 @@ /** | ||
var GregorianCalendar = require('gregorian-calendar'); | ||
var GregorianCalendar = require('../../../'); | ||
var ONE_SECOND = 1000; | ||
@@ -19,4 +19,5 @@ var ONE_MINUTE = 60 * ONE_SECOND; | ||
beforeEach(function () { | ||
gregorianCalendar = new GregorianCalendar(2013, | ||
GregorianCalendar.JUNE, 8, 18, 0, 0, 0); | ||
gregorianCalendar = new GregorianCalendar(require('../../../lib/locale/zh-cn')); | ||
gregorianCalendar.set(2013, | ||
GregorianCalendar.JUNE, 8, 18, 0, 0, 0); | ||
}); | ||
@@ -23,0 +24,0 @@ |
@@ -6,3 +6,3 @@ /** | ||
var GregorianCalendar = require('gregorian-calendar'); | ||
var GregorianCalendar = require('../../../'); | ||
var ONE_SECOND = 1000; | ||
@@ -9,0 +9,0 @@ var ONE_MINUTE = 60 * ONE_SECOND; |
var GregorianCalendar = require('../../'); | ||
var date = new GregorianCalendar(GregorianCalendar.locales['zh-cn']); | ||
var date = new GregorianCalendar(require('../../lib/locale/zh-cn')); | ||
date.setTime(+new Date()); | ||
console.log(date.getDayOfWeek()); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
2
68492
34
20
1781