src/main/scala/net/tz/lift/model/JodaTimeField.scala
changeset 99 49eb72a46208
parent 24 149113e059bd
equal deleted inserted replaced
98:eac38214183d 99:49eb72a46208
    88     s => setBox(AsDateMidnight(s) map {_.toGregorianCalendar}),
    88     s => setBox(AsDateMidnight(s) map {_.toGregorianCalendar}),
    89     "class" -> "date"))
    89     "class" -> "date"))
    90 }
    90 }
    91 
    91 
    92 /**
    92 /**
       
    93  * Optional Joda date field.
       
    94  */
       
    95 class OptionalJodaDateMidnightField[T <: Record[T]](rec: T) extends
       
    96   OptionalDateTimeField(rec) {
       
    97   override def setFromAny(in: Any): Box[Calendar] = in match {
       
    98     case dt: AbstractDateTime => setBox(Full(dt.toGregorianCalendar))
       
    99     case x => super.setFromAny(x)
       
   100   }
       
   101 
       
   102   def set(dt: Box[AbstractDateTime]): Box[Calendar] =
       
   103     set(dt map(_.toGregorianCalendar))
       
   104 
       
   105   def date = get map(new DateMidnight(_))
       
   106 
       
   107   override def toString = date map(AsDateMidnight(_)) getOrElse ""
       
   108 
       
   109   override def asHtml = Text(toString)
       
   110   override def toForm = Full(SHtml.text(toString,
       
   111     s => setBox(AsDateMidnight(s) map {_.toGregorianCalendar}),
       
   112     "class" -> "date"))
       
   113 }
       
   114 
       
   115 /**
    93  * Joda date-time converters.
   116  * Joda date-time converters.
    94  */
   117  */
    95 object AsDateTime {
   118 object AsDateTime {
    96   val fmt = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm")
   119   val fmt = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm")
    97 
   120 
   104  */
   127  */
   105 object AsDateMidnight {
   128 object AsDateMidnight {
   106   val fmt = DateTimeFormat.forPattern("dd.MM.yyyy")
   129   val fmt = DateTimeFormat.forPattern("dd.MM.yyyy")
   107 
   130 
   108   def apply(d: DateMidnight): String = fmt.print(d)
   131   def apply(d: DateMidnight): String = fmt.print(d)
   109   def apply(s: String): Box[DateMidnight] = tryo {
   132   def apply(s: String): Box[DateMidnight] = s match {
   110     fmt.parseDateTime(s).toDateMidnight }
   133     case null | "" => Empty
       
   134     case s => tryo { fmt.parseDateTime(s).toDateMidnight }
       
   135   }
   111 }
   136 }
   112 
   137 
   113 // vim: set ts=2 sw=2 et:
   138 // vim: set ts=2 sw=2 et: