Format utc time in text locations

This commit is contained in:
Chumva 2019-12-09 17:33:20 +02:00
parent fba1f9e28e
commit b15463488e
3 changed files with 55 additions and 89 deletions

View file

@ -1077,14 +1077,15 @@ class TelegramSettings(private val app: TelegramApplication) {
R.drawable.ic_world_globe_dark, R.string.time_zone, R.drawable.ic_world_globe_dark, R.string.time_zone,
R.string.time_zone_descr R.string.time_zone_descr
) { ) {
private val formattedUtcOffsets = DataConstants.utcOffsets.keys.toList()
override fun getCurrentValue() = utcOffset override fun getCurrentValue() = utcOffset
override fun setCurrentValue(index: Int) { override fun setCurrentValue(index: Int) {
utcOffset = DataConstants.utcOffsets[index] utcOffset = formattedUtcOffsets[index]
} }
override fun getMenuItems() = DataConstants.utcOffsets override fun getMenuItems() = formattedUtcOffsets
} }
abstract inner class ListPreference( abstract inner class ListPreference(

View file

@ -47,45 +47,45 @@ object DataConstants {
"UK" to "+44", "US" to "+1","UY" to "+598", "VI" to "+1340", "UZ" to "+998", "VU" to "+678", "VE" to "+58", "UK" to "+44", "US" to "+1","UY" to "+598", "VI" to "+1340", "UZ" to "+998", "VU" to "+678", "VE" to "+58",
"VA" to "+3906698,+379", "VN" to "+84", "WF" to "+681","YE" to "+967", "ZM" to "+260", "ZW" to "+263") "VA" to "+3906698,+379", "VN" to "+84", "WF" to "+681","YE" to "+967", "ZM" to "+260", "ZW" to "+263")
val utcOffsets = listOf( val utcOffsets = mapOf(
"$UTC_FORMAT12", "$UTC_FORMAT-12" to -12f,
"$UTC_FORMAT11", "$UTC_FORMAT-11" to -11f,
"$UTC_FORMAT10", "$UTC_FORMAT-10" to -10f,
"$UTC_FORMAT09:30", "$UTC_FORMAT-09:30" to -9.5f,
"$UTC_FORMAT9", "$UTC_FORMAT-9" to -9f,
"$UTC_FORMAT8", "$UTC_FORMAT-8" to -8f,
"$UTC_FORMAT7", "$UTC_FORMAT-7" to -7f,
"$UTC_FORMAT6", "$UTC_FORMAT-6" to -6f,
"$UTC_FORMAT5", "$UTC_FORMAT-5" to -5f,
"$UTC_FORMAT4", "$UTC_FORMAT-4" to -4f,
"$UTC_FORMAT03:30", "$UTC_FORMAT-03:30" to -3.5f,
"$UTC_FORMAT3", "$UTC_FORMAT-3" to -3f,
"$UTC_FORMAT2", "$UTC_FORMAT-2" to -2f,
"$UTC_FORMAT1", "$UTC_FORMAT-1" to -1f,
UTC_FORMAT, UTC_FORMAT to 0f,
"$UTC_FORMAT+1", "$UTC_FORMAT+1" to 1f,
"$UTC_FORMAT+2", "$UTC_FORMAT+2" to 2f,
"$UTC_FORMAT+3", "$UTC_FORMAT+3" to 3f,
"$UTC_FORMAT+03:30", "$UTC_FORMAT+03:30" to 3.5f,
"$UTC_FORMAT+4", "$UTC_FORMAT+4" to 4f,
"$UTC_FORMAT+04:30", "$UTC_FORMAT+04:30" to 4.5f,
"$UTC_FORMAT+5", "$UTC_FORMAT+5" to 5f,
"$UTC_FORMAT+05:30", "$UTC_FORMAT+05:30" to 5.5f,
"$UTC_FORMAT+05:45", "$UTC_FORMAT+05:45" to 5.75f,
"$UTC_FORMAT+6", "$UTC_FORMAT+6" to 6f,
"$UTC_FORMAT+06:30", "$UTC_FORMAT+06:30" to 6.5f,
"$UTC_FORMAT+7", "$UTC_FORMAT+7" to 7f,
"$UTC_FORMAT+8", "$UTC_FORMAT+8" to 8f,
"$UTC_FORMAT+08:45", "$UTC_FORMAT+08:45" to 8.75f,
"$UTC_FORMAT+9", "$UTC_FORMAT+9" to 9f,
"$UTC_FORMAT+09:30", "$UTC_FORMAT+09:30" to 9.5f,
"$UTC_FORMAT+10", "$UTC_FORMAT+10" to 10f,
"$UTC_FORMAT+10:30", "$UTC_FORMAT+10:30" to 10.5f,
"$UTC_FORMAT+11", "$UTC_FORMAT+11" to 11f,
"$UTC_FORMAT+12", "$UTC_FORMAT+12" to 12f,
"$UTC_FORMAT+12:45", "$UTC_FORMAT+12:45" to 12.75f,
"$UTC_FORMAT+13", "$UTC_FORMAT+13" to 13f,
"$UTC_FORMAT+13:45", "$UTC_FORMAT+13:45" to 13.75f,
"$UTC_FORMAT+14" "$UTC_FORMAT+14" to 14f
) )
} }

