-
-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathDateUtil.kt
180 lines (148 loc) · 7.75 KB
/
DateUtil.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.wikipedia.util
import android.content.Context
import android.icu.text.RelativeDateTimeFormatter
import android.os.Build
import android.text.format.DateFormat
import org.wikipedia.R
import org.wikipedia.WikipediaApp
import org.wikipedia.feed.model.UtcDate
import java.text.SimpleDateFormat
import java.time.*
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.TemporalAccessor
import java.util.*
import java.util.concurrent.ConcurrentHashMap
object DateUtil {
private val DATE_FORMATS = ConcurrentHashMap<String, SimpleDateFormat>()
private val DATE_TIME_FORMATTERS = ConcurrentHashMap<String, DateTimeFormatter>()
// TODO: Switch to DateTimeFormatter when minSdk = 26.
fun iso8601DateParse(date: String): Date {
return Date.from(Instant.parse(date))
}
fun iso8601LocalDateTimeParse(timestamp: String): LocalDateTime {
return LocalDateTime.ofInstant(Instant.parse(timestamp), ZoneId.systemDefault())
}
fun iso8601LocalDateParse(timestamp: String): LocalDate {
return LocalDate.ofInstant(Instant.parse(timestamp), ZoneId.systemDefault())
}
fun dbDateFormat(date: Date): String {
return getCachedDateFormat("yyyyMMddHHmmss", Locale.ROOT, true).format(date)
}
fun dbDateParse(date: String): Date {
return getCachedDateFormat("yyyyMMddHHmmss", Locale.ROOT, true).parse(date)!!
}
fun getFeedCardDateString(age: Int): String {
return getShortDateString(UtcDate(age).baseCalendar.time)
}
fun getFeedCardShortDateString(date: Calendar): String {
return getExtraShortDateString(date.time)
}
fun getMDYDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MM/dd/yyyy")
}
fun getMonthOnlyDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMMM d")
}
fun getMonthOnlyWithoutDayDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMMM")
}
fun getYearOnlyDateString(date: LocalDate): String {
return getDateStringWithSkeletonPattern(date, "yyyy")
}
fun getYMDDateString(date: Date): String {
return getCachedDateFormat("yyyyMMdd", Locale.ROOT, true).format(date)
}
private fun getExtraShortDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMM d")
}
fun getTimeString(context: Context, date: Date): String {
val datePattern = if (DateFormat.is24HourFormat(context)) "HH:mm" else "hh:mm a"
return getDateStringWithSkeletonPattern(date, datePattern)
}
fun getTimeString(context: Context, localDateTime: LocalDateTime): String {
val datePattern = if (DateFormat.is24HourFormat(context)) "HH:mm" else "hh:mm a"
return getDateStringWithSkeletonPattern(localDateTime, datePattern)
}
fun getShortDayWithTimeString(dateStr: String): String {
return getDateStringWithSkeletonPattern(iso8601DateParse(dateStr), "MMM d HH:mm")
}
fun getShortDayWithTimeString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MM/dd/yyyy HH:mm")
}
fun getTimeAndDateString(context: Context, date: Date): String {
val datePattern = if (DateFormat.is24HourFormat(context)) "HH:mm, MMM d, yyyy" else "hh:mm a, MMM d, yyyy"
return getDateStringWithSkeletonPattern(date, datePattern)
}
fun getTimeAndDateString(context: Context, dateStr: String): String {
val datePattern = if (DateFormat.is24HourFormat(context)) "HH:mm, MMM d, yyyy" else "hh:mm a, MMM d, yyyy"
return getDateStringWithSkeletonPattern(iso8601DateParse(dateStr), datePattern)
}
fun getDateAndTime(context: Context, date: Date): String {
val datePattern = if (DateFormat.is24HourFormat(context)) "MMM d, yyyy, HH:mm" else "MMM d, yyyy, hh:mm a"
return getCachedDateFormat(datePattern, Locale.getDefault(), false).format(date)
}
private fun getDateStringWithSkeletonPattern(date: Date, pattern: String): String {
return getCachedDateFormat(pattern, Locale.getDefault(), utc = false, skeleton = true)
.format(date)
}
private fun getDateStringWithSkeletonPattern(temporalAccessor: TemporalAccessor, pattern: String): String {
return getCachedDateTimeFormatter(pattern, Locale.getDefault(), utc = false, skeleton = true)
.format(temporalAccessor)
}
private fun getCachedDateFormat(pattern: String, locale: Locale, utc: Boolean, skeleton: Boolean = false): SimpleDateFormat {
return DATE_FORMATS.getOrPut(pattern) {
val df = SimpleDateFormat(if (skeleton) DateFormat.getBestDateTimePattern(locale, pattern) else pattern, locale)
if (utc) {
df.timeZone = TimeZone.getTimeZone("UTC")
}
df
}
}
private fun getCachedDateTimeFormatter(pattern: String, locale: Locale, utc: Boolean, skeleton: Boolean = false): DateTimeFormatter {
return DATE_TIME_FORMATTERS.getOrPut(pattern) {
val dtf = DateTimeFormatter.ofPattern(if (skeleton) DateFormat.getBestDateTimePattern(locale, pattern) else pattern, locale)
return if (utc) dtf.withZone(ZoneOffset.UTC) else dtf
}
}
fun getShortDateString(date: Date): String {
// todo: consider allowing TWN date formats. It would be useful to have but might be
// difficult for translators to write correct format specifiers without being able to
// test them. We should investigate localization support in date libraries such as
// Joda-Time and how TWN solves this classic problem.
val dateFormat = DateFormat.getMediumDateFormat(WikipediaApp.instance)
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
return dateFormat.format(date)
}
fun getShortDateString(localDate: LocalDate): String {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(localDate)
}
fun getUtcRequestDateFor(age: Int): UtcDate {
return UtcDate(age)
}
fun getDefaultDateFor(age: Int): Calendar {
val calendar = Calendar.getInstance(TimeZone.getDefault())
calendar.add(Calendar.DATE, -age)
return calendar
}
fun yearToStringWithEra(year: Int): String {
val cal: Calendar = GregorianCalendar(year, 1, 1)
return getDateStringWithSkeletonPattern(cal.time, if (year < 0) "y GG" else "y")
}
fun getYearDifferenceString(year: Int, languageCode: String): String {
val diffInYears = Calendar.getInstance()[Calendar.YEAR] - year
val targetResource = L10nUtil.getResourcesForWikiLang(languageCode) ?: WikipediaApp.instance.resources
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val firstMatchLocaleInstance = RelativeDateTimeFormatter.getInstance(targetResource.configuration.locales.getFirstMatch(arrayOf(languageCode)))
when (diffInYears) {
0 -> firstMatchLocaleInstance.format(RelativeDateTimeFormatter.Direction.THIS, RelativeDateTimeFormatter.AbsoluteUnit.YEAR)
1 -> firstMatchLocaleInstance.format(RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.AbsoluteUnit.YEAR)
-1 -> firstMatchLocaleInstance.format(RelativeDateTimeFormatter.Direction.NEXT, RelativeDateTimeFormatter.AbsoluteUnit.YEAR)
else -> firstMatchLocaleInstance.format(diffInYears.toDouble(), RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.RelativeUnit.YEARS)
}
} else {
return if (diffInYears == 0) L10nUtil.getStringForArticleLanguage(languageCode, R.string.this_year)
else targetResource.getQuantityString(R.plurals.diff_years, diffInYears, diffInYears)
}
}
}