Socket
Socket
Sign inDemoInstall

@0dep/piso

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @0dep/piso

ISO 8601 interval, date, and duration parser


Version published
Weekly downloads
43
decreased by-81.55%
Maintainers
1
Install size
80.5 kB
Created
Weekly downloads
 

Changelog

Source

[0.1.3] - 2024-04-22

  • getDate(arg) now checks if the argument is a date or a number, if so it will put it into a new Date(arg)

Readme

Source

piso

BuildCoverage Status

ISO 8601 date, duration, and interval parsing package as declared on Wikipedia ISO 8601.

In Spain, piso refers to the whole apartment, whereas in Mexico, it refers only to the floor of your departamento. But the above has nothing to do with this project.

Api

parseInterval(iso8601Interval)

Parse interval from an ISO 8601 interval string.

  • iso8601Interval: string with ISO 8601 interval source

Returns ISOInterval.

import { parseInterval } from '@0dep/piso';

const viableIntervals = [
  '2007-03-01/2007-04-01',
  'P2Y/2007-03-01T13:00:00Z',
  '2007-03-01T13:00:00Z/P2Y',
  'R5/P1Y/2025-05-01T13:00:00Z',
  'R-1/2009-07-01T00:00Z/P1M',
  'R-1/1972-07-01T00:02Z/PT1H3M',
  'R-1/P1M/2024-07-27T00:00Z',
];

for (const i of viableIntervals) {
  console.log({ [i]: parseInterval(i).getExpireAt() });
}

parseDuration(iso8601Duration)

Parse duration from an ISO 8601 duration string.

  • iso8601Duration: string with ISO 8601 duration source

Returns ISODuration.

import { parseDuration } from '@0dep/piso';

const viableDurations = [
  'PT1M5S',
  'PT1M0.5S',
  'PT0.5S',
  'PT0.01S',
  'PT0.001S',
  'PT0.0001S',
  'PT0.5M',
  'PT0.5H',
  'PT1.5H',
  'P0.5D',
  'P1W',
  'P0.5W',
  'P0.5M',
  'P0.5D',
  'P1Y',
  'P1Y2M3W4DT5H6M7S',
  'PT0S',
  'P0D',
];

for (const d of viableDurations) {
  console.log({ [d]: parseDuration(d).getExpireAt() });
}

try {
  // fractions are only allowed on the smallest unit
  parseDuration('P0.5YT3S');
} catch (err) {
  console.log({ err });
}

getDate(iso8601Date)

Get Date from an ISO 8601 date time string.

  • iso8601Date: string with ISO 8601 date source, date and number are also accepted

Returns date.

import { getDate } from '@0dep/piso';

const viableDates = [
  '2024-01-27',
  '2024-02-28',
  '2024-02-29',
  '2020-02-29',
  '2016-02-29',
  '2024-01',
  '2024-12',
  '20240127',
  '2024-02-27T08:06:30',
  '2024-02-27T08:06:30.001',
  '2024-02-27T08:06:30.0011',
  '2024-02-27T08:06:30.0',
  '2024-02-27T08:06:30,001',
  '2024-02-27T08:06:30Z',
  '2024-02-03T08:06:30+02:00',
  '2024-02-03T08:06:30.5+02:00',
  '20240203T080630+0200',
  '2024-02-03T08:06:30-02:30',
  '2024-02-03T08:06:30-02',
  '2025-01-01T12:00:42.01-02:00',
  '2025-01-01T12:00:42.01+02:30',
  '2025-01-01T12:00:42.01+02:30:30',
  '2025-01-01T23:59',
  '2025-01-01T24:00',
  '2025-01-01T24:00:00',
  '2025-01-01T24:00:00.000',
  '2025-01-01T24:00Z',
  '2025-01-01T24:00+01',
  '2025-01-01T24:00:00+01',
  '2025-01-01T24:00:00.00+01',
  '20240127T1200',
  '20240127T120001',
  '20240127T120001,001',
  new Date(2024, 3, 22),
  0,
  Date.UTC(2024, 3, 22),
];

for (const d of viableDates) {
  console.log({ [d]: getDate(d) });
}

try {
  getDate('2023-02-29');
} catch (err) {
  console.log({ err });
}

