Jean Tuffier  [11:15 AM] Is there a way to convert...
# kotlinx-datetime
j
Jean Tuffier  [11:15 AM] Is there a way to convert a string from utc time zone like this 
2021-11-29T23:00:00.000+0000
 to it’s equivalent in another time zone? I tried this :
Copy code
internal fun String.localDateTime(): LocalDateTime = Instant.parseWithBasicOffset(this) //parseWithBasicOffset is a custom parse due to time zone written without ":" 
    .toLocalDateTime(TimeZone.currentSystemDefault())
but
myString.localDateTime().toString()
returns
2021-11-30T00:00
the date is correct but I lost the time zone there, which I need for further processing. So my question is : how do I go from
2021-11-29T23:00:00.000+0000
to
2021-11-30T00:00:00.000+0100
(or eventually
+01:00
)
d
A
LocalDateTime
is inherently without time zone, because it literally just points to a date and time. You can combine it with a timezone to get an
Instant
back, but that will just result in the instant you started with.
At the moment I don't believe there's any date formatting in the library yet, besides the default
Instant.toString()
. But you could create the string yourself using
TimeZone.offsetAt()
.
i
@jean What operations do you need to do for that further processing? The idea of kotlinx-datetime library is that you operate mostly with Instants and then use
toLocalDateTime()
when you need to get their representation in a particular time zone or offset.
j
I needed to compare electricity prices over several days or months. To do so, I got a list of pair
date -> consumptions
per predefined period for each compared days/months. After that I was trying to convert utc dates to local zone then to string to be able to group them by relevant criteria (value of the day if days we compared, or value of the month if months were compared). Since the month or day value of the first day of the month depends on the timezone when converted to a string (issue I posted originally) I was trying to convert it to the local value string. But I figured out I could simply use the Instant string without timezone to do so. All in all, I did not need the local string with the timezone value 🙂