From 30630c3358aeadd42541cb3f1927a2cbc7afc39d Mon Sep 17 00:00:00 2001 From: Quentin Hibon Date: Wed, 20 Jan 2021 23:18:50 +0100 Subject: [PATCH] Convert Timestamp --- .../isoron/uhabits/core/models/Timestamp.java | 159 ------------------ .../isoron/uhabits/core/models/Timestamp.kt | 148 ++++++++++++++++ .../core/preferences/PreferencesTest.kt | 2 +- 3 files changed, 149 insertions(+), 160 deletions(-) delete mode 100644 uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.java create mode 100644 uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.kt diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.java deleted file mode 100644 index f67c35ed5..000000000 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.core.models; - -import org.isoron.platform.time.LocalDate; -import org.apache.commons.lang3.builder.*; -import org.isoron.uhabits.core.utils.*; - -import java.util.*; - -import static java.util.Calendar.*; - -public final class Timestamp implements Comparable { - public static final long DAY_LENGTH = 86400000; - - public static final Timestamp ZERO = new Timestamp(0); - - private final long unixTime; - - public static Timestamp fromLocalDate(LocalDate date) { - return new Timestamp(946684800000L + date.getDaysSince2000() * 86400000L); - } - - public Timestamp(long unixTime) { - if (unixTime < 0) - throw new IllegalArgumentException( - "Invalid unix time: " + unixTime); - - if (unixTime % DAY_LENGTH != 0) - unixTime = (unixTime / DAY_LENGTH) * DAY_LENGTH; - - this.unixTime = unixTime; - } - - public Timestamp(GregorianCalendar cal) { - this(cal.getTimeInMillis()); - } - - public static Timestamp from(int year, int javaMonth, int day) { - GregorianCalendar cal = DateUtils.getStartOfTodayCalendar(); - cal.set(year, javaMonth, day, 0, 0, 0); - return new Timestamp(cal.getTimeInMillis()); - } - - public long getUnixTime() { - return unixTime; - } - - public LocalDate toLocalDate() { - long millisSince2000 = unixTime - 946684800000L; - int daysSince2000 = (int) (millisSince2000 / 86400000); - return new LocalDate(daysSince2000); - } - - /** - * Returns -1 if this timestamp is older than the given timestamp, 1 if this - * timestamp is newer, or zero if they are equal. - */ - @Override - public int compareTo(Timestamp other) { - return Long.signum(this.unixTime - other.unixTime); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - - if (o == null || getClass() != o.getClass()) return false; - - Timestamp timestamp = (Timestamp) o; - - return new EqualsBuilder() - .append(unixTime, timestamp.unixTime) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37).append(unixTime).toHashCode(); - } - - /** - * Given two timestamps, returns whichever timestamp is the oldest one. - */ - public static Timestamp oldest(Timestamp first, Timestamp second) { - return first.unixTime < second.unixTime ? first : second; - } - - public Timestamp minus(int days) { - return plus(-days); - } - - public Timestamp plus(int days) { - return new Timestamp(unixTime + DAY_LENGTH * days); - } - - /** - * Returns the number of days between this timestamp and the given one. If - * the other timestamp equals this one, returns zero. If the other timestamp - * is older than this one, returns a negative number. - */ - public int daysUntil(Timestamp other) { - return (int) ((other.unixTime - this.unixTime) / DAY_LENGTH); - } - - public boolean isNewerThan(Timestamp other) { - return compareTo(other) > 0; - } - - public boolean isOlderThan(Timestamp other) { - return compareTo(other) < 0; - } - - - public Date toJavaDate() { - return new Date(unixTime); - } - - public GregorianCalendar toCalendar() { - GregorianCalendar day = - new GregorianCalendar(TimeZone.getTimeZone("GMT")); - day.setTimeInMillis(unixTime); - return day; - } - - @Override - public String toString() { - return DateFormats.getCSVDateFormat().format(new Date(unixTime)); - } - - /** - * Returns an integer corresponding to the day of the week. Saturday maps - * to 0, Sunday maps to 1, and so on. - */ - public int getWeekday() { - return toCalendar().get(DAY_OF_WEEK) % 7; - } - - Timestamp truncate(DateUtils.TruncateField field, int firstWeekday) { - return new Timestamp(DateUtils.truncate(field, unixTime, firstWeekday)); - } -} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.kt new file mode 100644 index 000000000..d50f5d52d --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Timestamp.kt @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.core.models + +import org.apache.commons.lang3.builder.EqualsBuilder +import org.apache.commons.lang3.builder.HashCodeBuilder +import org.isoron.platform.time.LocalDate +import org.isoron.uhabits.core.utils.DateFormats.Companion.getCSVDateFormat +import org.isoron.uhabits.core.utils.DateUtils +import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar +import org.isoron.uhabits.core.utils.DateUtils.Companion.truncate +import java.util.Calendar +import java.util.Date +import java.util.GregorianCalendar +import java.util.TimeZone + +class Timestamp(unixTime: Long) : Comparable { + val unixTime: Long + + constructor(cal: GregorianCalendar) : this(cal.timeInMillis) {} + + fun toLocalDate(): LocalDate { + val millisSince2000 = unixTime - 946684800000L + val daysSince2000 = (millisSince2000 / 86400000).toInt() + return LocalDate(daysSince2000) + } + + /** + * Returns -1 if this timestamp is older than the given timestamp, 1 if this + * timestamp is newer, or zero if they are equal. + */ + override fun compareTo(other: Timestamp): Int { + return java.lang.Long.signum(unixTime - other.unixTime) + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || javaClass != other.javaClass) return false + val timestamp = other as Timestamp + return EqualsBuilder() + .append(unixTime, timestamp.unixTime) + .isEquals + } + + override fun hashCode(): Int { + return HashCodeBuilder(17, 37).append(unixTime).toHashCode() + } + + operator fun minus(days: Int): Timestamp { + return plus(-days) + } + + operator fun plus(days: Int): Timestamp { + return Timestamp(unixTime + DAY_LENGTH * days) + } + + /** + * Returns the number of days between this timestamp and the given one. If + * the other timestamp equals this one, returns zero. If the other timestamp + * is older than this one, returns a negative number. + */ + fun daysUntil(other: Timestamp): Int { + return ((other.unixTime - unixTime) / DAY_LENGTH).toInt() + } + + fun isNewerThan(other: Timestamp): Boolean { + return compareTo(other) > 0 + } + + fun isOlderThan(other: Timestamp): Boolean { + return compareTo(other) < 0 + } + + fun toJavaDate(): Date { + return Date(unixTime) + } + + fun toCalendar(): GregorianCalendar { + val day = GregorianCalendar(TimeZone.getTimeZone("GMT")) + day.timeInMillis = unixTime + return day + } + + override fun toString(): String { + return getCSVDateFormat().format(Date(unixTime)) + } + + /** + * Returns an integer corresponding to the day of the week. Saturday maps + * to 0, Sunday maps to 1, and so on. + */ + val weekday: Int + get() = toCalendar()[Calendar.DAY_OF_WEEK] % 7 + + fun truncate(field: DateUtils.TruncateField?, firstWeekday: Int): Timestamp { + return Timestamp( + truncate( + field!!, + unixTime, + firstWeekday + ) + ) + } + + companion object { + const val DAY_LENGTH: Long = 86400000 + val ZERO = Timestamp(0) + fun fromLocalDate(date: LocalDate): Timestamp { + return Timestamp(946684800000L + date.daysSince2000 * 86400000L) + } + + fun from(year: Int, javaMonth: Int, day: Int): Timestamp { + val cal = getStartOfTodayCalendar() + cal[year, javaMonth, day, 0, 0] = 0 + return Timestamp(cal.timeInMillis) + } + + /** + * Given two timestamps, returns whichever timestamp is the oldest one. + */ + fun oldest(first: Timestamp, second: Timestamp): Timestamp { + return if (first.unixTime < second.unixTime) first else second + } + } + + init { + var unixTime = unixTime + require(unixTime >= 0) { "Invalid unix time: $unixTime" } + if (unixTime % DAY_LENGTH != 0L) unixTime = unixTime / DAY_LENGTH * DAY_LENGTH + this.unixTime = unixTime + } +} diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/preferences/PreferencesTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/preferences/PreferencesTest.kt index 340f97440..d73cf9bce 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/preferences/PreferencesTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/preferences/PreferencesTest.kt @@ -26,7 +26,7 @@ import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.models.HabitList -import org.isoron.uhabits.core.models.Timestamp.ZERO +import org.isoron.uhabits.core.models.Timestamp.Companion.ZERO import org.isoron.uhabits.core.ui.ThemeSwitcher import org.junit.Before import org.junit.Test