Skip to content

Commit

Permalink
Add getUnixTime (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl authored and kossnocorp committed Sep 25, 2018
1 parent ebccdd5 commit e48cdfb
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/esm/fp/index.js
Expand Up @@ -130,6 +130,8 @@ export {default as getSeconds} from './getSeconds/index.js'
export {default as getSecondsWithOptions} from './getSecondsWithOptions/index.js'
export {default as getTime} from './getTime/index.js'
export {default as getTimeWithOptions} from './getTimeWithOptions/index.js'
export {default as getUnixTime} from './getUnixTime/index.js'
export {default as getUnixTimeWithOptions} from './getUnixTimeWithOptions/index.js'
export {default as getWeek} from './getWeek/index.js'
export {default as getWeekOfMonth} from './getWeekOfMonth/index.js'
export {default as getWeekOfMonthWithOptions} from './getWeekOfMonthWithOptions/index.js'
Expand Down
1 change: 1 addition & 0 deletions src/esm/index.js
Expand Up @@ -65,6 +65,7 @@ export {default as getOverlappingDaysInIntervals} from './getOverlappingDaysInIn
export {default as getQuarter} from './getQuarter/index.js'
export {default as getSeconds} from './getSeconds/index.js'
export {default as getTime} from './getTime/index.js'
export {default as getUnixTime} from './getUnixTime/index.js'
export {default as getWeek} from './getWeek/index.js'
export {default as getWeekOfMonth} from './getWeekOfMonth/index.js'
export {default as getWeekYear} from './getWeekYear/index.js'
Expand Down
4 changes: 4 additions & 0 deletions src/fp/getUnixTime/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import {getUnixTime} from 'date-fns/fp'
export = getUnixTime
8 changes: 8 additions & 0 deletions src/fp/getUnixTime/index.js
@@ -0,0 +1,8 @@
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it.

import fn from '../../getUnixTime/index.js'
import convertToFP from '../_lib/convertToFP/index.js'

var getUnixTime = convertToFP(fn, 1)

export default getUnixTime
51 changes: 51 additions & 0 deletions src/fp/getUnixTime/index.js.flow
@@ -0,0 +1,51 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | string | number,
end: Date | string | number
}

type Options = {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
roundingMethod?: 'floor' | 'ceil' | 'round'
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

type CurriedFn1<A, R> = <A>(a: A) => R

declare module.exports: CurriedFn1<Date | string | number, number>
4 changes: 4 additions & 0 deletions src/fp/getUnixTimeWithOptions/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import {getUnixTimeWithOptions} from 'date-fns/fp'
export = getUnixTimeWithOptions
8 changes: 8 additions & 0 deletions src/fp/getUnixTimeWithOptions/index.js
@@ -0,0 +1,8 @@
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it.

import fn from '../../getUnixTime/index.js'
import convertToFP from '../_lib/convertToFP/index.js'

var getUnixTimeWithOptions = convertToFP(fn, 2)

export default getUnixTimeWithOptions
54 changes: 54 additions & 0 deletions src/fp/getUnixTimeWithOptions/index.js.flow
@@ -0,0 +1,54 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | string | number,
end: Date | string | number
}

