Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TimeUtility |
|
| 1.4;1.4 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy.io.xml; | |
11 | ||
12 | ||
13 | import org.joda.time.DateTime; | |
14 | import org.joda.time.DateTimeZone; | |
15 | import org.joda.time.Period; | |
16 | ||
17 | import org.openpermis.policy.TimeStamp; | |
18 | ||
19 | ||
20 | ||
21 | /** | |
22 | * Utility for reading and writing time. | |
23 | * @since 0.3.0 | |
24 | */ | |
25 | public final class TimeUtility { | |
26 | ||
27 | //---- Static | |
28 | ||
29 | /** | |
30 | * Converts from {@link TimeStamp} to {@link DateTime}. | |
31 | * @return a {@link DateTime}. | |
32 | * @since 0.3.0 | |
33 | */ | |
34 | public static DateTime getDateTime (TimeStamp timeStamp) { | |
35 | 16 | return new DateTime(timeStamp.getDate(), DateTimeZone.forTimeZone(timeStamp.getTimeZone())); |
36 | } | |
37 | ||
38 | /** | |
39 | * Converts from {@link TimeStamp} to {@link DateTimeZone}. | |
40 | * @return a {@link DateTimeZone}. | |
41 | * @since 0.3.0 | |
42 | */ | |
43 | public static DateTimeZone getDateTimeZone (TimeStamp timeStamp) { | |
44 | 0 | return DateTimeZone.forTimeZone(timeStamp.getTimeZone()); |
45 | } | |
46 | ||
47 | /** | |
48 | * Parses a duration. | |
49 | * @param string a {@link String}. | |
50 | * @return a {@link Period}. | |
51 | * @since 0.3.0 | |
52 | */ | |
53 | public static Period parseDuration (String string) { | |
54 | 42 | if (string.contains("-")) { |
55 | 0 | throw new NumberFormatException("Duration parts may not be negative."); |
56 | } | |
57 | 42 | return new Period(string); |
58 | } | |
59 | ||
60 | /** | |
61 | * Writes a duration to string. | |
62 | * @param duration a {@link Period}. | |
63 | * @return a {@link String}. | |
64 | * @since 0.3.0 | |
65 | */ | |
66 | public static String toString (Period duration) { | |
67 | 3 | return duration.toString(); |
68 | } | |
69 | ||
70 | //---- Constructors | |
71 | ||
72 | 0 | private TimeUtility () { |
73 | // Prevents instantiation. | |
74 | 0 | } |
75 | ||
76 | } |