Repository navigation
Expand file tree
/
Copy pathHTime.java
More file actions
169 lines (149 loc) · 4.72 KB
/
Copy pathHTime.java
File metadata and controls
169 lines (149 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Copyright (c) 2011, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 06 Jun 2011 Brian Frank Creation
//
package org.projecthaystack;
import java.util.Calendar;
import org.projecthaystack.io.HZincReader;
/**
* HTime models a time of day tag value.
*
* @see <a href='http://project-haystack.org/doc/TagModel#tagKinds'>Project Haystack</a>
*/
public class HTime extends HVal
{
/** Construct with all fields */
public static HTime make(int hour, int min, int sec, int ms)
{
if (hour < 0 || hour > 23) throw new IllegalArgumentException("Invalid hour");
if (min < 0 || min > 59) throw new IllegalArgumentException("Invalid min");
if (sec < 0 || sec > 59) throw new IllegalArgumentException("Invalid sec");
if (ms < 0 || ms > 999) throw new IllegalArgumentException("Invalid ms");
return new HTime(hour, min, sec, ms);
}
/** Convenience constructing with ms = 0 */
public static HTime make(int hour, int min, int sec)
{
return make(hour, min, sec, 0);
}
/** Convenience constructing with sec = 0 and ms = 0 */
public static HTime make(int hour, int min)
{
return make(hour, min, 0, 0);
}
/** Initialize from Java calendar instance */
public static HTime make(Calendar c)
{
return new HTime(c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
c.get(Calendar.SECOND),
c.get(Calendar.MILLISECOND));
}
/** Parse from string fomat "hh:mm:ss.FF" or raise ParseException */
public static HTime make(String s)
{
ParseException err = new ParseException(s);
int hour, min, sec, ms;
try { hour = Integer.parseInt(s.substring(0, 2)); }
catch (Exception e) { throw new ParseException("Invalid hours: " + s); }
if (s.charAt(2) != ':') throw err;
try { min = Integer.parseInt(s.substring(3, 5)); }
catch (Exception e) { throw new ParseException("Invalid minutes: " + s); }
if (s.charAt(5) != ':') throw err;
try { sec = Integer.parseInt(s.substring(6, 8)); }
catch (Exception e) { throw new ParseException("invalid seconds: " + s); }
if (s.length() == "hh:mm:ss".length()) return HTime.make(hour, min, sec);
if (s.charAt(8) != '.') throw err;
ms = 0;
int pos = 9;
int places = 0;
int len = s.length();
// HTime only stores ms precision, so truncate anything after that
while (pos < len && places < 3)
{
ms = (ms * 10) + (s.charAt(pos) - '0');
++pos;
++places;
}
switch (places)
{
case 1: ms *= 100; break;
case 2: ms *= 10; break;
case 3: break;
default: throw new IllegalStateException("Too much fractional precision: " + s);
}
return HTime.make(hour, min, sec, ms);
}
/** Singleton for midnight 00:00 */
public static final HTime MIDNIGHT = new HTime(0, 0, 0, 0);
/** Private constructor */
private HTime(int hour, int min, int sec, int ms)
{
this.hour = hour;
this.min = min;
this.sec = sec;
this.ms = ms;
}
/** Hour of day as 0-23 */
public final int hour;
/** Minute of hour as 0-59 */
public final int min;
/** Second of minute as 0-59 */
public final int sec;
/** Fractional seconds in milliseconds 0-999 */
public final int ms;
/** Hash is based on hour, min, sec, ms */
public int hashCode()
{
return (hour << 24) ^ (min << 20) ^ (sec << 16) ^ ms;
}
/** Equals is based on year, month, day */
public boolean equals(Object that)
{
if (!(that instanceof HTime)) return false;
HTime x = (HTime)that;
return hour == x.hour && min == x.min && sec == x.sec && ms == x.ms;
}
/** Return sort order as negative, 0, or positive */
public int compareTo(Object that)
{
HTime x = (HTime)that;
if (hour < x.hour) return -1; else if (hour > x.hour) return 1;
if (min < x.min) return -1; else if (min > x.min) return 1;
if (sec < x.sec) return -1; else if (sec > x.sec) return 1;
if (ms < x.ms) return -1; else if (ms > x.ms) return 1;
return 0;
}
/** Encode as "h:hh:mm:ss.FFF" */
public String toJson()
{
StringBuffer s = new StringBuffer();
s.append("h:");
encode(s);
return s.toString();
}
/** Encode as "hh:mm:ss.FFF" */
public String toZinc()
{
StringBuffer s = new StringBuffer();
encode(s);
return s.toString();
}
/** Package private implementation shared with HDateTime */
void encode(StringBuffer s)
{
if (hour < 10) s.append('0'); s.append(hour).append(':');
if (min < 10) s.append('0'); s.append(min).append(':');
if (sec < 10) s.append('0'); s.append(sec);
if (ms != 0)
{
s.append('.');
if (ms < 10) s.append('0');
if (ms < 100) s.append('0');
s.append(ms);
}
}
}