Formatting a Duration in Java 8 / jsr310

Java 9 and later: Duration::to…Part methods In Java 9 the Duration class gained new to…Part methods for returning the various parts of days, hours, minutes, seconds, milliseconds/nanoseconds. See this pre-release OpenJDK source code. Given a duration of 49H30M20.123S… toNanosPart() = 123000000 toMillisPart() = 123 toSecondsPart() = 20 toMinutesPart() = 30 toHoursPart() = 1 toDaysPart() = … Read more

current/duration time of html5 video?

HTML: <video id=”video-active” class=”video-active” width=”640″ height=”390″ controls=”controls”> <source src=”https://stackoverflow.com/questions/6380956/myvideo.mp4″ type=”video/mp4″> </video> <div id=”current”>0:00</div> <div id=”duration”>0:00</div> JavaScript: $(document).ready(function(){ $(“#video-active”).on( “timeupdate”, function(event){ onTrackedVideoFrame(this.currentTime, this.duration); }); }); function onTrackedVideoFrame(currentTime, duration){ $(“#current”).text(currentTime); //Change #current to currentTime $(“#duration”).text(duration) } Notes: Every 15 to 250ms, or whenever the MediaController’s media controller position changes, whichever happens least often, the user agent must … Read more

Measuring elapsed time in python

For measuring elapsed CPU time, look at time.clock(). This is the equivalent of Linux’s times() user time field. For benchmarking, use timeit. The datetime module, which is part of Python 2.3+, also has microsecond time if supported by the platform. Example: >>> import datetime as dt >>> n1=dt.datetime.now() >>> n2=dt.datetime.now() >>> (n2-n1).microseconds 678521 >>> (n2.microsecond-n1.microsecond)/1e6 … Read more

Converting Java to Scala durations

I don’t know whether an explicit conversion is the only way, but if you want to do it right FiniteDuration(d.toNanos, TimeUnit.NANOSECONDS) toNanos will return the total duration, while getNano will only return the nanoseconds component, which is not what you want. E.g. import java.time.Duration import jata.time.temporal.ChronoUnit Duration.of(1, ChronoUnit.HOURS).getNano // 0 Duration.of(1, ChronoUnit.HOURS).toNanos // 3600000000000L That … Read more

What does ‘PT’ prefix stand for in Duration?

As can be found on the page Jesper linked to (ISO-8601 – Data elements and interchange formats – Information interchange – Representation of dates and times) P is the duration designator (for period) placed at the start of the duration representation. Y is the year designator that follows the value for the number of years. … Read more