@beauwest/business-date-parser
Advanced tools
Comparing version 1.0.16 to 1.0.17
# Change Log | ||
## Release 1.0.17 | ||
- Adds support for additional machine-style dates. | ||
## Release 1.0.16 | ||
- Improves support for time-only date & time parsing | ||
- Simplify tests | ||
- Improves support for time-only date & time parsing. | ||
- Simplify tests. | ||
@@ -32,3 +35,3 @@ ## Release 1.0.15 | ||
- Always return the original input if it's falsy and not a timestamp value of 0. | ||
- Parse as a date part, in the date & time parsing, if there is only one part and it's likely a date because it contains a "-" or a "/". | ||
- Parse as a date part, in the date & time parsing, if there is only one part, and it's likely a date because it contains a "-" or a "/". | ||
@@ -39,3 +42,3 @@ ## Release 1.0.7 | ||
## Release 1.0.6 | ||
- Added a `rules` option to all parse functions that allows users to specify their own parsing rules. | ||
- Added a `rules` option to all parse functions which allows users to specify their own parsing rules. | ||
- Improved the documentation. | ||
@@ -42,0 +45,0 @@ |
24
index.js
@@ -39,2 +39,18 @@ function calculateFullYear(input) { | ||
function isLikelyISOFormat(input) { | ||
// We only want this to match full formats that include full date, separator, and something after the time. | ||
// YYYY-MM-DDTHH:MM:SSZ | ||
if (input.length >= 20 && isLikelyDateFormat(input)) { | ||
// Check for the "T" delimiter | ||
if (/\d{2}T\d{2}/.test(input)) { | ||
return true; | ||
} | ||
// See if it ends in a timezone, or offset. | ||
if (/([A-Z]{1,5})|([+-]\d{1,4})$/.test(input)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function adjustForBusinessHours(hour) { | ||
@@ -106,2 +122,7 @@ hour = parseInt(hour, 10); | ||
if (isLikelyISOFormat(input)) { | ||
datePart = input; | ||
timePart = null; | ||
} | ||
if (options.preferTime && !isLikelyDateFormat(input)) { | ||
@@ -120,3 +141,2 @@ const likelyTime = parseTime(input, options); | ||
if (isDate(likelyTime)) { | ||
datePart = options.defaultDate || new Date(); | ||
timePart = likelyTime; | ||
@@ -131,2 +151,4 @@ } | ||
if (input === '2021-11-21 11:19:16+00') console.log(parsedDate, parsedTime, new Date().getTimezoneOffset()); | ||
return parsedDate; | ||
@@ -133,0 +155,0 @@ } |
{ | ||
"name": "@beauwest/business-date-parser", | ||
"version": "1.0.16", | ||
"version": "1.0.17", | ||
"description": "An opinionated, zero-dependency, fast user input parser for date & times.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -48,2 +48,11 @@ import {parseDate, parseTime, parseDateAndTime} from '../index.js'; | ||
function getSystemOffset(addMinutes = 0) { | ||
// Use a fixed date for the offset, so we get a non-varied offset when dealing with DST | ||
const offset = new Date('2016-05-01T03:24:00Z').getTimezoneOffset() + addMinutes; | ||
return { | ||
hours: Math.floor(offset / 60), | ||
minutes: (offset % 60) | ||
}; | ||
} | ||
test('Blank Date', t => { | ||
@@ -364,20 +373,14 @@ const blankValue = parseDate(''); | ||
test('datetime: PostgreSQL ISO 8601 +00', t => { | ||
const offset = new Date().getTimezoneOffset(); | ||
const offsetHours = Math.floor(offset / 60); | ||
const offsetMinutes = (offset % 60); | ||
expectDateAndTime(t, '2021-11-21 11:19:16+00', 2021, 11, 21, 11 - offsetHours, 19 - offsetMinutes, 16, 0); | ||
const offset = getSystemOffset(); | ||
expectDateAndTime(t, '2016-05-01 11:19:16+00', 2016, 5, 1, 11 - offset.hours, 19 - offset.minutes, 16, 0); | ||
}); | ||
test('datetime: PostgreSQL ISO 8601 +0200', t => { | ||
const offset = new Date().getTimezoneOffset(); | ||
const offsetHours = Math.floor(offset / 60) + 2; | ||
const offsetMinutes = (offset % 60); | ||
expectDateAndTime(t, '2021-11-21 11:19:16+0200', 2021, 11, 21, 11 - offsetHours, 19 - offsetMinutes, 16, 0); | ||
const offset = getSystemOffset(120); | ||
expectDateAndTime(t, '2016-05-01 11:19:16+0200', 2016, 5, 1, 11 - offset.hours, 19 - offset.minutes, 16, 0); | ||
}); | ||
test('datetime: PostgreSQL ISO 8601 -0200', t => { | ||
const offset = new Date().getTimezoneOffset(); | ||
const offsetHours = Math.floor(offset / 60) - 2; | ||
const offsetMinutes = (offset % 60); | ||
expectDateAndTime(t, '2021-11-21 11:19:16-0200', 2021, 11, 21, 11 - offsetHours, 19 - offsetMinutes, 16, 0); | ||
const offset = getSystemOffset(-120); | ||
expectDateAndTime(t, '2016-05-01 11:19:16-0200', 2016, 5, 1, 11 - offset.hours, 19 - offset.minutes, 16, 0); | ||
}); | ||
@@ -403,2 +406,17 @@ | ||
expectDateAndTime(t, '3pm', now.getFullYear(), now.getMonth() + 1, now.getDate(), 15, 0, 0, 0, {preferTime: true}); | ||
}); | ||
test('datetime: 2022-04-01 13:00:00.000 PDT', t => { | ||
const offset = getSystemOffset(120); | ||
expectDateAndTime(t, t.title, 2022, 4, 1, 8 + offset.hours, 0 + offset.minutes, 0, 0, {preferTime: true}); | ||
}); | ||
test('datetime: 2022-04-01 12:00:00.000 Z', t => { | ||
const offset = getSystemOffset(); | ||
expectDateAndTime(t, t.title, 2022, 4, 1, 12 - offset.hours, 0 - offset.minutes, 0, 0, {preferTime: true}); | ||
}); | ||
test('datetime: 2022-04-01T19:00:00.000Z', t => { | ||
const offset = getSystemOffset(); | ||
expectDateAndTime(t, t.title, 2022, 4, 1, 19 - offset.hours, 0 - offset.minutes, 0, 0, {preferTime: true}); | ||
}); |
28616
698