Skip to content

Commit

Permalink
Merge pull request #477 from eikek/parse-with-tz
Browse files Browse the repository at this point in the history
Support parsing zone offsets
  • Loading branch information
mergify[bot] authored Sep 2, 2023
2 parents 0aa41b1 + 8a7ca88 commit fc55781
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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

0 comments on commit fc55781

Please sign in to comment.