Skip to content

Commit e7e07ce

Browse files
Fixed Date Picker Evaluation of Timestamps (#341)
Co-authored-by: Paul Griffith <pgriffith@inductiveautomation.com>
1 parent cb3e318 commit e7e07ce

1 file changed

Lines changed: 36 additions & 20 deletions

File tree

  • src/main/kotlin/io/github/inductiveautomation/kindling/log

src/main/kotlin/io/github/inductiveautomation/kindling/log/TimePanel.kt

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ internal class TimePanel<T : LogEvent>(
207207
endSelector.addPropertyChangeListener("time") {
208208
updateCoveredRange()
209209
}
210+
211+
Timezone.Default.addChangeListener {
212+
lowerBound = data.minOf { it.timestamp }
213+
upperBound = data.maxOf { it.timestamp }
214+
totalCurrentRange = lowerBound..upperBound
215+
startSelector.range = totalCurrentRange
216+
endSelector.range = totalCurrentRange
217+
reset()
218+
}
210219
}
211220

212221
override fun isFilterApplied(): Boolean = coveredRange != totalCurrentRange
@@ -284,26 +293,33 @@ internal class TimePanel<T : LogEvent>(
284293
}
285294
}
286295

287-
private var JXDatePicker.localDate: LocalDate?
288-
get() = date?.toInstant()?.let { LocalDate.ofInstant(it, Timezone.Default.zoneId) }
289-
set(value) {
290-
date =
291-
value?.atStartOfDay()
292-
?.atOffset(Timezone.Default.zoneId.rules.getOffset(value.atStartOfDay()))
293-
?.toInstant()
294-
.let(Date::from)
295-
}
296+
private fun ZonedDateTime.toDate(): Date = toLocalDate()
297+
.atStartOfDay(zone)
298+
.toInstant()
299+
.let(Date::from)
300+
301+
private fun JXDatePicker.getLocalDate(): LocalDate? = date?.toInstant()
302+
?.let { LocalDate.ofInstant(it, timeZone.toZoneId()) }
303+
304+
private fun JXDatePicker.setDate(zonedDateTime: ZonedDateTime) {
305+
timeZone = java.util.TimeZone.getTimeZone(zonedDateTime.zone)
306+
date = zonedDateTime.toDate()
307+
}
296308

297-
class DateTimeSelector(
309+
private class DateTimeSelector(
298310
var defaultValue: Instant,
299311
initialRange: ClosedRange<Instant>,
300312
) : JPanel(MigLayout("ins 0")) {
301313
var range: ClosedRange<Instant> = initialRange
302314
set(value) {
303315
field = value
304316
datePicker.monthView.apply {
305-
lowerBound = Date.from(value.start)
306-
upperBound = Date.from(value.endInclusive)
317+
lowerBound = value.start
318+
.atZone(Timezone.Default.zoneId)
319+
.toDate()
320+
upperBound = value.endInclusive
321+
.atZone(Timezone.Default.zoneId)
322+
.toDate()
307323
}
308324
}
309325

@@ -312,20 +328,20 @@ class DateTimeSelector(
312328

313329
private val datePicker =
314330
JXDatePicker().apply {
315-
localDate = initialZonedTime.toLocalDate()
331+
setDate(initialZonedTime)
316332
editor.horizontalAlignment = SwingConstants.CENTER
317333
monthView.apply {
318334
// adjust calendar from java.time to java.util weekday numbering
319335
firstDayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek.value % 7 + 1
320336

321-
lowerBound = Date.from(range.start)
322-
upperBound = Date.from(range.endInclusive)
337+
lowerBound = range.start.atZone(Timezone.Default.zoneId).toDate()
338+
upperBound = range.endInclusive.atZone(Timezone.Default.zoneId).toDate()
323339
}
324340
linkPanel = JPanel() // can't be null or BasicDatePickerUI throws an NPE on theme change
325341

326342
addActionListener {
327343
if (date == null) { // out of range selection sets null in JXDatePicker - we'll be nicer and reset
328-
localDate = initialZonedTime.toLocalDate()
344+
setDate(initialZonedTime)
329345
} else {
330346
firePropertyChange("time", null, time)
331347
}
@@ -341,20 +357,20 @@ class DateTimeSelector(
341357

342358
var time: Instant
343359
get() {
344-
val localDate = datePicker.localDate
360+
val localDate = datePicker.getLocalDate()
345361
return if (localDate == null) {
346362
defaultValue
347363
} else {
348364
ZonedDateTime.of(
349365
localDate,
350366
timeSelector.localTime,
351367
Timezone.Default.zoneId,
352-
).toInstant() ?: defaultValue
368+
).toInstant()
353369
}
354370
}
355371
set(value) {
356372
val zonedDateTime = value.atZone(Timezone.Default.zoneId)
357-
datePicker.localDate = zonedDateTime.toLocalDate()
373+
datePicker.setDate(zonedDateTime)
358374
timeSelector.localTime = zonedDateTime.toLocalTime()
359375
}
360376

@@ -387,7 +403,7 @@ class DateTimeSelector(
387403
}
388404
}
389405

390-
class TimeSelector : JPanel(MigLayout("fill, ins 0")) {
406+
private class TimeSelector : JPanel(MigLayout("fill, ins 0")) {
391407
private val hourSelector = timePartSpinner(HOUR_OF_DAY, "00", 10)
392408
private val minuteSelector = timePartSpinner(MINUTE_OF_HOUR, ":00", 6)
393409
private val secondSelector = timePartSpinner(SECOND_OF_MINUTE, ":00", 6)

0 commit comments

Comments
 (0)