That fuck shit the fascists are using
at master 118 lines 3.4 kB view raw
1package org.tm.archive.util 2 3import android.content.Context 4import android.os.Build 5import android.text.format.DateFormat 6import java.time.DayOfWeek 7import java.time.Instant 8import java.time.LocalDate 9import java.time.LocalDateTime 10import java.time.LocalTime 11import java.time.OffsetDateTime 12import java.time.ZoneId 13import java.time.ZoneOffset 14import java.time.ZonedDateTime 15import java.time.format.DateTimeFormatter 16import java.time.format.FormatStyle 17import java.time.temporal.WeekFields 18import java.util.Locale 19import java.util.concurrent.TimeUnit 20 21/** 22 * Given a [ZoneId] return the time offset as a [ZoneOffset]. 23 */ 24fun ZoneId.toOffset(): ZoneOffset { 25 return OffsetDateTime.now(this).offset 26} 27 28/** 29 * Convert [LocalDateTime] to be same as [System.currentTimeMillis] 30 */ 31@JvmOverloads 32fun LocalDateTime.toMillis(zoneOffset: ZoneOffset = ZoneId.systemDefault().toOffset()): Long { 33 return TimeUnit.SECONDS.toMillis(toEpochSecond(zoneOffset)) 34} 35 36/** 37 * Convert [ZonedDateTime] to be same as [System.currentTimeMillis] 38 */ 39fun ZonedDateTime.toMillis(): Long { 40 return TimeUnit.SECONDS.toMillis(toEpochSecond()) 41} 42 43/** 44 * Convert [LocalDateTime] to a [ZonedDateTime] at the UTC offset 45 */ 46fun LocalDateTime.atUTC(): ZonedDateTime { 47 return atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC)) 48} 49 50/** 51 * Create a LocalDateTime with the same year, month, and day, but set 52 * to midnight. 53 */ 54fun LocalDateTime.atMidnight(): LocalDateTime { 55 return LocalDateTime.of(year, month, dayOfMonth, 0, 0) 56} 57 58/** 59 * Return true if the [LocalDateTime] is within [start] and [end] inclusive. 60 */ 61fun LocalDateTime.isBetween(start: LocalDateTime, end: LocalDateTime): Boolean { 62 return (isEqual(start) || isAfter(start)) && (isEqual(end) || isBefore(end)) 63} 64 65/** 66 * Convert milliseconds to local date time with provided [zoneId]. 67 */ 68fun Long.toLocalDateTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalDateTime { 69 return LocalDateTime.ofInstant(Instant.ofEpochMilli(this), zoneId) 70} 71 72/** 73 * Convert milliseconds to local date with provided [zoneId]. 74 */ 75fun Long.toLocalDate(zoneId: ZoneId = ZoneId.systemDefault()): LocalDate { 76 return Instant.ofEpochMilli(this).atZone(zoneId).toLocalDate() 77} 78 79/** 80 * Convert milliseconds to local date time with provided [zoneId]. 81 */ 82fun Instant.toLocalDateTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalDateTime { 83 return LocalDateTime.ofInstant(this, zoneId) 84} 85 86/** 87 * Converts milliseconds to local time with provided [zoneId]. 88 */ 89fun Long.toLocalTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalTime { 90 return LocalDateTime.ofInstant(Instant.ofEpochMilli(this), zoneId).toLocalTime() 91} 92 93/** 94 * Formats [LocalTime] as localized time. For example, "8:00 AM" 95 */ 96fun LocalTime.formatHours(context: Context): String { 97 return if (Build.VERSION.SDK_INT >= 26 || !DateFormat.is24HourFormat(context)) { 98 DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this) 99 } else { 100 DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()).format(this) 101 } 102} 103 104/** 105 * Get the days of the week in order based on [Locale]. 106 */ 107fun Locale.orderOfDaysInWeek(): List<DayOfWeek> { 108 val firstDayOfWeek: DayOfWeek = WeekFields.of(this).firstDayOfWeek 109 return listOf( 110 firstDayOfWeek, 111 firstDayOfWeek.plus(1), 112 firstDayOfWeek.plus(2), 113 firstDayOfWeek.plus(3), 114 firstDayOfWeek.plus(4), 115 firstDayOfWeek.plus(5), 116 firstDayOfWeek.plus(6) 117 ) 118}