From 21f9e4a250440c216aa365a95a8a225f7b9fdf5f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 27 May 2016 07:24:44 +0200 Subject: [PATCH] Use zoned date time instead of local --- .../rhynodge/webpages/weather/wettercom/WetterComFilter.kt | 6 ++++-- .../rhynodge/webpages/weather/wettercom/WetterComState.kt | 4 ++-- .../rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt | 3 ++- .../rhynodge/webpages/weather/wettercom/WetterComStateTest.kt | 10 +++++----- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt index f9411a5..cf8cd3f 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt @@ -7,6 +7,8 @@ import net.pterodactylus.rhynodge.states.HtmlState import org.jsoup.nodes.Document import org.jsoup.nodes.Element import java.time.LocalDateTime +import java.time.ZoneId.of +import java.time.ZonedDateTime import java.time.format.DateTimeFormatter /** @@ -36,12 +38,12 @@ class WetterComFilter : Filter { return wetterComState } - private fun parseDateTime(document: Document): LocalDateTime? { + private fun parseDateTime(document: Document): ZonedDateTime? { val dateElement = document.select("#furtherDetails .portable-mb h3") .single()?.text()?.split(",")?.get(1)?.trim() ?: return null val timeElement = document.select(".weather-strip--detail .delta.palm-hide") .first()?.text()?.split(" ")?.first() ?: return null - return LocalDateTime.from(dateTimeFormatter.parse("%s %s".format(dateElement, timeElement))) + return LocalDateTime.from(dateTimeFormatter.parse("%s %s".format(dateElement, timeElement))).atZone(of("Europe/Berlin")) } private fun parseHourStates(document: Document): List { diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt index f14337f..1181406 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt @@ -1,14 +1,14 @@ package net.pterodactylus.rhynodge.webpages.weather.wettercom import net.pterodactylus.rhynodge.states.AbstractState -import java.time.LocalDateTime +import java.time.ZonedDateTime /** * Contains the state parsed from [wetter.com](https://www.wetter.com/). * * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) */ -class WetterComState(val dateTime: LocalDateTime) : AbstractState(true) { +class WetterComState(val dateTime: ZonedDateTime) : AbstractState(true) { val hours: List = mutableListOf() diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt index bd951f2..6fc4995 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt @@ -14,6 +14,7 @@ import org.mockito.Mockito.`when` import org.mockito.Mockito.mock import java.time.LocalDateTime import java.time.Month +import java.time.ZoneId /** * Unit test for [WetterComFilter]. @@ -49,7 +50,7 @@ class WetterComFilterTest { val document = loadDocument(javaClass, "wetter.com.html", url) val htmlState = HtmlState(url, document) val newState = filter.filter(htmlState) as WetterComState - assertThat(newState.dateTime, `is`(LocalDateTime.of(2016, Month.MAY, 23, 5, 0))) + assertThat(newState.dateTime, `is`(LocalDateTime.of(2016, Month.MAY, 23, 5, 0).atZone(ZoneId.of("Europe/Berlin")))) assertThat(newState.hours, contains( HourState(0, 15.0, 0.65, 0.8, WindDirection.NORTH, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), HourState(1, 15.0, 0.7, 0.9, WindDirection.NONE, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt index 2b52a77..d230c50 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt @@ -4,8 +4,8 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.`is` import org.junit.Test import java.time.Instant -import java.time.LocalDateTime import java.time.ZoneId +import java.time.ZonedDateTime /** * Unit test for [WetterComState]. @@ -18,8 +18,8 @@ class WetterComStateTest { fun statesWithoutHoursEqualOneAnother() { val now = Instant.now().atZone(ZoneId.of("UTC")) println("%s %s".format(now, now.javaClass)) - val firstState = WetterComState(LocalDateTime.from(now)) - val secondState = WetterComState(LocalDateTime.from(now)) + val firstState = WetterComState(ZonedDateTime.from(now)) + val secondState = WetterComState(ZonedDateTime.from(now)) assertThat(firstState, `is`(secondState)) } @@ -27,10 +27,10 @@ class WetterComStateTest { fun statesWithTheSameHoursAreEqual() { val now = Instant.now().atZone(ZoneId.of("UTC")) println("%s %s".format(now, now.javaClass)) - val firstState = WetterComState(LocalDateTime.from(now)) + val firstState = WetterComState(ZonedDateTime.from(now)) firstState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1")) firstState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2")) - val secondState = WetterComState(LocalDateTime.from(now)) + val secondState = WetterComState(ZonedDateTime.from(now)) secondState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1")) secondState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2")) assertThat(firstState, `is`(secondState)) -- 2.7.4