try {
  // unbalanced separators
  getDate('2023-02-28T1200');
} catch (err) {
  console.log({ err });
}

new ISOInterval(source)

Interval instance.

Properties:

interval.type

Number representing the interval type flags. Available after parse.

  • 1: Repeat
  • 2: Start date
  • 4: Duration
  • 8: End date

Example flags

  • 3: Repeat and start date, rather pointless but possible nevertheless
  • 5: Repeat and duration
  • 6: Start date and duration
  • 7: Repeat, start date, and duration
  • 10: Start- and end date
  • 12: Duration and end date
  • 13: Repeat, duration, and end date

Do I have repeat in my interval?

import { parseInterval } from '@0dep/piso';

console.log((parseInterval('R3/P1Y').type & 1) === 1 ? 'Yes' : 'No');
// Yes

console.log((parseInterval('R-1/P1Y').type & 1) === 1 ? 'Yes' : 'No');
// Yes, indefinite number of repetititions

console.log((parseInterval('R-1/2024-03-27/P1Y').type & 1) === 1 ? 'Yes' : 'No');
// Yes, indefinite number of repetititions from start date

console.log((parseInterval('R-1/P1Y/2024-03-27').type & 1) === 1 ? 'Yes' : 'No');
// Yes, indefinite number of repetititions until end date

console.log((parseInterval('R0/P1Y').type & 1) === 1 ? 'Yes' : 'No');
// No, zero is equal to once

console.log((parseInterval('R1/P1Y').type & 1) === 1 ? 'Yes' : 'No');
// No, since it's just once

console.log((parseInterval('R1/2024-03-28').type & 1) === 1 ? 'Yes' : 'No');
// No, pointless repeat

console.log((parseInterval('R1/2024-03-28/31').type & 1) === 1 ? 'Yes' : 'No');
// No, pointless repeat

console.log((parseInterval('R1/P1Y/2024-03-28').type & 1) === 1 ? 'Yes' : 'No');
// No

Is start date defined in my interval?

import { parseInterval } from '@0dep/piso';

const interval = parseInterval('R-1/2024-03-28/P1Y');

console.log((interval.type | 2) === interval.type ? 'Yes' : 'No');

interval.parse()

Returns ISOInterval.

Throws RangeError if something is off.

new ISODate(source[, offset])

ISO date instance.

Constructor:

  • source: ISO 8601 date source string
  • offset: optional source string offset column number

Properties:

  • result:
    • Y: full year
    • M: javascript month
    • D: date
    • H: hours
    • m: minutes
    • S: seconds
    • F: milliseconds
    • Z: Z, +, or -
    • OH: offset hours
    • Om: offset minutes
    • OS: offset seconds

date.parse()

date.parsePartialDate(Y, M, D)

  • Y: required full year
  • M: required javascript month
  • D: required date

Returns ISODate

date.toDate()

Get Date represented by source.

new ISODuration(source[, offset])

Duration instance.

Constructor:

  • source: duration source string
  • offset: optional source string offset column number

Properties:

  • result:
    • Y: years
    • M: months
    • W: weeks
    • D: days
    • H: hours
    • m: minutes
    • S: seconds

duration.toMilliseconds([startDate])

Get duration in milliseconds from optional start date.

duration.untilMilliseconds([endDate])

Get duration in milliseconds until optional end date.

Example

An example to get start and end date:

import { parseInterval } from '@0dep/piso';

const source = '2007-03-01T13:00:00Z/P1Y2M10DT2H30M';

const interval = parseInterval(source);

console.log('starts at', interval.getStartAt());
console.log('expires at', interval.getExpireAt());
console.log('duration milliseconds', interval.duration.toMilliseconds());

An example to get duration milliseconds:

import { parseDuration } from '@0dep/piso';

const duration = parseDuration('PT2H30M');

console.log('duration millisecods', duration.toMilliseconds(new Date()));

Repetitions

With end date

R4/P2Y/2007-08-01

Repetitionstart atexpire at
41999-08-012001-08-01
32001-08-012003-08-01
22003-08-012005-08-01
12005-08-012007-08-01

Benchmarking

Seems to run 3 times more efficient than RegExp implementations. But, date parsing is, off course, slower, compared to new Date('2024-03-26').

Keywords

FAQs

Last updated on 22 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc