· tor java

Duration and Period

This blog post is more than two years old. It is preserved here in the hope that it is useful to someone, but please be aware that links may be broken and that opinions expressed here may not reflect my current views. If this is a technical article, it may no longer reflect current best practice.

I’m in the process or tidying up the bot behind TorAtlas and I’ve chosen to write it in Java. My undergraduate degree had a lot of Java in it, but that was while ago now, and there are some cool new features I’ve learnt about including Period and Duration.

Some of the tweets coming from the bot talk about how long a relay has been contributing to the Tor network. When fetching the details document I look at the first_seen field and get this as a ZonedDateTime to make sure I’m doing all my calculations in UTC. I then use the between() method on various ChronoUnits to work out how long between the first time the relay was seen and now, and then I produced something human readable from this:

public static String formatAge(Temporal first_seen_date) {
    int years = (int) ChronoUnit.YEARS.between(first_seen_date, ZonedDateTime.now(ZoneOffset.UTC));
    int days = (int) ChronoUnit.DAYS.between(first_seen_date, ZonedDateTime.now(ZoneOffset.UTC));
    int hours = (int) ChronoUnit.HOURS.between(first_seen_date, ZonedDateTime.now(ZoneOffset.UTC));
    int minutes = (int) ChronoUnit.MINUTES.between(first_seen_date, ZonedDateTime.now(ZoneOffset.UTC));
    int seconds = (int) ChronoUnit.SECONDS.between(first_seen_date, ZonedDateTime.now(ZoneOffset.UTC));

    if (years > 0) {
        return String.format("%s years and %s days", years, (days % 365));
    } else if (days > 0) {
        return String.format("%s days and %s hours", days, (hours % 24));
    } else if (hours > 0) {
        return String.format("%s hours and %s minutes", hours, (minutes % 60));
    } else if (minutes > 0) {
        return String.format("%s minutes and %s seconds", minutes, (seconds % 60));
    } else {
        return String.format("%s seconds", seconds);
    }
}

I would be very happy if I could find something to accept a Duration instead, and produce a human-readable representation. I haven’t found anything quite like the library I was using in Python. I also have a restriction that I’d like things to be installable from Debian, so any new library would need to be something that could be maintained in Debian (i.e. active upstream and DFSG-compliant).

In Python, I used humanfriendly.format_timespan() which is a lovely library. If you’re writing Python, I’d definitely recommend it.


If you would like to contact me with comments, please send me an email.
If you would like to support my free software work, you can support me on Patreon or donate via PayPal.


This post was syndicated on:

Articles from my friends' blogs

/pubs

Rethinking ACKs at the Transport Layer. Ana Custura, Tom Jones, Gorry Fairhurst. FIT 2020: 731-736 Measuring mobile performance in the Tor network with OnionPerf. Ana Custura, Iain Learmonth, Gorry Fairhurst. MNM 2019: 233-238 Exploring Usable Path MTU in t…

via Analog August 29, 2020

SOTA Trip Reports: July 12, August 01, 02 2020

Here’re my first sota trip reports. I made some mistakes and have lots to learn still, but they were great days out and I’m dead keen to do more. Most importantly, I’ve got better at radio in general - I’m much more comfortable on a microphone, I understa…

via Blogs on Foxk.it August 4, 2020

Controlling MimoLive with Bitfocus Companion

I was recently asked how I control the video mixer software I use at work …

via Alfie, online - blog July 31, 2020

Generated by openring

Share Me