time-to-botec

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

minutes_in_month.js (3480B)


      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 lowercase = require( '@stdlib/string/lowercase' );
     27 var isLeapYear = require( '@stdlib/assert/is-leap-year' );
     28 var MINUTES_IN_DAY = require( '@stdlib/constants/time/minutes-in-day' );
     29 var MINUTES_IN_MONTH = require( './minutes.json' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns the number of minutes in a month.
     36 *
     37 * @param {(string|Date|integer)} [month] - month
     38 * @param {integer} [year] - year
     39 * @throws {TypeError} first argument must be either a string, integer, or `Date` object
     40 * @throws {Error} must provide a recognized month
     41 * @throws {RangeError} an integer month argument must be on the interval `[1,12]`
     42 * @throws {TypeError} second argument must be an integer
     43 * @returns {integer} minutes in a month
     44 *
     45 * @example
     46 * var num = minutesInMonth();
     47 * // returns <number>
     48 *
     49 * @example
     50 * var num = minutesInMonth( 2 );
     51 * // returns <number>
     52 *
     53 * @example
     54 * var num = minutesInMonth( 2, 2016 );
     55 * // returns 41760
     56 *
     57 * @example
     58 * var num = minutesInMonth( 2, 2017 );
     59 * // returns 40320
     60 */
     61 function minutesInMonth( month, year ) {
     62 	var mins;
     63 	var mon;
     64 	var yr;
     65 	var d;
     66 	if ( arguments.length === 0 ) {
     67 		// Note: cannot cache as application may cross over into a new year:
     68 		d = new Date();
     69 		mon = d.getMonth() + 1; // zero-based
     70 		yr = d.getFullYear();
     71 	} else if ( arguments.length === 1 ) {
     72 		if ( isDateObject( month ) ) {
     73 			d = month;
     74 			mon = d.getMonth() + 1; // zero-based
     75 			yr = d.getFullYear();
     76 		} else if ( isString( month ) || isInteger( month ) ) {
     77 			// Note: cannot cache as application may cross over into a new year:
     78 			yr = ( new Date() ).getFullYear();
     79 			mon = month;
     80 		} else {
     81 			throw new TypeError( 'invalid argument. First argument must be either a string, integer, or `Date` object. Value: `'+month+'`.' );
     82 		}
     83 	} else {
     84 		if ( !isString( month ) && !isInteger( month ) ) {
     85 			throw new TypeError( 'invalid argument. First argument must be either a string or integer. Value: `'+month+'`.' );
     86 		}
     87 		if ( !isInteger( year ) ) {
     88 			throw new TypeError( 'invalid argument. Second argument must be an integer. Value: `'+year+'`.' );
     89 		}
     90 		mon = month;
     91 		yr = year;
     92 	}
     93 	if ( isInteger( mon ) && (mon < 1 || mon > 12) ) {
     94 		throw new RangeError( 'invalid argument. An integer month value must be on the interval `[1,12]`. Value: `'+mon+'`.' );
     95 	}
     96 	mon = lowercase( mon.toString() );
     97 	mins = MINUTES_IN_MONTH[ mon ];
     98 	if ( mins === void 0 ) {
     99 		throw new Error( 'invalid argument. Must provide a recognized month. Value: `'+mon+'`.' );
    100 	}
    101 	// Check if February during a leap year...
    102 	if ( mins === 40320 && isLeapYear( yr ) ) {
    103 		mins += MINUTES_IN_DAY;
    104 	}
    105 	return mins;
    106 }
    107 
    108 
    109 // EXPORTS //
    110 
    111 module.exports = minutesInMonth;