summaryrefslogtreecommitdiff
path: root/date-fns/src/getDaysInYear
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/getDaysInYear')
-rw-r--r--date-fns/src/getDaysInYear/benchmark.js15
-rw-r--r--date-fns/src/getDaysInYear/index.d.ts4
-rw-r--r--date-fns/src/getDaysInYear/index.js.flow52
-rw-r--r--date-fns/src/getDaysInYear/index.ts36
-rw-r--r--date-fns/src/getDaysInYear/test.ts42
5 files changed, 149 insertions, 0 deletions
diff --git a/date-fns/src/getDaysInYear/benchmark.js b/date-fns/src/getDaysInYear/benchmark.js
new file mode 100644
index 0000000..b076dc8
--- /dev/null
+++ b/date-fns/src/getDaysInYear/benchmark.js
@@ -0,0 +1,15 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import getDaysInYear from '.'
+
+suite('getDaysInYear', function () {
+ benchmark('date-fns', function () {
+ return getDaysInYear(this.date)
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ }
+})
diff --git a/date-fns/src/getDaysInYear/index.d.ts b/date-fns/src/getDaysInYear/index.d.ts
new file mode 100644
index 0000000..053d44b
--- /dev/null
+++ b/date-fns/src/getDaysInYear/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { getDaysInYear } from 'date-fns'
+export default getDaysInYear
diff --git a/date-fns/src/getDaysInYear/index.js.flow b/date-fns/src/getDaysInYear/index.js.flow
new file mode 100644
index 0000000..07bb012
--- /dev/null
+++ b/date-fns/src/getDaysInYear/index.js.flow
@@ -0,0 +1,52 @@
+// @flow
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+export type Interval = {
+ start: Date | number,
+ end: Date | number,
+}
+
+export type Locale = {
+ code?: string,
+ formatDistance?: (...args: Array<any>) => any,
+ formatRelative?: (...args: Array<any>) => any,
+ localize?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ formatLong?: {
+ date: (...args: Array<any>) => any,
+ time: (...args: Array<any>) => any,
+ dateTime: (...args: Array<any>) => any,
+ },
+ match?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ options?: {
+ weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
+ firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
+ },
+}
+
+export type Duration = {
+ years?: number,
+ months?: number,
+ weeks?: number,
+ days?: number,
+ hours?: number,
+ minutes?: number,
+ seconds?: number,
+}
+
+export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6
+
+declare module.exports: (date: Date | number) => number
diff --git a/date-fns/src/getDaysInYear/index.ts b/date-fns/src/getDaysInYear/index.ts
new file mode 100644
index 0000000..c374a8f
--- /dev/null
+++ b/date-fns/src/getDaysInYear/index.ts
@@ -0,0 +1,36 @@
+import toDate from '../toDate/index'
+import isLeapYear from '../isLeapYear/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name getDaysInYear
+ * @category Year Helpers
+ * @summary Get the number of days in a year of the given date.
+ *
+ * @description
+ * Get the number of days in a year of the given date.
+ *
+ * ### v2.0.0 breaking changes:
+ *
+ * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
+ *
+ * @param {Date|Number} date - the given date
+ * @returns {Number} the number of days in a year
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // How many days are in 2012?
+ * const result = getDaysInYear(new Date(2012, 0, 1))
+ * //=> 366
+ */
+export default function getDaysInYear(dirtyDate: Date | number): number {
+ requiredArgs(1, arguments)
+
+ const date = toDate(dirtyDate)
+
+ if (String(new Date(date)) === 'Invalid Date') {
+ return NaN
+ }
+
+ return isLeapYear(date) ? 366 : 365
+}
diff --git a/date-fns/src/getDaysInYear/test.ts b/date-fns/src/getDaysInYear/test.ts
new file mode 100644
index 0000000..8cf2918
--- /dev/null
+++ b/date-fns/src/getDaysInYear/test.ts
@@ -0,0 +1,42 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import getDaysInYear from '.'
+
+describe('getDaysInYear', function() {
+ it('returns the number of days in the year of the given date', function() {
+ const result = getDaysInYear(new Date(2014, 6 /* Jul */, 2))
+ assert(result === 365)
+ })
+
+ it('works for a leap year', function() {
+ const result = getDaysInYear(new Date(2012, 6 /* Jul */, 2))
+ assert(result === 366)
+ })
+
+ it('works for the years divisible by 100 but not by 400', function() {
+ const result = getDaysInYear(new Date(2100, 6 /* Jul */, 2))
+ assert(result === 365)
+ })
+
+ it('works for the years divisible by 400', function() {
+ const result = getDaysInYear(new Date(2000, 6 /* Jul */, 2))
+ assert(result === 366)
+ })
+
+ it('accepts a timestamp', function() {
+ const date = new Date(2012, 6 /* Jul */, 2).getTime()
+ const result = getDaysInYear(date)
+ assert(result === 366)
+ })
+
+ it('returns NaN if the given date is invalid', function() {
+ const result = getDaysInYear(new Date(NaN))
+ assert(isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function() {
+ assert.throws(getDaysInYear.bind(null), TypeError)
+ })
+})