Giter VIP home page Giter VIP logo

Comments (5)

tolu avatar tolu commented on July 24, 2024

I can see why that would make sense but I think it's out of scope for this project since this is aimed at consuming and not constructing iso8601 durations.

The reason being that (without much thought):

  • given the example, it seems simple to just do that in your own code
  • given to construct from 2 dates, codebase would grow and there are certainly lots of corner cases
  • this is the first interest expressed in such a feature

from iso8601-duration.

tolu avatar tolu commented on July 24, 2024

we'll keep this issue open and see if others chime in 👍
Thanks for creating it!

from iso8601-duration.

luwes avatar luwes commented on July 24, 2024

I'm looking for this function as well. Use case is to create structured data for a video object, needed for the duration of the video. https://schema.org/VideoObject

from iso8601-duration.

luwes avatar luwes commented on July 24, 2024

This should be it I think.

export function secondsToISOString(seconds) {
  let Y = Math.floor(seconds / 60 / 60 / 24 / 365);
  let M = Math.floor(seconds / 60 / 60 / 24 / 30.42 % 12);
  let D = Math.floor(seconds / 60 / 60 / 24 % 30.42);
  let h = Math.floor(seconds / 60 / 60 % 24);
  let m = Math.floor(seconds / 60 % 60);
  let s = Math.floor(seconds % 60);

  if (!seconds) {
    return 'P0D';
  }

  return (
    (seconds < 0 ? '-' : '') +
    'P' +
    (Y ? Y + 'Y' : '') +
    (M ? M + 'M' : '') +
    (D ? D + 'D' : '') +
    (h || m || s ? 'T' : '') +
    (h ? h + 'H' : '') +
    (m ? m + 'M' : '') +
    (s ? s + 'S' : '')
  );
}

Tests:

import test from 'tape';
import { secondsToISOString } from '../src/utils.js';

test('secondsToISOString', (t) => {
  t.equal(secondsToISOString(0), 'P0D');
  t.equal(secondsToISOString(120), 'PT2M');
  t.equal(secondsToISOString(3601), 'PT1H1S');
  t.equal(secondsToISOString(1), 'PT1S');
  t.equal(secondsToISOString(5410), 'PT1H30M10S');
  t.equal(secondsToISOString(37226652), 'P1Y2M4DT20H44M12S');
  t.equal(secondsToISOString(37313052), 'P1Y2M5DT20H44M12S');
  t.end();
});

from iso8601-duration.

tolu avatar tolu commented on July 24, 2024

A better solution is to use this then:
https://www.npmjs.com/package/temporal-polyfill

new Temporal.Duration(0, 0, 0, 40); // => P40D

I've decided not to add more logic to this project since the Temporal proposal is around the corner (hopefully 🤞 )

from iso8601-duration.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.