aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt
blob: c0feab1e8c8a3a6691ce38f287455448df87ca87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright 2019 Allan Wang
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.utils

import android.content.Context
import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.R
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

/**
 * Converts time in millis to readable date,
 * eg Apr 24 at 7:32 PM
 *
 * With regards to date modifications in calendars,
 * it appears to respect calendar rules;
 * see https://stackoverflow.com/a/43227817/4407321
 */
fun Long.toReadableTime(context: Context): String {
    val cal = Calendar.getInstance()
    cal.timeInMillis = this
    val timeFormatter = SimpleDateFormat.getTimeInstance(DateFormat.SHORT)
    val time = timeFormatter.format(Date(this))
    val day = when {
        cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -1) } -> context.string(R.string.today)
        cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -2) } -> context.string(R.string.yesterday)
        else -> {
            val dayFormatter = SimpleDateFormat("MMM dd", Locale.getDefault())
            dayFormatter.format(Date(this))
        }
    }
    return context.getString(R.string.time_template, day, time)
}