src/Pure/General/date.scala
changeset 64060 f3fa0bb3f666
parent 64059 365d367d2b45
child 64094 629558a1ecf5
equal deleted inserted replaced
64059:365d367d2b45 64060:f3fa0bb3f666
    18 {
    18 {
    19   /* format */
    19   /* format */
    20 
    20 
    21   object Format
    21   object Format
    22   {
    22   {
    23     def make(fmts: List[DateTimeFormatter]): Format =
    23     def make(fmts: List[DateTimeFormatter], filter: String => String = s => s): Format =
    24     {
    24     {
    25       require(fmts.nonEmpty)
    25       require(fmts.nonEmpty)
    26 
    26 
    27       @tailrec def parse_first(
    27       @tailrec def parse_first(
    28         last_exn: Option[Throwable], fs: List[DateTimeFormatter], str: String): TemporalAccessor =
    28         last_exn: Option[Throwable], fs: List[DateTimeFormatter], str: String): TemporalAccessor =
    35         }
    35         }
    36       }
    36       }
    37       new Format {
    37       new Format {
    38         def apply(date: Date): String = fmts.head.format(date.rep)
    38         def apply(date: Date): String = fmts.head.format(date.rep)
    39         def parse(str: String): Date =
    39         def parse(str: String): Date =
    40           new Date(ZonedDateTime.from(parse_first(None, fmts, str)))
    40           new Date(ZonedDateTime.from(parse_first(None, fmts, filter(str))))
    41       }
    41       }
    42     }
    42     }
       
    43 
       
    44     def make_patterns(patterns: List[String], filter: String => String = s => s): Format =
       
    45       make(patterns.toList.map(DateTimeFormatter.ofPattern(_)), filter)
    43 
    46 
    44     def apply(patterns: String*): Format =
    47     def apply(patterns: String*): Format =
    45       make(patterns.toList.map(DateTimeFormatter.ofPattern(_)))
    48       make(patterns.toList.map(DateTimeFormatter.ofPattern(_)))
    46 
    49 
    47     val default: Format = apply("dd-MMM-uuuu HH:mm:ss xx")
    50     val default: Format = apply("dd-MMM-uuuu HH:mm:ss xx")