Use system timezone

This commit is contained in:
2022-03-18 23:53:02 +01:00
parent ea2349759c
commit 0494a06e9c
2 changed files with 44 additions and 42 deletions

View File

@@ -5,8 +5,8 @@ import eu.fosil.okupamicoche.usecase.travel.createTravel
import mu.KotlinLogging
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
private val logger = KotlinLogging.logger {}
@@ -51,8 +51,8 @@ object CommandParser {
val date = args[2]
val time = args[3]
val formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm")
val unixTime = LocalDateTime.from(formatter.parse("$date $time")).toEpochSecond(ZoneOffset.UTC) * 1000
val formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm").withZone(ZoneId.systemDefault())
val unixTime = Instant.from(formatter.parse("$date $time")).toEpochMilli()
val travelOptions = TravelOptions(
from = args[0],
@@ -67,4 +67,4 @@ object CommandParser {
}
}
}

View File

@@ -15,12 +15,16 @@ import net.folivo.trixnity.core.model.UserId
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
private val logger = KotlinLogging.logger {}
data class RoomAliasAndName(
val aliasId: RoomAliasId,
val name: String
)
suspend fun createTravel(
travelOptions: TravelOptions,
driver: UserId,
@@ -42,24 +46,38 @@ suspend fun createTravel(
}
}
private suspend fun getRoomAlias(travelOptions: TravelOptions): RoomAliasId {
private suspend fun createRoom(travelOptions: TravelOptions, driver: UserId): RoomId {
requireNotNull(matrixApiClient)
val newRoomAliasAndName = getRoomAliasAndName(travelOptions)
return matrixApiClient.rooms.createRoom(
visibility = Visibility.PUBLIC,
roomAliasId = newRoomAliasAndName.aliasId,
name = newRoomAliasAndName.name,
topic = travelOptions.description,
invite = setOf(driver)
).getOrThrow()
}
private suspend fun getRoomAliasAndName(travelOptions: TravelOptions): RoomAliasAndName {
val roomAliasPrefix = getRoomAliasPrefix(travelOptions)
logger.info { "roomAliasPrefix=$roomAliasPrefix" }
var i = 0
var attempt = 0
// Look for available room alias
while (!isRoomAliasAvailable(getRoomAliasAttempt(roomAliasPrefix, i)))
i++
while (!isRoomAliasAvailable(getRoomAliasAttempt(roomAliasPrefix, attempt)))
attempt++
logger.info { "getRoomAlias=${getRoomAliasAttempt(roomAliasPrefix, i)}" }
return getRoomAliasAttempt(roomAliasPrefix, i)
logger.info { "getRoomAlias=${getRoomAliasAttempt(roomAliasPrefix, attempt)}" }
return RoomAliasAndName(
getRoomAliasAttempt(roomAliasPrefix, attempt),
getRoomName(travelOptions, attempt)
)
}
private fun getRoomAliasPrefix(travelOptions: TravelOptions): String {
val timezone = ZoneId.of("UTC")
// val timezone = ZoneId.of("Europe/Paris")
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travelOptions.time), timezone)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime)
val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime).replace(':', 'H')
val instant = Instant.ofEpochMilli(travelOptions.time)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").withZone(ZoneId.systemDefault()).format(instant)
val time = DateTimeFormatter.ofPattern("H:mm").withZone(ZoneId.systemDefault()).format(instant).replace(':', 'H')
return travelOptions.run {
"#viaje_${from}-${to}_${date}_${time}"
@@ -82,42 +100,26 @@ private suspend fun isRoomAliasAvailable(roomAliasId: RoomAliasId): Boolean {
return roomId == null
}
private fun getRoomName(travelOptions: TravelOptions): String {
val timezone = ZoneId.of("UTC")
// val timezone = ZoneId.of("Europe/Paris")
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travelOptions.time), timezone)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime)
val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime)
private fun getRoomName(travelOptions: TravelOptions, attempt: Int): String {
val instant = Instant.ofEpochMilli(travelOptions.time)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").withZone(ZoneId.systemDefault()).format(instant)
val time = DateTimeFormatter.ofPattern("H:mm").withZone(ZoneId.systemDefault()).format(instant)
val attemptSuffix = if (attempt > 0) " ($attempt)" else null
return travelOptions.run {
"Viaje ${from}-${to} $date $time | $places places available"
"Viaje ${from}-${to} $date $time | $places places available" + attemptSuffix
}
}
private suspend fun createRoom(travelOptions: TravelOptions, driver: UserId): RoomId {
requireNotNull(matrixApiClient)
val newRoomAliasId = getRoomAlias(travelOptions)
return matrixApiClient.rooms.createRoom(
visibility = Visibility.PUBLIC,
roomAliasId = newRoomAliasId,
name = getRoomName(travelOptions),
topic = travelOptions.description,
invite = setOf(driver)
).getOrThrow()
}
private suspend fun sendCreateEvents(
travel: Travel,
roomId: RoomId
) {
requireNotNull(matrixApiClient)
val timezone = ZoneId.of("UTC")
// val timezone = ZoneId.of("Europe/Paris")
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travel.options.time), timezone)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime)
val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime)
val instant = Instant.ofEpochMilli(travel.options.time)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").withZone(ZoneId.systemDefault()).format(instant)
val time = DateTimeFormatter.ofPattern("H:mm").withZone(ZoneId.systemDefault()).format(instant)
// Send text message
val displayName = matrixApiClient.users.getDisplayName(travel.driver).getOrNull() ?: travel.driver.full