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