src/Pure/General/date.scala
author wenzelm
Wed, 05 Oct 2016 20:06:54 +0200
changeset 64057 fd73e0019605
parent 64056 0edc966bee55
child 64059 365d367d2b45
permissions -rw-r--r--
more operations; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/date.scala
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     3
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     4
Date and time, with time zone.
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     5
*/
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     6
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     7
package isabelle
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     8
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
     9
64057
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    10
import java.time.{Instant, ZonedDateTime, ZoneId}
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    11
import java.time.format.{DateTimeFormatter, DateTimeParseException}
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    12
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    13
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    14
object Date
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    15
{
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    16
  /* format */
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    17
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    18
  object Format
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    19
  {
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    20
    def apply(fmt: DateTimeFormatter): Format =
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    21
      new Format {
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    22
        def apply(date: Date): String = fmt.format(date.rep)
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    23
        def parse(str: String): Date = new Date(ZonedDateTime.from(fmt.parse(str)))
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    24
      }
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    25
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    26
    def apply(pattern: String): Format =
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    27
      apply(DateTimeFormatter.ofPattern(pattern))
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    28
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    29
    val default: Format = apply("dd-MMM-uuuu HH:mm:ss xx")
64057
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    30
    val date: Format = apply("dd-MMM-uuuu")
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    31
    val time: Format = apply("HH:mm:ss")
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    32
  }
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    33
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    34
  abstract class Format private
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    35
  {
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    36
    def apply(date: Date): String
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    37
    def parse(str: String): Date
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    38
    def unapply(str: String): Option[Date] =
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    39
      try { Some(parse(str)) }
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    40
      catch { case _: DateTimeParseException => None }
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    41
  }
64057
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    42
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    43
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    44
  /* date operations */
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    45
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    46
  def timezone(): ZoneId = ZoneId.systemDefault
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    47
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    48
  def now(zone: ZoneId = timezone()): Date = new Date(ZonedDateTime.now(zone))
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    49
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    50
  def apply(t: Time, zone: ZoneId = timezone()): Date =
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    51
    new Date(ZonedDateTime.ofInstant(t.instant, zone))
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    52
}
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    53
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    54
sealed case class Date private(private[Date] rep: ZonedDateTime)
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    55
{
64057
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    56
  def time: Time = Time.instant(Instant.from(rep))
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    57
  def timezone: ZoneId = rep.getZone
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    58
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    59
  def format(fmt: Date.Format = Date.Format.default): String = fmt(this)
fd73e0019605 more operations;
wenzelm
parents: 64056
diff changeset
    60
  override def toString: String = format()
64056
0edc966bee55 more date and time operations from Java 8;
wenzelm
parents:
diff changeset
    61
}