type Options = {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
roundingMethod?: 'floor' | 'ceil' | 'round'
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

type CurriedFn1<A, R> = <A>(a: A) => R

type CurriedFn2<A, B, R> = <A>(a: A) => CurriedFn1<B, R>
| <A, B>(a: A, b: B) => R

declare module.exports: CurriedFn2<Options, Date | string | number, number>
2 changes: 2 additions & 0 deletions src/fp/index.js
Expand Up @@ -131,6 +131,8 @@ module.exports = {
getSecondsWithOptions: require('./getSecondsWithOptions/index.js'),
getTime: require('./getTime/index.js'),
getTimeWithOptions: require('./getTimeWithOptions/index.js'),
getUnixTime: require('./getUnixTime/index.js'),
getUnixTimeWithOptions: require('./getUnixTimeWithOptions/index.js'),
getWeek: require('./getWeek/index.js'),
getWeekOfMonth: require('./getWeekOfMonth/index.js'),
getWeekOfMonthWithOptions: require('./getWeekOfMonthWithOptions/index.js'),
Expand Down
2 changes: 2 additions & 0 deletions src/fp/index.js.flow
Expand Up @@ -191,6 +191,8 @@ declare module.exports: {
getSecondsWithOptions: CurriedFn2<Options, Date | string | number, number>,
getTime: CurriedFn1<Date | string | number, number>,
getTimeWithOptions: CurriedFn2<Options, Date | string | number, number>,
getUnixTime: CurriedFn1<Date | string | number, number>,
getUnixTimeWithOptions: CurriedFn2<Options, Date | string | number, number>,
getWeek: CurriedFn1<Date | string | number, number>,
getWeekOfMonth: CurriedFn1<Date | string | number, number>,
getWeekOfMonthWithOptions: CurriedFn2<Options, Date | string | number, number>,
Expand Down
21 changes: 21 additions & 0 deletions src/getUnixTime/benchmark.js
@@ -0,0 +1,21 @@
// @flow
/* eslint-env mocha */
/* global suite, benchmark */

import getUnixTime from '.'
import moment from 'moment'

suite('getTime', function () {
benchmark('date-fns', function () {
return getUnixTime(this.date)
})

benchmark('Moment.js', function () {
return this.moment.unix()
})
}, {
setup: function () {
this.date = new Date()
this.moment = moment()
}
})
4 changes: 4 additions & 0 deletions src/getUnixTime/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import {getUnixTime} from 'date-fns'
export = getUnixTime
29 changes: 29 additions & 0 deletions src/getUnixTime/index.js
@@ -0,0 +1,29 @@
import getTime from '../getTime/index.js'

/**
* @name getUnixTime
* @category Timestamp Helpers
* @summary Get the seconds timestamp of the given date.
*
* @description
* Get the seconds timestamp of the given date.
*
* @param {Date|String|Number} date - the given date
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
* @returns {Number} the timestamp
* @throws {TypeError} 1 argument required
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
*
* @example
* // Get the timestamp of 29 February 2012 11:45:05:
* var result = getTime(new Date(2012, 1, 29, 11, 45, 5))
* //=> 1330515905
*/
export default function getUnixTime (dirtyDate, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
}

return Math.floor(getTime(dirtyDate, dirtyOptions) / 1000)
}
52 changes: 52 additions & 0 deletions src/getUnixTime/index.js.flow
@@ -0,0 +1,52 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | string | number,
end: Date | string | number
}

type Options = {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
roundingMethod?: 'floor' | 'ceil' | 'round'
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

declare module.exports: (
date: Date | string | number,
options?: Options
) => number
41 changes: 41 additions & 0 deletions src/getUnixTime/test.js
@@ -0,0 +1,41 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import getUnixTime from '.'

describe('getUnixTime', function () {
it('returns the timestamp of the given date', function () {
var timestamp = 1483228800000
var result = getUnixTime(new Date(timestamp))
assert(result === Math.floor(timestamp / 1000))
})

it('accepts a string', function () {
var timestamp = 1484503736150
var result = getUnixTime(new Date(timestamp).toISOString())
assert(result === Math.floor(timestamp / 1000))
})

it('accepts a timestamp (and returns it unchanged)', function () {
var timestamp = 804643200000
var result = getUnixTime(timestamp)
assert(result === Math.floor(timestamp / 1000))
})

it('returns NaN if the given date is invalid', function () {
var result = getUnixTime(new Date(NaN))
assert(isNaN(result))
})

it('throws `RangeError` if `options.additionalDigits` is not convertable to 0, 1, 2 or undefined', function () {
var timestamp = 1483228800000
// $ExpectedMistake
var block = getUnixTime.bind(null, new Date(timestamp), {additionalDigits: NaN})
assert.throws(block, RangeError)
})

it('throws TypeError exception if passed less than 1 argument', function () {
assert.throws(getUnixTime.bind(null), TypeError)
})
})
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -66,6 +66,7 @@ module.exports = {
getQuarter: require('./getQuarter/index.js'),
getSeconds: require('./getSeconds/index.js'),
getTime: require('./getTime/index.js'),
getUnixTime: require('./getUnixTime/index.js'),
getWeek: require('./getWeek/index.js'),
getWeekOfMonth: require('./getWeekOfMonth/index.js'),
getWeekYear: require('./getWeekYear/index.js'),
Expand Down
5 changes: 5 additions & 0 deletions src/index.js.flow
Expand Up @@ -409,6 +409,11 @@ declare module.exports: {
options?: Options
) => number,

getUnixTime: (
date: Date | string | number,
options?: Options
) => number,

getWeek: (
date: Date | string | number,
options?: Options
Expand Down

0 comments on commit e48cdfb

Please sign in to comment.