made with requirebin
Last active
September 25, 2017 05:34
-
-
Save rhysburnie/798015e919e8ab5c93c0345cecf4d514 to your computer and use it in GitHub Desktop.
iso8601-duration conversions - requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Welcome! require() some modules from npm (like you were using browserify) | |
// and then hit Run Code to run your code on the right side. | |
// Modules get downloaded from browserify-cdn and bundled in your browser. | |
var iso8601Duration = require('iso8601-duration'); | |
// https://github.com/wking/milliseconds-to-iso-8601-duration/blob/master/milliseconds-to-iso-8601-duration.js | |
var millisecondsToISO8601Duration = function(milliseconds) { | |
if (milliseconds == 0) { | |
return 'P0D'; | |
} | |
var offset = Math.floor(milliseconds); | |
var days = 0; | |
if (offset < 0) { | |
days = Math.floor(offset % 86400000); | |
offset -= 86400000 * days; | |
} | |
var milliseconds = offset % 1000; | |
offset = Math.floor(offset / 1000); | |
var seconds = offset % 60; | |
offset = Math.floor(offset / 60); | |
var minutes = offset % 60; | |
offset = Math.floor(offset / 60); | |
var hours = offset % 24; | |
days += Math.floor(offset / 24); | |
var parts = ['P']; | |
if (days) { | |
parts.push(days + 'D'); | |
} | |
if (hours || minutes || seconds || milliseconds) { | |
parts.push('T'); | |
if (hours) { | |
parts.push(hours + 'H'); | |
} | |
if (minutes) { | |
parts.push(minutes + 'M'); | |
} | |
if (seconds || milliseconds) { | |
parts.push(seconds); | |
if (milliseconds) { | |
milliseconds = milliseconds.toString(); | |
while (milliseconds.length < 3) { | |
milliseconds = '0' + milliseconds; | |
} | |
parts.push('.' + milliseconds); | |
} | |
parts.push('S'); | |
} | |
} | |
return parts.join('') | |
}; | |
var strDuration = 'P0Y0M0DT0H3M30.001S'; | |
var objDuration = iso8601Duration.parse(strDuration); | |
console.log(objDuration); | |
var msMap = { | |
days: 8.64e+7, | |
hours: 3.6e+6, | |
minutes: 60000, | |
months: 2.628e+9, | |
seconds: 1000, | |
weeks: 6.048e+8, | |
years: 3.154e+10, | |
}; | |
var totalMS = 0; | |
Object.keys(msMap).forEach(function(key){ | |
if (objDuration[key]) { | |
totalMS += objDuration[key] * msMap[key]; | |
} | |
}); | |
console.log('totalMS', totalMS); | |
console.log(millisecondsToISO8601Duration(totalMS), strDuration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function(){require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"iso8601-duration":[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var numbers="\\d+(?:[\\.,]\\d{0,3})?";var weekPattern="("+numbers+"W)";var datePattern="("+numbers+"Y)?("+numbers+"M)?("+numbers+"D)?";var timePattern="T("+numbers+"H)?("+numbers+"M)?("+numbers+"S)?";var iso8601="P(?:"+weekPattern+"|"+datePattern+"(?:"+timePattern+")?)";var objMap=["weeks","years","months","days","hours","minutes","seconds"];var pattern=exports.pattern=new RegExp(iso8601);var parse=exports.parse=function parse(durationString){return durationString.match(pattern).slice(1).reduce(function(prev,next,idx){prev[objMap[idx]]=parseFloat(next)||0;return prev},{})};var toSeconds=exports.toSeconds=function toSeconds(duration,startDate){var timestamp=startDate?startDate.getTime():Date.now();var now=new Date(timestamp);var then=new Date(timestamp);then.setFullYear(then.getFullYear()+duration.years);then.setMonth(then.getMonth()+duration.months);then.setDate(then.getDate()+duration.days);then.setHours(then.getHours()+duration.hours);then.setMinutes(then.getMinutes()+duration.minutes);then.setMilliseconds(then.getMilliseconds()+duration.seconds*1e3);then.setDate(then.getDate()+duration.weeks*7);var seconds=(then.getTime()-now.getTime())/1e3;return seconds};exports.default={toSeconds:toSeconds,pattern:pattern,parse:parse}},{}]},{},[]);var iso8601Duration=require("iso8601-duration");var millisecondsToISO8601Duration=function(milliseconds){if(milliseconds==0){return"P0D"}var offset=Math.floor(milliseconds);var days=0;if(offset<0){days=Math.floor(offset%864e5);offset-=864e5*days}var milliseconds=offset%1e3;offset=Math.floor(offset/1e3);var seconds=offset%60;offset=Math.floor(offset/60);var minutes=offset%60;offset=Math.floor(offset/60);var hours=offset%24;days+=Math.floor(offset/24);var parts=["P"];if(days){parts.push(days+"D")}if(hours||minutes||seconds||milliseconds){parts.push("T");if(hours){parts.push(hours+"H")}if(minutes){parts.push(minutes+"M")}if(seconds||milliseconds){parts.push(seconds);if(milliseconds){milliseconds=milliseconds.toString();while(milliseconds.length<3){milliseconds="0"+milliseconds}parts.push("."+milliseconds)}parts.push("S")}}return parts.join("")};var strDuration="P0Y0M0DT0H3M30.001S";var objDuration=iso8601Duration.parse(strDuration);console.log(objDuration);var msMap={days:864e5,hours:36e5,minutes:6e4,months:2628e6,seconds:1e3,weeks:6048e5,years:3154e7};var totalMS=0;Object.keys(msMap).forEach(function(key){if(objDuration[key]){totalMS+=objDuration[key]*msMap[key]}});console.log("totalMS",totalMS);console.log(millisecondsToISO8601Duration(totalMS),strDuration)},0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"iso8601-duration": "1.0.6" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some tests for adding iso8601-duration strings