Maven
License: MIT
CI Status

asimov/time

Useful time and date related functions and extensions.

Installation

Gradle (Kotlin)

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.nbottarini:asimov-time:0.5")
}

Gradle (Groovy)

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.nbottarini:asimov-time:0.5'
}

Maven

<dependency>
    <groupId>com.nbottarini</groupId>
    <artifactId>asimov-time</artifactId>
    <version>0.5</version>
</dependency>

Usage

Extensions

    yearMonth.formatAsISO8601() // Formats a year month in ISO-8601. Ex: 2020-06
    localDate.formatAsISO8601() // Formats date in ISO-8601. Ex: 2020-06-09
    localDateTime.formatAsISO8601() // Formats date time in ISO-8601. Ex: 2020-06-09T22:10:05Z

Parsers

    LocalDateParser().parseISO8601("2020-06-09") // returns LocalDate
    LocalDateParser().parse("09/06/2020", "d/M/yyyy") // returns LocalDate
    
    LocalDateTimeParser().parseISO8601("2020-06-09T22:10:05Z") // returns LocalDateTime
    LocalDateTimeParser().parseISO8601("2020-06-09T22:10:05-03:00") // returns LocalDateTime in UTC
    LocalDateTimeParser().parseISO8601("2020-06-09T22:10:05-03:00", ZoneOffset.UTC) // returns LocalDateTime in UTC

    YearMonthParser().parseISO8601("2020-06") // returns YearMonth

Clock

Provides a central clock to use in your code. The clock can be stopped to control time in tests.

    Clock.now() // returns LocalDateTime with current date and time in UTC
    Clock.today() // returns LocalDate with current date
    Clock.stoppedAt(someDate) // Stops clock at given date with time 00:00:00
    Clock.stoppedAt(someDateTime) // Stops clock at given time
    Clock.live() // Sets the clock to have live time again

It also comes with a timed function to use in tests. The timed function receives and executes a lambda.
It stops the clock and makes it advance 1 second with each call. This function helps to make tests that depends on time
to be deterministic.

    timed { someAction() }
    timed { someOtherAction() } // Time passes on each call

GitHub

View Github