Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Some API adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Oct 24, 2017
1 parent 6410145 commit 4c6bffe
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,131 @@ Consistent and portable date and time utilities for multiplatform kotlin

[![Build Status](https://travis-ci.org/soywiz/klock.svg?branch=master)](https://travis-ci.org/soywiz/klock)
[![Maven Version](https://img.shields.io/github/tag/soywiz/klock.svg?style=flat&label=maven)](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22klock%22)

```kotlin
object Klock {
fun currentTimeMillis(): Long
fun getLocalTimezoneOffset(unix: Long): Int
}

enum class DayOfWeek {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;
}

enum class Month {
January, February, March, April, May, June,
July, August, September, October, November, December;

val index0: Int

fun days(isLeap: Boolean): Int
fun daysToStart(isLeap: Boolean): Int
fun daysToEnd(isLeap: Boolean): Int

fun days(year: Int): Int
fun daysToStart(year: Int): Int
fun daysToEnd(year: Int): Int
}

object Year {
fun isLeap(year: Int): Boolean
}

interface DateTime {
companion object {
val EPOCH: DateTime
operator fun invoke(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0, milliseconds: Int = 0): DateTime
operator fun invoke(time: Long): DateTime

fun fromUnix(time: Long): DateTime
fun fromUnixLocal(time: Long): DateTime

fun nowUnix(): Long
fun now(): DateTime
fun nowLocal(): DateTime
fun createAdjusted(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0, milliseconds: Int = 0): DateTime
fun isLeapYear(year: Int): Boolean
fun daysInMonth(month: Int, isLeap: Boolean): Int
fun daysInMonth(month: Int, year: Int): Int
}

val year: Int
val month: Int
val dayOfWeekInt: Int
val dayOfMonth: Int
val dayOfYear: Int
val hours: Int
val minutes: Int
val seconds: Int
val milliseconds: Int
val timeZone: String
val unix: Long
val offset: Int
val utc: UtcDateTime
fun add(deltaMonths: Int, deltaMilliseconds: Long): DateTime

val dayOfWeek: DayOfWeek
val month0: Int
val month1: Int
val monthEnum: Month

fun toUtc(): DateTime
fun toLocal(): DateTime
fun addOffset(offset: Int): DateTime
fun toOffset(offset: Int): DateTime
fun addYears(delta: Int): DateTime
fun addMonths(delta: Int): DateTime
fun addHours(delta: Double): DateTime
fun addMinutes(delta: Double): DateTime
fun addSeconds(delta: Double): DateTime
fun addMilliseconds(delta: Double): DateTime
fun addMilliseconds(delta: Long): DateTime

operator fun plus(delta: TimeDistance): DateTime
operator fun minus(delta: TimeDistance): DateTime

fun toString(format: String): String = SimplerDateFormat(format).format(this)
}


data class TimeDistance(val years: Int = 0, val months: Int = 0, val days: Double = 0.0, val hours: Double = 0.0, val minutes: Double = 0.0, val seconds: Double = 0.0, val milliseconds: Double = 0.0) {
operator fun unaryMinus(): TimeDistance
operator fun minus(other: TimeDistance): TimeDistance
operator fun plus(other: TimeDistance): TimeDistance
operator fun times(times: Double): TimeDistance
}

data class TimeSpan {
val milliseconds: Int
val seconds: Double

companion object {
val ZERO = TimeSpan(0)
}

override fun compareTo(other: TimeSpan): Int
operator fun plus(other: TimeSpan): TimeSpan
operator fun minus(other: TimeSpan): TimeSpan
operator fun times(scale: Int): TimeSpan
operator fun times(scale: Double): TimeSpan
}

inline val Int.years: TimeDistance
inline val Int.months: TimeDistance
inline val Number.days: TimeDistance
inline val Number.hours: TimeDistance
inline val Number.minutes: TimeDistance
inline val Number.seconds: TimeSpan
inline val Number.milliseconds: TimeSpan

class SimplerDateFormat(val format: String) {
companion object {
val DEFAULT_FORMAT: SimplerDateFormat // "EEE, dd MMM yyyy HH:mm:ss z"
}

fun format(date: Long): String
fun format(dd: DateTime): String
fun parse(str: String): Long
fun parseDate(str: String): DateTime
}
```
3 changes: 1 addition & 2 deletions klock/common/src/main/kotlin/com/soywiz/klock/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ interface DateTime {

operator fun plus(delta: TimeDistance): DateTime = this.add(delta.years * 12 + delta.months, (delta.days * MILLIS_PER_DAY + delta.hours * MILLIS_PER_HOUR + delta.minutes * MILLIS_PER_MINUTE + delta.seconds * MILLIS_PER_SECOND + delta.milliseconds).toLong())
operator fun minus(delta: TimeDistance): DateTime = this + -delta
fun toString(format: String): String = SimplerDateFormat(format).format(this)

companion object {
val EPOCH by lazy { DateTime(1970, 1, 1, 0, 0, 0) as UtcDateTime }
Expand Down Expand Up @@ -183,7 +184,6 @@ class OffsetDateTime private constructor(
OffsetDateTime(utc.add(deltaMonths, deltaMilliseconds), offset)

override fun toString(): String = SimplerDateFormat.DEFAULT_FORMAT.format(this)
fun toString(format: String): String = SimplerDateFormat(format).format(this)
}

class UtcDateTime internal constructor(internal val internalMillis: Long, dummy: Boolean) : DateTime {
Expand Down Expand Up @@ -275,7 +275,6 @@ class UtcDateTime internal constructor(internal val internalMillis: Long, dummy:
override fun hashCode(): Int = internalMillis.hashCode()
override fun equals(other: Any?): Boolean = this.unix == (other as? DateTime?)?.unix
override fun toString(): String = SimplerDateFormat.DEFAULT_FORMAT.format(this)
fun toString(format: String): String = SimplerDateFormat(format).format(this)
}

data class TimeDistance(val years: Int = 0, val months: Int = 0, val days: Double = 0.0, val hours: Double = 0.0, val minutes: Double = 0.0, val seconds: Double = 0.0, val milliseconds: Double = 0.0) {
Expand Down

0 comments on commit 4c6bffe

Please sign in to comment.