src/main/scala/net/tz/lift/util/Helpers.scala
author Tomas Zeman <tzeman@volny.cz>
Tue, 23 Apr 2013 10:36:04 +0200
changeset 108 ef4e3e0ef83f
parent 5 993582ca8d2e
permissions -rw-r--r--
84a94fa29a67504b Task/Project notifications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package net.tz.lift.util
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import java.text.SimpleDateFormat
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import java.util.Date
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
import net.liftweb.http.LiftRules
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
import net.liftweb.util.Helpers.tryo
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
import org.joda.time.{DateMidnight, DateTime, Period, ReadableInstant}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
import org.joda.time.format.{DateTimeFormat, PeriodFormatterBuilder}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
import scala.xml.{NodeSeq, Text}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
object AsIsoDateTime {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
  val df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
  def unapply(in: String): Option[Date] = tryo { df.parse(in) }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
  def apply(d: Date): String = df.format(d)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
object AsDate {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  import DateExtension._
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
  def unapply(in: String): Option[Date] = LiftRules.dateTimeConverter().
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
    parseDate(in).map(_.noTime)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
  def apply(d: Date) = LiftRules.dateTimeConverter().formatDate(d)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
object AsDateTime {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
  def unapply(in: String): Option[Date] = LiftRules.dateTimeConverter().
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
    parseDateTime(in)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
  def apply(d: Date) = LiftRules.dateTimeConverter().formatDateTime(d)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
object AsDateMidnight {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
  lazy val fmt = DateTimeFormat.forPattern("yyyy-MM-dd")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
  def unapply(in: String): Option[DateMidnight] = apply(in)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
  def apply(in: String): Option[DateMidnight] = tryo {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
    fmt.parseDateTime(in).toDateMidnight
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
  def apply(d: ReadableInstant) = fmt.print(d)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
  implicit def dt2d(d: DateTime): Date = d.toDate
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
  implicit def dm2d(d: DateMidnight): Date = d.toDate
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
  implicit def d2dm(d: Date): DateMidnight = new DateMidnight(d)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
  implicit def dm2dt(d: DateMidnight): DateTime = d.toDateTime
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
object AsTimePeriod {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    60
  lazy val fmt = (new PeriodFormatterBuilder).printZeroAlways.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    61
    minimumPrintedDigits(2).appendHours.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
    appendSeparator(":").appendMinutes.toFormatter
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    63
  lazy val fmtDt = DateTimeFormat.forPattern("HH:mm")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    64
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
  /** Parses HH:mm time into period. */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
  def apply(in: String): Option[Period] = tryo { fmt.parsePeriod(in) }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67
  def apply(p: Period) = fmt.print(p)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    68
  def apply(dt: DateTime) = fmtDt.print(dt)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    69
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    70
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    71
object Bytes {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    72
  implicit def dbl2ns(d: Double): NodeSeq =
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    73
    Text(String.format(fmt, d.asInstanceOf[AnyRef]))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    74
  def fmt = "%.2f"
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    75
  def kb(v: Long): NodeSeq = v.toDouble / 1024
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    76
  def mb(v: Long): NodeSeq = v.toDouble / (1024*1024)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    77
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    78
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    79
// vim: set ts=2 sw=2 et: