time-to-botec

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

day_of_quarter.js (3279B)


      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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     25 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     26 var dayOfYear = require( './../../day-of-year' );
     27 var quarterOfYear = require( './../../quarter-of-year' );
     28 var isLeapYear = require( '@stdlib/assert/is-leap-year' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 // Quarter days:
     34 var NON_LEAP_YEAR = [ 0, 90, 181, 273 ];
     35 var LEAP_YEAR = [ 0, 91, 182, 274 ];
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Returns the day of the quarter.
     42 *
     43 * @param {(string|integer|Date)} [month] - month (or `Date`)
     44 * @param {integer} [day] - day
     45 * @param {integer} [year] - year
     46 * @throws {TypeError} first argument must be either a string, integer, or `Date` object
     47 * @throws {Error} must provide a recognized month
     48 * @throws {RangeError} an integer month argument must be on the interval `[1,12]`
     49 * @throws {TypeError} second argument must be an integer
     50 * @throws {RangeError} second argument must be greater than `0` and less than or equal to the maximum number of days in a month
     51 * @throws {TypeError} third argument must be an integer
     52 * @returns {integer} day of the quarter
     53 *
     54 * @example
     55 * var day = dayOfQuarter();
     56 * // returns <number>
     57 *
     58 * day = dayOfQuarter( new Date() );
     59 * // returns <number>
     60 *
     61 * day = dayOfQuarter( 12, 31, 2017 );
     62 * // returns 92
     63 */
     64 function dayOfQuarter( month, day, year ) {
     65 	var date;
     66 	var yr;
     67 	var q;
     68 	var d;
     69 	if ( arguments.length === 0 ) {
     70 		// Note: cannot cache as application may cross over into a new year:
     71 		date = new Date();
     72 		q = quarterOfYear( date );
     73 		d = dayOfYear( date );
     74 		yr = date.getFullYear();
     75 	} else if ( arguments.length === 1 ) {
     76 		if ( isDateObject( month ) ) {
     77 			date = month;
     78 			q = quarterOfYear( date );
     79 			d = dayOfYear( date );
     80 			yr = date.getFullYear();
     81 		} else {
     82 			throw new TypeError( 'invalid argument. If only providing a single argument, must provide a `Date` object. Value: `'+month+'`.' );
     83 		}
     84 	} else {
     85 		if ( !isString( month ) && !isInteger( month ) ) {
     86 			throw new TypeError( 'invalid argument. First argument must be either a string or integer. Value: `'+month+'`.' );
     87 		}
     88 		if ( !isInteger( day ) ) {
     89 			throw new TypeError( 'invalid argument. Second argument must be an integer. Value: `'+day+'`.' );
     90 		}
     91 		if ( !isInteger( year ) ) {
     92 			throw new TypeError( 'invalid argument. Third argument must be an integer. Value: `'+year+'`.' );
     93 		}
     94 		q = quarterOfYear( month );
     95 		d = dayOfYear( month, day, year );
     96 		yr = year;
     97 	}
     98 	if ( isLeapYear( yr ) ) {
     99 		return d - LEAP_YEAR[ q-1 ];
    100 	}
    101 	return d - NON_LEAP_YEAR[ q-1 ];
    102 }
    103 
    104 
    105 // EXPORTS //
    106 
    107 module.exports = dayOfQuarter;