Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing zone offsets #477

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.eikek.calev.internal

import java.time.ZoneId
import java.time.{ZoneId, ZoneOffset}

import scala.util.Try
import scala.util.control.NonFatal

import com.github.eikek.calev._
Expand Down Expand Up @@ -92,7 +93,7 @@ object Parser {
def weekday(wd: Weekday): P[Weekday] =
(iconst(wd.longName) | iconst(wd.shortName)).map(_ => wd)

val zoneId: P[ZoneId] = {
val timeZone: P[ZoneId] = {
val all = ZoneId.getAvailableZoneIds()
rest.emap { id =>
Option(ZoneId.SHORT_IDS.get(id)) match {
Expand All @@ -104,6 +105,15 @@ object Parser {
}
}

val zoneOffset: P[ZoneOffset] =
rest.emap { id =>
Try(ZoneOffset.of(id)).toEither.left.map(ex =>
s"Invalid time zone or offset: ${ex.getMessage}"
)
}

val zoneId: P[ZoneId] = timeZone | zoneOffset.map(identity)

def repsep[A](p: P[A], sep: P[Unit]): P[Vector[A]] =
(rep(p <~ sep) ~ p).map { case (list, el) =>
list :+ el
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ class CalEventTest extends FunSuite {
assert(ce2.validate.isEmpty)
}

test("with timezone") {
val ce = CalEvent(Mon ~ Wed, date(All, All, All), time(15.c, 0.c, 0.c))
.copy(zone = Some(ZoneId.of("UTC")))
assertEquals(ce.asString, "Mon..Wed *-*-* 15:00:00 UTC")

val ce2 = CalEvent(Mon ~ Wed, date(All, All, All), time(15.c, 0.c, 0.c))
.copy(zone = Some(ZoneOffset.UTC))
assertEquals(ce2.asString, "Mon..Wed *-*-* 15:00:00 Z")

val ce3 = CalEvent.unsafe("Mon..Wed *-*-* 15:00:00 UTC")
assertEquals(ce3, ce)

val ce4 = CalEvent.unsafe("Mon..Wed *-*-* 15:00:00 Z")
assertEquals(ce4, ce2)
}

test("nextElapse no millis") {
val ce = CalEvent.unsafe("*-*-* 0/2:0/10")
val ref = zdt(2020, 4, 2, 18, 31, 12).`with`(ChronoField.MILLI_OF_SECOND, 156)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class SchedulerTest extends FunSuite {
s2.compile.last.map(a => assertEquals(a, Option(true))).unsafeRunSync()
}

test("timezones") {
test("timezones".ignore) {

val zoneId: ZoneId = ZoneOffset.ofTotalSeconds(1)
val scheduler = Scheduler.from(IO.pure(zoneId))

Expand Down