time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

iso_weeks_in_year.js (2173B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isDateObject = require( '@stdlib/assert/is-date-object' );
     24 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     25 var floor = require( '@stdlib/math/base/special/floor' );
     26 
     27 
     28 // VARIABLES //
     29 
     30 var SHORT_YEAR = 52;
     31 var LONG_YEAR = 53;
     32 
     33 
     34 // FUNCTIONS //
     35 
     36 /**
     37 * Formula for determining if a year is "long" or "short".
     38 *
     39 * @private
     40 * @param {integer} yr - year
     41 * @returns {integer} result
     42 */
     43 function p( yr ) {
     44 	var v = yr + floor( yr/4 ) - floor( yr/100 ) + floor( yr/400 );
     45 	return v % 7;
     46 }
     47 
     48 
     49 // MAIN //
     50 
     51 /**
     52 * Returns the number of ISO weeks in a year.
     53 *
     54 * @param {(integer|Date)} value - year or `Date` object
     55 * @throws {TypeError} must provide either an integer or a `Date` object
     56 * @returns {integer} number of ISO weeks in a year
     57 *
     58 * @example
     59 * var num = isoWeeksInYear();
     60 * // returns <number>
     61 *
     62 * @example
     63 * var num = isoWeeksInYear( 2015 );
     64 * // returns 53
     65 *
     66 * @example
     67 * var num = isoWeeksInYear( 2017 );
     68 * // returns 52
     69 */
     70 function isoWeeksInYear( value ) {
     71 	var yr;
     72 	if ( arguments.length ) {
     73 		if ( isDateObject( value ) ) {
     74 			yr = value.getFullYear();
     75 		} else if ( isInteger( value ) ) {
     76 			yr = value;
     77 		} else {
     78 			throw new TypeError( 'invalid argument. Must provide either an integer or a `Date` object. Value: `'+value+'`.' );
     79 		}
     80 	} else {
     81 		// Note: cannot cache as application could cross over into a new year:
     82 		yr = ( new Date() ).getFullYear();
     83 	}
     84 	if ( p( yr ) === 4 || p( yr-1 ) === 3 ) {
     85 		return LONG_YEAR;
     86 	}
     87 	return SHORT_YEAR;
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = isoWeeksInYear;