These are chat archives for nrinaudo/kantan.csv

17th
Oct 2016
Andrew Roberts
@aroberts
Oct 17 2016 14:07
@nrinaudo That would totally solve my case. Here’s my workaround:
  implicit val timestampCellDecoder: CellDecoder[ZonedDateTime] = CellDecoder.from(s => {
    // parse timestamp as epoch seconds/nanos, always try first (eager)
    val epochAttempt = Try {
      val (seconds, nano) = s.split("\\.", 2) match {
        case Array(sec, n) => (sec.toLong, n.toInt)
        case Array(sec) => (sec.toLong, 0)
      }
      LocalDateTime.ofEpochSecond(seconds, nano, ZoneOffset.UTC)
    }

    // parse timestamp using format strings from config, evaluated lazily
    val formattedAttempts = Config.parsing.timestampFormats.iterator.map { f =>
      Try { LocalDateTime.parse(s, DateTimeFormatter.ofPattern(f)) }
    }

    // find the first attempt that results in Success(time)
    val parsed = (Iterator(epochAttempt) ++ formattedAttempts).collectFirst {
      case Success(local) => local.atZone(UTCZoneId)
    }

    Result.fromOption(parsed, TypeError(s"Couldn't decode timestamp: $s"))
  })
Nicolas Rinaudo
@nrinaudo
Oct 17 2016 14:09
cheers @aroberts. I'm busy with my day job right now, but I'll take a look later when I can give it my undivided attention.
Andrew Roberts
@aroberts
Oct 17 2016 14:09
Sounds good!
Nicolas Rinaudo
@nrinaudo
Oct 17 2016 14:09
CellDecoder.oneOf is definitely going to make it to the next version, it's a handy combinator to have
also, I think you could simplify your code a bit by replacing Try {...} by DecodeResult {....atZone(UTCZoneId)} and then collect on that
but oneOf is going to sort you out entirely anyway
Nicolas Rinaudo
@nrinaudo
Oct 17 2016 19:32
@aroberts also, not to toot my own horn, but that s.split("\\.", 2) thing could be better expressed using kantan.regex