/
DateFormatter.kt
94 lines (80 loc) · 3.17 KB
/
DateFormatter.kt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS 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.
*
* QKSMS 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 QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.util
import android.content.Context
import android.text.format.DateFormat
import com.moez.QKSMS.common.util.extensions.isSameDay
import com.moez.QKSMS.common.util.extensions.isSameWeek
import com.moez.QKSMS.common.util.extensions.isSameYear
import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class DateFormatter @Inject constructor(val context: Context) {
/**
* Formats the [pattern] correctly for the current locale, and replaces 12 hour format with
* 24 hour format if necessary
*/
private fun getFormatter(pattern: String): SimpleDateFormat {
var formattedPattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), pattern)
if (DateFormat.is24HourFormat(context)) {
formattedPattern = formattedPattern.replace("h", "HH").replace(" a".toRegex(), "")
}
return SimpleDateFormat(formattedPattern, Locale.getDefault())
}
fun getDetailedTimestamp(date: Long): String {
return getFormatter("M/d/y, h:mm:ss a").format(date)
}
fun getTimestamp(date: Long): String {
return getFormatter("h:mm a").format(date)
}
fun getMessageTimestamp(date: Long): String {
val now = Calendar.getInstance()
val then = Calendar.getInstance()
then.timeInMillis = date
return when {
now.isSameDay(then) -> getFormatter("h:mm a")
now.isSameWeek(then) -> getFormatter("E h:mm a")
now.isSameYear(then) -> getFormatter("MMM d, h:mm a")
else -> getFormatter("MMM d yyyy, h:mm a")
}.format(date)
}
fun getConversationTimestamp(date: Long): String {
val now = Calendar.getInstance()
val then = Calendar.getInstance()
then.timeInMillis = date
return when {
now.isSameDay(then) -> getFormatter("h:mm a")
now.isSameWeek(then) -> getFormatter("E")
now.isSameYear(then) -> getFormatter("MMM d")
else -> getFormatter("MM/d/yy")
}.format(date)
}
fun getScheduledTimestamp(date: Long): String {
val now = Calendar.getInstance()
val then = Calendar.getInstance()
then.timeInMillis = date
return when {
now.isSameDay(then) -> getFormatter("h:mm a")
now.isSameYear(then) -> getFormatter("MMM d h:mm a")
else -> getFormatter("MMM d yyyy h:mm a")
}.format(date)
}
}