1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.instance.data;
|
11
|
|
|
12
|
|
import java.math.BigDecimal;
|
13
|
|
|
14
|
|
import org.jbind.util.other.StrBuffer;
|
15
|
|
import org.jbind.xml.core.data.CompareResult;
|
16
|
|
import org.jbind.xml.core.data.IDateTime;
|
17
|
|
import org.jbind.xml.core.data.ITimeZone;
|
18
|
|
|
19
|
|
public class DateTime extends PointInTime implements IDateTime {
|
20
|
|
|
21
|
|
private static ITimeZone MIN_SHIFT = new TimeZone(-1, 14, 0);
|
22
|
|
private static ITimeZone MAX_SHIFT = new TimeZone(1, 14, 0);
|
23
|
|
|
24
|
11
|
public static IDateTime create(int aSign, int aYear, int aMonth, int aDay, int anHour, int aMinute, int aSecond, BigDecimal aFraction, ITimeZone aTimeZone) {
|
25
|
11
|
IDateTime dt = new DateTime(aSign, aYear, aMonth, aDay, anHour, aMinute, aSecond, aFraction, null != aTimeZone);
|
26
|
11
|
IDateTime res = null;
|
27
|
11
|
if (null != aTimeZone) {
|
28
|
9
|
res = TimeHelper.subtract(dt, aTimeZone);
|
29
|
|
} else {
|
30
|
2
|
res = dt;
|
31
|
|
}
|
32
|
11
|
return res;
|
33
|
|
}
|
34
|
|
|
35
|
|
protected int mySign;
|
36
|
|
protected int myYear;
|
37
|
|
protected int myMonth;
|
38
|
|
protected int myDay;
|
39
|
|
|
40
|
24
|
public int getSign() {
|
41
|
24
|
return mySign;
|
42
|
|
}
|
43
|
37
|
public int getYear() {
|
44
|
37
|
return myYear;
|
45
|
|
}
|
46
|
48
|
public int getMonth() {
|
47
|
48
|
return myMonth;
|
48
|
|
}
|
49
|
46
|
public int getDay() {
|
50
|
46
|
return myDay;
|
51
|
|
}
|
52
|
|
|
53
|
24
|
public DateTime(int aSign, int aYear, int aMonth, int aDay, int anHour, int aMinute, int aSecond, BigDecimal aFraction, boolean aHadTimeZone) {
|
54
|
24
|
super(anHour, aMinute, aSecond, aFraction, aHadTimeZone);
|
55
|
24
|
mySign = aSign;
|
56
|
24
|
myYear = aYear;
|
57
|
24
|
myMonth = aMonth;
|
58
|
24
|
myDay = aDay;
|
59
|
|
}
|
60
|
|
|
61
|
0
|
public CompareResult compareTo(IDateTime aDateTime) {
|
62
|
0
|
CompareResult res1, res2;
|
63
|
0
|
if (mySign < 0) {
|
64
|
0
|
if (aDateTime.getSign() < 0) {
|
65
|
0
|
res1 = CompareResult.GREATER;
|
66
|
0
|
res2 = CompareResult.LESS;
|
67
|
|
} else {
|
68
|
0
|
res1 = CompareResult.LESS;
|
69
|
0
|
res2 = CompareResult.LESS;
|
70
|
|
}
|
71
|
|
} else {
|
72
|
0
|
if (aDateTime.getSign() < 0) {
|
73
|
0
|
res1 = CompareResult.GREATER;
|
74
|
0
|
res2 = CompareResult.GREATER;
|
75
|
|
} else {
|
76
|
0
|
res1 = CompareResult.LESS;
|
77
|
0
|
res2 = CompareResult.GREATER;
|
78
|
|
}
|
79
|
|
}
|
80
|
0
|
if ((hadTimeZone() && aDateTime.hadTimeZone()) || (!hadTimeZone() && !aDateTime.hadTimeZone())) {
|
81
|
0
|
if (getYear() < aDateTime.getYear()) {
|
82
|
0
|
return res1;
|
83
|
0
|
} else if (getYear() > aDateTime.getYear()) {
|
84
|
0
|
return res2;
|
85
|
|
}
|
86
|
0
|
if (getMonth() < aDateTime.getMonth()) {
|
87
|
0
|
return res1;
|
88
|
0
|
} else if (getMonth() > aDateTime.getMonth()) {
|
89
|
0
|
return res2;
|
90
|
|
}
|
91
|
0
|
if (getDay() < aDateTime.getDay()) {
|
92
|
0
|
return res1;
|
93
|
0
|
} else if (getDay() > aDateTime.getDay()) {
|
94
|
0
|
return res2;
|
95
|
|
}
|
96
|
0
|
if (getHour() < aDateTime.getHour()) {
|
97
|
0
|
return res1;
|
98
|
0
|
} else if (getHour() > aDateTime.getHour()) {
|
99
|
0
|
return res2;
|
100
|
|
}
|
101
|
0
|
if (getMinute() < aDateTime.getMinute()) {
|
102
|
0
|
return res1;
|
103
|
0
|
} else if (getMinute() > aDateTime.getMinute()) {
|
104
|
0
|
return res2;
|
105
|
|
}
|
106
|
0
|
if (getSecond() < aDateTime.getSecond()) {
|
107
|
0
|
return res1;
|
108
|
0
|
} else if (getSecond() > aDateTime.getSecond()) {
|
109
|
0
|
return res2;
|
110
|
|
}
|
111
|
0
|
if ((null != getFraction()) && (null != aDateTime.getFraction())) {
|
112
|
0
|
int c = getFraction().compareTo(aDateTime.getFraction());
|
113
|
0
|
return (c < 0) ? res1 : ((c > 0) ? res2 : CompareResult.EQUAL);
|
114
|
0
|
} else if (null != getFraction()) {
|
115
|
0
|
return res2;
|
116
|
0
|
} else if (null != aDateTime.getFraction()) {
|
117
|
0
|
return res1;
|
118
|
|
}
|
119
|
0
|
return CompareResult.EQUAL;
|
120
|
|
} else {
|
121
|
0
|
IDateTime thisMin = hadTimeZone() ? this : TimeHelper.subtract(this, MIN_SHIFT);
|
122
|
0
|
IDateTime thisMax = hadTimeZone() ? this : TimeHelper.subtract(this, MAX_SHIFT);
|
123
|
0
|
IDateTime thatMin = aDateTime.hadTimeZone() ? aDateTime : TimeHelper.subtract(aDateTime, MIN_SHIFT);
|
124
|
0
|
IDateTime thatMax = aDateTime.hadTimeZone() ? aDateTime : TimeHelper.subtract(aDateTime, MAX_SHIFT);
|
125
|
0
|
CompareResult r1 = thisMin.compareTo(thatMax);
|
126
|
0
|
CompareResult r2 = thisMax.compareTo(thatMin);
|
127
|
0
|
return (r1 == r2) ? r1 : CompareResult.UNDETERMINED;
|
128
|
|
}
|
129
|
|
}
|
130
|
|
|
131
|
5
|
public boolean equals(Object anObject) {
|
132
|
5
|
boolean res = super.equals(anObject);
|
133
|
5
|
if (res) {
|
134
|
5
|
res = anObject instanceof IDateTime;
|
135
|
5
|
if (res) {
|
136
|
5
|
IDateTime t = (IDateTime)anObject;
|
137
|
5
|
res = (mySign == t.getSign()) && (myYear == t.getYear()) && (myMonth == t.getMonth()) && (myDay == t.getDay());
|
138
|
|
}
|
139
|
|
}
|
140
|
5
|
return res;
|
141
|
|
}
|
142
|
|
|
143
|
10
|
public int hashCode() {
|
144
|
10
|
return super.hashCode() + mySign + myYear + myMonth + myDay;
|
145
|
|
}
|
146
|
|
|
147
|
0
|
public String toString() {
|
148
|
0
|
StrBuffer sb = new StrBuffer();
|
149
|
0
|
if (mySign < 0) {
|
150
|
0
|
sb.append('-');
|
151
|
|
}
|
152
|
0
|
if (myYear > 9999) {
|
153
|
0
|
StringBuffer b = new StringBuffer();
|
154
|
0
|
for (int x = myYear; x != 0; x /= 10) {
|
155
|
0
|
b.append((char)('0' + x % 10));
|
156
|
|
}
|
157
|
0
|
for (int i = b.length(); --i >= 0; ) {
|
158
|
0
|
sb.append(b.charAt(i));
|
159
|
|
}
|
160
|
|
} else {
|
161
|
0
|
TimeHelper.output(myYear, 4, sb);
|
162
|
|
}
|
163
|
0
|
sb.append('-');
|
164
|
0
|
TimeHelper.output(myMonth, 2, sb);
|
165
|
0
|
sb.append('-');
|
166
|
0
|
TimeHelper.output(myDay, 2, sb);
|
167
|
0
|
sb.append('T');
|
168
|
0
|
sb.append(super.toString());
|
169
|
0
|
return sb.toString();
|
170
|
|
}
|
171
|
|
}
|
172
|
|
|