Skip to content

Commit

Permalink
Add DateTimehelper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Sep 20, 2024
1 parent 2bfd782 commit 1f82946
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation(projects.feature.sydneyTrains.database.real)
implementation(projects.feature.sydneyTrains.network.api)
implementation(projects.feature.sydneyTrains.network.real)
implementation(projects.feature.tripPlanner.domain) // TODO not really required remove it later
implementation(projects.feature.tripPlanner.network.api)
implementation(projects.feature.tripPlanner.network.real)

Expand Down
23 changes: 2 additions & 21 deletions app/src/main/java/xyz/ksharma/krail/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.ksharma.krail.design.system.theme.StartTheme
import xyz.ksharma.krail.trip_planner.domain.DateTimeHelper.formatTo12HourTime
import xyz.ksharma.krail.trip_planner.domain.DateTimeHelper.utcToAEST
import xyz.ksharma.krail.trip_planner.network.api.model.StopType
import xyz.ksharma.krail.trip_planner.network.api.repository.TripPlanningRepository
import java.time.ZoneId
Expand Down Expand Up @@ -76,25 +78,4 @@ class MainActivity : ComponentActivity() {
}
}
}

private fun String.utcToAEST(): String {
// Parse the string as a ZonedDateTime in UTC
val utcDateTime = ZonedDateTime.parse(this, DateTimeFormatter.ISO_ZONED_DATE_TIME)

// Convert to AEST time zone (UTC+10)
val aestZoneId = ZoneId.of("Australia/Sydney")
val aestDateTime = utcDateTime.withZoneSameInstant(aestZoneId)
return aestDateTime.format(DateTimeFormatter.ISO_DATE_TIME)
}

fun String.formatTo12HourTime(): String {
// Parse the string as ZonedDateTime
val zonedDateTime = ZonedDateTime.parse(this)

// Define the formatter for 12-hour time with AM/PM
val timeFormatter = DateTimeFormatter.ofPattern("h:mm a")

// Format the ZonedDateTime to 12-hour format
return zonedDateTime.format(timeFormatter)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package xyz.ksharma.krail.trip_planner.domain

import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

object DateTimeHelper {

fun String.formatTo12HourTime(): String {
// Parse the string as ZonedDateTime
val zonedDateTime = ZonedDateTime.parse(this)

// Define the formatter for 12-hour time with AM/PM
val timeFormatter = DateTimeFormatter.ofPattern("h:mm a")

// Format the ZonedDateTime to 12-hour format
return zonedDateTime.format(timeFormatter)
}


fun String.utcToAEST(): String {
// Parse the string as a ZonedDateTime in UTC
val utcDateTime = ZonedDateTime.parse(this, DateTimeFormatter.ISO_ZONED_DATE_TIME)

// Convert to AEST time zone (UTC+10)
val aestZoneId = ZoneId.of("Australia/Sydney")
val aestDateTime = utcDateTime.withZoneSameInstant(aestZoneId)
return aestDateTime.format(DateTimeFormatter.ISO_DATE_TIME)
}
}

0 comments on commit 1f82946

Please sign in to comment.