Skip to content

Commit

Permalink
feat: Add DateUtils to new Core (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne authored Feb 21, 2025
2 parents 9aed9b2 + 6723178 commit fea7798
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Legacy/AppLock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
}

dependencies {
implementation project(path: ':Core:Legacy')
implementation project(':Core:Legacy')

implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05'
implementation("com.louiscad.splitties:splitties-appctx:3.0.0")
Expand Down
2 changes: 1 addition & 1 deletion Legacy/BugTracker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ android {
}

dependencies {
implementation project(path: ':Core:Legacy')
implementation project(':Core:Legacy')
}
2 changes: 1 addition & 1 deletion Legacy/Confetti/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ android {
kotlinOptions { jvmTarget = javaVersion }

dependencies {
implementation project(path: ':Core:Legacy')
implementation project(':Core:Legacy')
}
}
2 changes: 1 addition & 1 deletion Legacy/Stores/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {
}

dependencies {
implementation project(path: ':Core:Legacy')
implementation project(':Core:Legacy')

api 'androidx.datastore:datastore-preferences:1.1.2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class AccessTokenUsageInterceptor(
data class ApiCallRecord(val accessToken: String, val date: Long, val responseCode: Int)

companion object {
private const val SIX_MONTHS = 60 * 60 * 24 * 182L // In seconds
private const val SECONDS_IN_A_DAY = 86_400L
private const val SIX_MONTHS = SECONDS_IN_A_DAY * 182L // In seconds
private const val TEN_SECONDS = 10 // In seconds
}
}
29 changes: 0 additions & 29 deletions src/main/kotlin/com/infomaniak/core/DateUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.lib.core.utils
package com.infomaniak.core.utils

import android.os.Build
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.time.ZoneId
Expand All @@ -27,22 +26,27 @@ import java.util.Calendar
import java.util.Date
import java.util.Locale

const val FORMAT_DATE_CLEAR_MONTH = "dd MMM yyyy"
const val FORMAT_DATE_CLEAR_FULL_MONTH = "EEEE d MMMM yyyy"
const val FORMAT_DATE_DAY_MONTH = "EEE d MMM"
const val FORMAT_DATE_DAY_MONTH_YEAR = "EEE d MMM yyyy"
const val FORMAT_DATE_CLEAR_MONTH = "dd MMM yyyy"
const val FORMAT_DATE_CLEAR_MONTH_DAY_ONE_CHAR = "d MMM yyyy"
const val FORMAT_DATE_DAY_FULL_MONTH_WITH_TIME = "EEEE d MMMM HH:mm"
const val FORMAT_DATE_DAY_FULL_MONTH_YEAR_WITH_TIME = "EEEE d MMMM yyyy HH:mm"
const val FORMAT_DATE_CLEAR_MONTH_DAY_ONE_CHAR = "d MMM yyyy"
const val FORMAT_DATE_DAY_MONTH = "EEE d MMM"
const val FORMAT_DATE_DAY_MONTH_YEAR = "EEE d MMM yyyy"
const val FORMAT_DATE_DEFAULT = "dd.MM.yy"
const val FORMAT_DATE_HOUR_MINUTE = "HH:mm"
const val FORMAT_DATE_FULL = "EEEE d MMMM"
const val FORMAT_DATE_SHORT_DAY_ONE_CHAR = "d MMM"
const val FORMAT_SCHEDULE_MAIL = "yyyy-MM-dd'T'HH:mm:ssXXX"
const val FORMAT_DATE_SIMPLE = "dd/MM/yyyy"
const val FORMAT_DATE_TITLE = "E d MMMM"
const val FORMAT_DATE_WITH_TIMEZONE = "yyyy-MM-dd'T'HH:mm:ssZ"
const val FORMAT_EVENT_DATE = "dd/MM/yyyy HH:mm"
const val FORMAT_FULL_DATE = "EEEE dd MMMM yyyy"
const val FORMAT_FULL_DATE_WITH_HOUR = "EEEE MMM d yyyy HH:mm:ss"
const val FORMAT_HOUR_MINUTES = "HH:mm"
const val FORMAT_NEW_FILE = "yyyyMMdd_HHmmss"
const val FORMAT_SCHEDULE_MAIL = "yyyy-MM-dd'T'HH:mm:ssXXX"

const val SECONDS_IN_A_DAY = 86_400L

//region Format Dates
enum class FormatData {
Expand All @@ -51,12 +55,9 @@ enum class FormatData {
BOTH,
}

fun Date.format(pattern: String = FORMAT_DATE_DEFAULT): String {
val simpleDateFormat = SimpleDateFormat(pattern, Locale.getDefault())
return simpleDateFormat.format(this)
}
fun Date.format(pattern: String = FORMAT_DATE_DEFAULT): String = SimpleDateFormat(pattern, Locale.getDefault()).format(this)

@RequiresApi(Build.VERSION_CODES.O)
@RequiresApi(26)
fun Date.formatWithLocal(formatData: FormatData, formatStyle: FormatStyle, formatStyleSecondary: FormatStyle? = null): String {
val formatter = when (formatData) {
FormatData.DATE -> DateTimeFormatter.ofLocalizedDate(formatStyle)
Expand All @@ -69,6 +70,10 @@ fun Date.formatWithLocal(formatData: FormatData, formatStyle: FormatStyle, forma
//endregion

//region Get a new Date relatively to a given Date
fun Date.yesterday(): Date = addDays(-1)

fun Date.tomorrow(): Date = addDays(1)

fun Date.startOfTheDay(): Date = Calendar.getInstance().apply {
time = this@startOfTheDay
set(Calendar.HOUR_OF_DAY, 0)
Expand All @@ -83,11 +88,6 @@ fun Date.endOfTheDay(): Date = Calendar.getInstance().apply {
set(Calendar.SECOND, 59)
}.time

fun Date.tomorrow(): Date = Calendar.getInstance().apply {
time = this@tomorrow
add(Calendar.DATE, 1)
}.time

fun Date.startOfTomorrow(): Date = tomorrow().startOfTheDay()

fun Date.endOfTomorrow(): Date = tomorrow().endOfTheDay()
Expand Down Expand Up @@ -212,17 +212,11 @@ fun Date.isSameDayAs(targetDate: Date): Boolean {
day() == targetDate.day()
}

fun Date.isYesterday(): Boolean = isSameDayAs(Date().yesterday())

fun Date.isToday(): Boolean = isSameDayAs(Date())

fun Date.isYesterday(): Boolean {
val yesterday = Date().addDays(-1)
return isSameDayAs(yesterday)
}

fun Date.isThisWeek(): Boolean {
val now = Date()
return this in now.startOfTheWeek()..now.endOfTheWeek()
}
fun Date.isTomorrow(): Boolean = isSameDayAs(Date().tomorrow())

fun Date.isWeekend(): Boolean {
val calendar = Calendar.getInstance().apply {
Expand All @@ -232,6 +226,8 @@ fun Date.isWeekend(): Boolean {
return day == 1 || day == 7
}

fun Date.isThisWeek(): Boolean = Date().let { now -> this in now.startOfTheWeek()..now.endOfTheWeek() }

fun Date.isThisMonth(): Boolean = Date().let { now -> year() == now.year() && month() == now.month() }

fun Date.isThisYear(): Boolean = Date().let { now -> year() == now.year() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Core - Android
* Copyright (C) 2024 Infomaniak Network SA
* Copyright (C) 2024-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core
package com.infomaniak.core.utils

import android.app.DownloadManager.Request
import android.net.Uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core
package com.infomaniak.core.utils

import android.util.Patterns

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core
package com.infomaniak.core.utils

inline fun <reified T : Enum<T>> enumValueOfOrNull(value: String?): T? {
return value?.let { runCatching { enumValueOf<T>(it) }.getOrNull() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core
package com.infomaniak.core.utils

import java.util.Locale

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core
package com.infomaniak.core.utils

import android.os.Build

Expand Down

0 comments on commit fea7798

Please sign in to comment.