DateUtils.kt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.utils
  21. import android.content.res.Resources
  22. import android.icu.text.RelativeDateTimeFormatter
  23. import android.icu.text.RelativeDateTimeFormatter.Direction
  24. import android.icu.text.RelativeDateTimeFormatter.RelativeUnit
  25. import android.os.Build
  26. import com.nextcloud.talk.R
  27. import java.text.DateFormat
  28. import java.util.Calendar
  29. import java.util.Date
  30. import java.util.Locale
  31. import kotlin.math.roundToInt
  32. object DateUtils {
  33. private const val TIMESTAMP_CORRECTION_MULTIPLIER = 1000
  34. fun getLocalDateTimeStringFromTimestamp(timestamp: Long): String {
  35. val cal = Calendar.getInstance()
  36. val tz = cal.timeZone
  37. /* date formatter in local timezone */
  38. val format = DateFormat.getDateTimeInstance(
  39. DateFormat.DEFAULT, DateFormat.SHORT,
  40. Locale.getDefault()
  41. )
  42. format.timeZone = tz
  43. return format.format(Date(timestamp))
  44. }
  45. fun getLocalDateStringFromTimestampForLobby(timestamp: Long): String {
  46. return getLocalDateTimeStringFromTimestamp(timestamp * TIMESTAMP_CORRECTION_MULTIPLIER)
  47. }
  48. fun relativeStartTimeForLobby(timestamp: Long, resources: Resources): String {
  49. return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  50. val fmt = RelativeDateTimeFormatter.getInstance()
  51. val timeLeftMillis = timestamp * TIMESTAMP_CORRECTION_MULTIPLIER - System.currentTimeMillis()
  52. val minutes = timeLeftMillis.toDouble() / DateConstants.SECOND_DIVIDER / DateConstants.MINUTES_DIVIDER
  53. val hours = minutes / DateConstants.HOURS_DIVIDER
  54. val days = hours / DateConstants.DAYS_DIVIDER
  55. val minutesInt = minutes.roundToInt()
  56. val hoursInt = hours.roundToInt()
  57. val daysInt = days.roundToInt()
  58. when {
  59. daysInt > 0 -> {
  60. fmt.format(
  61. daysInt.toDouble(),
  62. Direction.NEXT,
  63. RelativeUnit.DAYS
  64. )
  65. }
  66. hoursInt > 0 -> {
  67. fmt.format(
  68. hoursInt.toDouble(),
  69. Direction.NEXT,
  70. RelativeUnit.HOURS
  71. )
  72. }
  73. minutesInt > 1 -> {
  74. fmt.format(
  75. minutesInt.toDouble(),
  76. Direction.NEXT,
  77. RelativeUnit.MINUTES
  78. )
  79. }
  80. else -> {
  81. resources.getString(R.string.nc_lobby_start_soon)
  82. }
  83. }
  84. } else {
  85. ""
  86. }
  87. }
  88. }