← 输入框|github.com|刷新
Skip to content

Inconsistency in parsing data with local-date #11

Open

Description

@elisehuard

I want to process a timeseries with monthly labels, so something like "2012-10" etc, but parser seems unhappy with this. I understand why, but it's pretty impractical.

user> (local-date-time "MM/yyyy" "09/2015")                                                       
DateTimeException Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=9, Year=2015},IS\
O of type java.time.format.Parsed  java.time.LocalDate.from (LocalDate.java:368)                  
user> (local-date 2015 10)                                                                        
#object[java.time.LocalDate 0x6a08b281 "2015-10-01"

Activity

  1. dm3 commented on Aug 16, 2017

    @dm3
    Owner

    Sadly, this library doesn't do much outside of what is provided by the java.time API. This is the limitation of the DateTimeFormatter:

    boot.user=> (java.time.LocalDate/parse "2017/05" (java.time.format.DateTimeFormatter/ofPattern "yyyy/MM"))
    
                java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {Year=2017, MonthOfYear=5},ISO of type java.time.format.Parsed
    java.time.format.DateTimeParseException: Text '2017/05' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2017, MonthOfYear=5},ISO of type java.time.format.Parsed
    

    I can only recommend something like:

    (defn month-year->local-date-time [^String str]
      (->> (.split str "\\/") (map #(Integer/parseInt %)) (reverse) (apply j/local-date-time)))
  2. dm3 commented on Aug 18, 2017

    @dm3
    Owner

    I can imagine a feature that would harmonize the Number* -> TemporalEntity constructors with their parse counterparts. It should require a dynamic context set up before parsing, or a special kind of formatter used so that the default (strict) behaviour is preserved.

    There's a DateTimeFormatter#parseUnresolved which can be used to obtain a parsed TemporalAccessor which then needs to be resolved manually. However, there doesn't seem to be an easy way to just throw more default fields into the parse result and resolve it.

    If anyone wants to tackle this - I'd be happy to collaborate :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions