Skip to content

Commit

Permalink
fix: set base time to after midday
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 1, 2020
1 parent 8fc2963 commit 830916e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
48 changes: 24 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@
"url": "http://gajus.com"
},
"ava": {
"babel": {
"compileAsTests": [
"test/helpers/**/*"
]
},
"files": [
"test/**/*"
],
"helpers": [
"test/helpers/**/*"
"test/extract-time/**/*"
],
"require": [
"@babel/register"
],
"sources": [
"src/**/*"
]
},
"dependencies": {
"cartesian": "^1.0.1",
"date-fns": "^2.8.0",
"roarr": "^2.14.5"
"date-fns": "^2.9.0",
"roarr": "^2.15.2"
},
"description": "Extracts time from an arbitrary text input.",
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/node": "^7.7.0",
"@babel/plugin-transform-flow-strip-types": "^7.6.3",
"@babel/preset-env": "^7.7.1",
"@babel/register": "^7.7.0",
"ava": "^2.4.0",
"babel-plugin-istanbul": "^5.2.0",
"coveralls": "^3.0.8",
"eslint": "^6.6.0",
"@ava/babel": "^1.0.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
"@babel/node": "^7.8.4",
"@babel/plugin-transform-flow-strip-types": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/register": "^7.8.3",
"ava": "^3.1.0",
"babel-plugin-istanbul": "^6.0.0",
"coveralls": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-canonical": "^18.1.0",
"flow-bin": "^0.112.0",
"flow-bin": "^0.117.0",
"flow-copy-source": "^2.0.9",
"gitdown": "^3.1.2",
"husky": "^3.1.0",
"nyc": "^14.1.1",
"semantic-release": "^15.13.31",
"sinon": "^7.5.0"
"husky": "^4.2.1",
"nyc": "^15.0.0",
"semantic-release": "^17.0.2",
"sinon": "^8.1.1"
},
"engines": {
"node": ">6"
Expand Down
9 changes: 8 additions & 1 deletion src/extractTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export default (input: string, timeNotation: TimeNotationType | null = null): $R

const matches = [];

// Assume that it is past midday.
// This is relevant when parsing civilian times such as '1:30'.
// Without being explicit about the base time being after midday,
// the above time would be interpreted either as '1:30' (military time)
// or '13:30' depending on the time of the day when the data is parsed.
const baseDate = parseDate('12:00', 'HH:mm', new Date());

for (const format of formats) {
const movingChunks = createMovingChunks(words, format.wordCount);

Expand All @@ -41,7 +48,7 @@ export default (input: string, timeNotation: TimeNotationType | null = null): $R

const subject = movingChunk.join(' ');

const date = parseDate(subject, format.dateFnsFormat, new Date());
const date = parseDate(subject, format.dateFnsFormat, baseDate);

if (!isDateValid(date)) {
continue;
Expand Down
33 changes: 31 additions & 2 deletions test/extract-time/extractTime/edge-cases.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
// @flow

import test from 'ava';
import sinon from 'sinon';
import {
parse as parseDate,
} from 'date-fns';
import test, {
afterEach,
beforeEach,
} from 'ava';
import extractTime from '../../../src/extractTime';

let clock;

beforeEach(() => {
clock = sinon.useFakeTimers();
});

afterEach(() => {
clock.restore();
});

test('extracts time from yyyy-MM-ddTHH:mm:ss', (t) => {
t.deepEqual(extractTime('2019-02-12T14:00:00'), [
{
Expand Down Expand Up @@ -31,7 +48,19 @@ test('does not extract time from a concatenation of time-like fragments longer t
t.deepEqual(extractTime('14:00:00:00'), []);
});

test('resolves times using specific time notation (12)', (t) => {
test('resolves times using specific time notation (12) (current time is before midday)', (t) => {
clock.tick(parseDate('2000-06-01 08:00', 'yyyy-MM-dd HH:mm', new Date()).getTime());

t.deepEqual(extractTime('1:30', 12), [
{
time: '13:30',
},
]);
});

test('resolves times using specific time notation (12) (current time is after midday)', (t) => {
clock.tick(parseDate('2000-06-01 20:00', 'yyyy-MM-dd HH:mm', new Date()).getTime());

t.deepEqual(extractTime('1:30', 12), [
{
time: '13:30',
Expand Down

0 comments on commit 830916e

Please sign in to comment.