View file

@ -163,9 +163,17 @@ object OsmandLocationUtils {
return String.format(Locale.US, "%.5f, %.5f", sig.lat, sig.lon) return String.format(Locale.US, "%.5f, %.5f", sig.lat, sig.lon)
} }
fun formatFullTime(ti: Long): String { fun formatFullTime(ti: Long, app: TelegramApplication): String {
val dt = Date(ti) val dt = Date(ti)
return UTC_DATE_FORMAT.format(dt) + " " + UTC_TIME_FORMAT.format(dt) + UTC_FORMAT_SUFFIX val offsetKey = app.settings.utcOffset
val utcOffset = DataConstants.utcOffsets[offsetKey] ?: 0f
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
simpleDateFormat.timeZone = TimeZone.getTimeZone(DataConstants.UTC_FORMAT).apply {
rawOffset = (utcOffset * 60 * 60 * 1000).toInt()
}
return "${simpleDateFormat.format(dt)} $offsetKey"
} }
fun parseOsmAndBotLocationContent(oldContent: MessageOsmAndBotLocation, content: TdApi.MessageContent): MessageOsmAndBotLocation { fun parseOsmAndBotLocationContent(oldContent: MessageOsmAndBotLocation, content: TdApi.MessageContent): MessageOsmAndBotLocation {
@ -329,49 +337,6 @@ object OsmandLocationUtils {
return 0 return 0
} }
fun getTextMessageContent(updateId: Int, location: LocationMessage): TdApi.InputMessageText {
val entities = mutableListOf<TdApi.TextEntity>()
val builder = StringBuilder()
val locationMessage = formatLocation(location)
val firstSpace = USER_TEXT_LOCATION_TITLE.indexOf(' ')
val secondSpace = USER_TEXT_LOCATION_TITLE.indexOf(' ', firstSpace + 1)
entities.add(TdApi.TextEntity(builder.length + firstSpace + 1, secondSpace - firstSpace, TdApi.TextEntityTypeTextUrl(SHARING_LINK)))
builder.append("$USER_TEXT_LOCATION_TITLE\n")
entities.add(TdApi.TextEntity(builder.lastIndex, LOCATION_PREFIX.length, TdApi.TextEntityTypeBold()))
builder.append(LOCATION_PREFIX)
entities.add(TdApi.TextEntity(builder.length, locationMessage.length,
TdApi.TextEntityTypeTextUrl("$BASE_SHARING_URL?lat=${location.lat}&lon=${location.lon}")))
builder.append("$locationMessage\n")
if (location.altitude != 0.0) {
entities.add(TdApi.TextEntity(builder.lastIndex, ALTITUDE_PREFIX.length, TdApi.TextEntityTypeBold()))
builder.append(String.format(Locale.US, "$ALTITUDE_PREFIX%.1f m\n", location.altitude))
}
if (location.speed > 0) {
entities.add(TdApi.TextEntity(builder.lastIndex, SPEED_PREFIX.length, TdApi.TextEntityTypeBold()))
builder.append(String.format(Locale.US, "$SPEED_PREFIX%.1f m/s\n", location.speed))
}
if (location.bearing > 0) {
entities.add(TdApi.TextEntity(builder.lastIndex, BEARING_PREFIX.length, TdApi.TextEntityTypeBold()))
builder.append(String.format(Locale.US, "$BEARING_PREFIX%.1f$BEARING_SUFFIX\n", location.bearing))
}
if (location.hdop != 0.0 && location.speed == 0.0) {
entities.add(TdApi.TextEntity(builder.lastIndex, HDOP_PREFIX.length, TdApi.TextEntityTypeBold()))
builder.append(String.format(Locale.US, "$HDOP_PREFIX%d m\n", location.hdop.toInt()))
}
if (updateId == 0) {
builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time)))
} else {
builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time), updateId))
}
val textMessage = builder.toString().trim()
return TdApi.InputMessageText(TdApi.FormattedText(textMessage, entities.toTypedArray()), true, true)
}
fun getTextMessageContent(updateId: Int, location: BufferMessage, app: TelegramApplication): TdApi.InputMessageText { fun getTextMessageContent(updateId: Int, location: BufferMessage, app: TelegramApplication): TdApi.InputMessageText {
val entities = mutableListOf<TdApi.TextEntity>() val entities = mutableListOf<TdApi.TextEntity>()
val builder = StringBuilder() val builder = StringBuilder()
@ -409,9 +374,9 @@ object OsmandLocationUtils {
builder.append(String.format(Locale.US, "$HDOP_PREFIX%s\n", formattedHdop)) builder.append(String.format(Locale.US, "$HDOP_PREFIX%s\n", formattedHdop))
} }
if (updateId == 0) { if (updateId == 0) {
builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time))) builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time, app)))
} else { } else {
builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time), updateId)) builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time, app), updateId))
} }
val textMessage = builder.toString().trim() val textMessage = builder.toString().trim()