Skip to content

Instantly share code, notes, and snippets.

@audinue
Created February 9, 2025 17:17
Show Gist options
  • Save audinue/cf44219cbeca2bbc8d026e884ea0871e to your computer and use it in GitHub Desktop.
Save audinue/cf44219cbeca2bbc8d026e884ea0871e to your computer and use it in GitHub Desktop.
fun main() {
println("Now : ${LocalDateTime.now()}")
println("Start of Day : ${LocalDateTime.now().startOfDay()}")
println("End of Day : ${LocalDateTime.now().endOfDay()}")
println("Start of Month: ${LocalDateTime.now().startOfMonth()}")
println("End of Month : ${LocalDateTime.now().endOfMonth()}")
println("Start of Year : ${LocalDateTime.now().startOfYear()}")
println("End of Year : ${LocalDateTime.now().endOfYear()}")
println(
LocalDateTime
.dates(
LocalDateTime.now().startOfMonth(),
LocalDateTime.now().endOfMonth()
).map { it.dayOfMonth }
)
application {
Window(
onCloseRequest = ::exitApplication
) {
val products = (1..1000).toList()
LazyVerticalGrid(
columns = GridCells.Fixed(5)
) {
items(products) { product ->
Column(
) {
Text(
modifier = Modifier
.padding(16.dp),
text = "Product $product",
overflow = TextOverflow.Ellipsis,
softWrap = false,
)
Divider()
}
}
}
}
}
}
fun Instant.toLocalDateTime(): LocalDateTime {
return toLocalDateTime(TimeZone.currentSystemDefault())
}
fun LocalTime.Companion.min(): LocalTime {
return LocalTime(0, 0)
}
fun LocalTime.Companion.max(): LocalTime {
return LocalTime(23, 59, 59, 999_999_999)
}
fun LocalDateTime.Companion.now(): LocalDateTime {
return Clock.System.now().toLocalDateTime()
}
fun LocalDateTime.Companion.dates(start: LocalDateTime, end: LocalDateTime): List<LocalDateTime> {
var dates = mutableListOf<LocalDateTime>()
var now = start
while (now <= end) {
dates.add(now)
now = now.plus(1, DateTimeUnit.DAY)
}
return dates
}
fun LocalDateTime.toInstant(): Instant {
return toInstant(TimeZone.currentSystemDefault())
}
fun LocalDateTime.plus(value: Long, unit: DateTimeUnit): LocalDateTime {
return toInstant()
.plus(value, unit, TimeZone.currentSystemDefault())
.toLocalDateTime()
}
fun LocalDateTime.startOfDay(): LocalDateTime {
return LocalDateTime(date, LocalTime.min())
}
fun LocalDateTime.endOfDay(): LocalDateTime {
return LocalDateTime(date, LocalTime.max())
}
fun LocalDateTime.startOfMonth(): LocalDateTime {
return LocalDateTime(
LocalDate(year, month, 1),
LocalTime.min()
)
}
fun LocalDateTime.endOfMonth(): LocalDateTime {
return plus(1, DateTimeUnit.MONTH)
.startOfMonth()
.plus(-1, DateTimeUnit.DAY)
.endOfDay()
}
fun LocalDateTime.startOfYear(): LocalDateTime {
return LocalDateTime(
LocalDate(year, 1, 1),
LocalTime.min()
)
}
fun LocalDateTime.endOfYear(): LocalDateTime {
return plus(1, DateTimeUnit.YEAR)
.startOfYear()
.plus(-1, DateTimeUnit.DAY)
.endOfDay()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment