Home > Mobile >  Different ZonedDateTime of GMT and BST become same Instant after toInstant conversion in Scala
Different ZonedDateTime of GMT and BST become same Instant after toInstant conversion in Scala

Time:08-04

to summarize my question, I have 2 different ZonedDateTime objects with GMT and BST but same local time. They become the same Instant values after toInstant conversion. I am expecting after conversion, they should be different values because although they are both 'Tue Feb 23 18:00:46 2021', the strings are representing time for different time zones. I am not sure whether this is a bug of library itself or I am doing something wrong.

I have 2 different ZonedDateTime generated from this piece of Scala code:

import ...
import java.util.{Set => JavaSet}
val string2 = "Tue Feb 23 18:00:46 BST 2021" 
val string1 = "Tue Feb 23 18:00:46 GMT 2021" 
val timestampFormat: DateTimeFormatter = new DateTimeFormatterBuilder()
    .appendOptional(DateTimeFormatter.ISO_DATE_TIME)
    .appendOptional(DateTimeFormatter.ISO_INSTANT)
    .appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
    .appendOptional(DateTimeFormatter.ISO_ZONED_DATE_TIME)
    .appendOptional(DateTimeFormatter.RFC_1123_DATE_TIME)
    .appendPattern("EEE MMM ppd HH:mm:ss ")
    .appendZoneText(TextStyle.SHORT, JavaSet.of(ZoneId.of("Europe/London")))
    .appendPattern(" yyyy")
    .toFormatter(Locale.US)

val parsedZdt2: ZonedDateTime = ZonedDateTime.parse(string2, timestampFormat)
val parsedZdt1: ZonedDateTime = ZonedDateTime.parse(string1, timestampFormat)
println(parsedZdt1 == parsedZdt2)
println("parsedZdt1.toString: "   parsedZdt1.toString)
println("parsedZdt2.toString: "   parsedZdt2.toString)

This prints out:

false
parsedZdt1.toString: 2021-02-23T18:00:46Z[GMT]
parsedZdt2.toString: 2021-02-23T18:00:46Z[Europe/London]

However when I convert them to instant:

println(parsedZdt1.toInstant == parsedZdt2.toInstant)
println("parsedZdt1.toInstant.toString: "   parsedZdt1.toInstant.toString)
println("parsedZdt2.toInstant.toString: "   parsedZdt2.toInstant.toString)

I got this printed out:

true
parsedZdt1.toInstant.toString: 2021-02-23T18:00:46Z
parsedZdt2.toInstant.toString: 2021-02-23T18:00:46Z

My questions are

  1. Why do the 2 values become the same after toInstant conversion?
  2. Are the 2 values representing same time even before toInstant, i.e. , are parsedZdt1 and parsedZdt2 representing the same moment?

Thank you!

CodePudding user response:

BST begins at 01:00 GMT every year on the last Sunday of March and ends at 01:00 GMT (02:00 BST) on the last Sunday of October.

In your example - 23rd of Feb represent the same time line even before the conversion. For the 2021 year, to see the 1 hour gap difference, you have to use a date between 28 March - 31 October inclusive, but with the appropriate hour.

E.g.:

  val string2 = "Tue Mar 30 18:00:46 BST 2021"
  val string1 = "Tue Mar 30 18:00:46 GMT 2021"

yields:

parsedZdt1.toString: 2021-03-30T18:00:46Z[GMT]
parsedZdt2.toString: 2021-03-30T18:00:46 01:00[Europe/London]

CodePudding user response:

tl;dr

You said:

the strings are representing time for different time zones

Different time zones, yes, but the time zones happen to share the same offset on that particular date. Many time zones share the same offset, coincidentally, on particular dates.

London time is GMT/UTC time, sometimes, when not observing Daylight Saving Time (DST).

BST = GMT (sometimes)

If by BST, you mean the time zone Europe/London, that zone had an offset of zero hours from UTC/GMT on that date of 2021-02-23.

  • Related