Subversion Repository Public Repository

litesoft

Diff Revisions 911 vs 912 for /trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/UtilDateAdaptor.java

Diff revisions: vs.
  @@ -5,6 +5,7 @@
5 5
6 6 import org.litesoft.core.simpletypes.temporal.nonpublic.*;
7 7 import org.litesoft.core.typeutils.*;
8 + import org.litesoft.core.typeutils.gregorian.*;
8 9 import org.litesoft.deprecated.*;
9 10
10 11 /**
  @@ -13,9 +14,17 @@
13 14 public class UtilDateAdaptor extends LL_UtilDateAdaptor implements TimestampWithResolutionAccessor,
14 15 TimestampAccessorHMSM
15 16 {
16 - public UtilDateAdaptor( Date pDate, TemporalResolution pTemporalResolution )
17 + private UtilDateAdaptor( long pUTC, TemporalResolution pTemporalResolution )
17 18 {
18 - super( pDate, TemporalResolution.deNull( pTemporalResolution ).ordinal() );
19 + super( new Date( pUTC ), TemporalResolution.deNull( pTemporalResolution ).ordinal() );
20 + }
21 +
22 + /**
23 + * create with NOW
24 + */
25 + public UtilDateAdaptor()
26 + {
27 + this( System.currentTimeMillis(), null );
19 28 }
20 29
21 30 public UtilDateAdaptor( Date pDate )
  @@ -23,92 +32,120 @@
23 32 this( pDate, null );
24 33 }
25 34
26 - public UtilDateAdaptor( long pUTC, TemporalResolution pTemporalResolution )
35 + public UtilDateAdaptor( Date pDate, TemporalResolution pTemporalResolution )
27 36 {
28 - this( new Date( pUTC ), pTemporalResolution );
37 + super( pDate, TemporalResolution.deNull( pTemporalResolution ).ordinal() );
29 38 }
30 39
31 - /**
32 - * create with NOW
33 - */
34 - public UtilDateAdaptor()
40 + private UtilDateAdaptor( int pOffsetMinutes, TemporalResolution pTemporalResolution, int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec,
41 + int pMilliSec )
35 42 {
36 - this( System.currentTimeMillis(), null );
43 + super( pOffsetMinutes, pTemporalResolution.ordinal(), pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec );
37 44 }
38 45
39 - public UtilDateAdaptor force( TemporalResolution pResolution )
46 + public static UtilDateAdaptor offsetWallToMilliSec( int pOffsetMinutes, int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSec )
40 47 {
41 - return (getTemporalResolution() == pResolution) ? this : new UtilDateAdaptor( getUTClong(), pResolution );
48 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToMilliSec, pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec );
42 49 }
43 50
44 - public static UtilDateAdaptor wall( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSec )
51 + public static UtilDateAdaptor currentWall( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSec )
45 52 {
46 - return new UtilDateAdaptor( longForWall( pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec ), TemporalResolution.ToMilliSec );
53 + return offsetWallToMilliSec( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec );
47 54 }
48 55
49 - public static UtilDateAdaptor wall( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec )
56 + public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSec )
50 57 {
51 - return new UtilDateAdaptor( longForWall( pYear, pMonth, pDay, pHour, pMin, pSec, 0 ), TemporalResolution.ToSec );
58 + return offsetWallToMilliSec( 0, pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec );
52 59 }
53 60
54 - public static UtilDateAdaptor wall( int pYear, int pMonth, int pDay, int pHour, int pMin )
61 + public static UtilDateAdaptor offsetWallToSec( int pOffsetMinutes, int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec )
55 62 {
56 - return new UtilDateAdaptor( longForWall( pYear, pMonth, pDay, pHour, pMin, 0, 0 ), TemporalResolution.ToMin );
63 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToSec, pYear, pMonth, pDay, pHour, pMin, pSec, 0 );
57 64 }
58 65
59 - public static UtilDateAdaptor wall( int pYear, int pMonth, int pDay, int pHour )
66 + public static UtilDateAdaptor currentWall( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec )
60 67 {
61 - return new UtilDateAdaptor( longForWall( pYear, pMonth, pDay, pHour, 0, 0, 0 ), TemporalResolution.ToHour );
68 + return offsetWallToSec( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth, pDay, pHour, pMin, pSec );
62 69 }
63 70
64 - public static UtilDateAdaptor wall( int pYear, int pMonth, int pDay )
71 + public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec )
65 72 {
66 - return new UtilDateAdaptor( longForWall( pYear, pMonth, pDay, 0, 0, 0, 0 ), TemporalResolution.ToDay );
73 + return offsetWallToSec( 0, pYear, pMonth, pDay, pHour, pMin, pSec );
67 74 }
68 75
69 - public static UtilDateAdaptor wall( int pYear, int pMonth )
76 + public static UtilDateAdaptor offsetWallToMin( int pOffsetMinutes, int pYear, int pMonth, int pDay, int pHour, int pMin )
70 77 {
71 - return new UtilDateAdaptor( longForWall( pYear, pMonth, 1, 0, 0, 0, 0 ), TemporalResolution.ToMonth );
78 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToMin, pYear, pMonth, pDay, pHour, pMin, 0, 0 );
72 79 }
73 80
74 - public static UtilDateAdaptor wall( int pYear )
81 + public static UtilDateAdaptor currentWall( int pYear, int pMonth, int pDay, int pHour, int pMin )
75 82 {
76 - return new UtilDateAdaptor( longForWall( pYear, 1, 1, 0, 0, 0, 0 ), TemporalResolution.ToYear );
83 + return offsetWallToMin( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth, pDay, pHour, pMin );
77 84 }
78 85
79 - public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSec )
86 + public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin )
80 87 {
81 - return new UtilDateAdaptor( UTC( pYear, pMonth, pDay, pHour, pMin, pSec, pMilliSec ), TemporalResolution.ToMilliSec );
88 + return offsetWallToMin( 0, pYear, pMonth, pDay, pHour, pMin );
82 89 }
83 90
84 - public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec )
91 + public static UtilDateAdaptor offsetWallToHour( int pOffsetMinutes, int pYear, int pMonth, int pDay, int pHour )
85 92 {
86 - return new UtilDateAdaptor( UTC( pYear, pMonth, pDay, pHour, pMin, pSec, 0 ), TemporalResolution.ToSec );
93 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToHour, pYear, pMonth, pDay, pHour, 0, 0, 0 );
87 94 }
88 95
89 - public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour, int pMin )
96 + public static UtilDateAdaptor currentWall( int pYear, int pMonth, int pDay, int pHour )
90 97 {
91 - return new UtilDateAdaptor( UTC( pYear, pMonth, pDay, pHour, pMin, 0, 0 ), TemporalResolution.ToMin );
98 + return offsetWallToHour( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth, pDay, pHour );
92 99 }
93 100
94 101 public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay, int pHour )
95 102 {
96 - return new UtilDateAdaptor( UTC( pYear, pMonth, pDay, pHour, 0, 0, 0 ), TemporalResolution.ToHour );
103 + return offsetWallToHour( 0, pYear, pMonth, pDay, pHour );
104 + }
105 +
106 + public static UtilDateAdaptor offsetWallToDay( int pOffsetMinutes, int pYear, int pMonth, int pDay )
107 + {
108 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToDay, pYear, pMonth, pDay, 0, 0, 0, 0 );
109 + }
110 +
111 + public static UtilDateAdaptor currentWall( int pYear, int pMonth, int pDay )
112 + {
113 + return offsetWallToDay( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth, pDay );
97 114 }
98 115
99 116 public static UtilDateAdaptor zulu( int pYear, int pMonth, int pDay )
100 117 {
101 - return new UtilDateAdaptor( UTC( pYear, pMonth, pDay, 0, 0, 0, 0 ), TemporalResolution.ToDay );
118 + return offsetWallToDay( 0, pYear, pMonth, pDay );
119 + }
120 +
121 + public static UtilDateAdaptor offsetWallToMonth( int pOffsetMinutes, int pYear, int pMonth )
122 + {
123 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToMonth, pYear, pMonth, 0, 0, 0, 0, 0 );
124 + }
125 +
126 + public static UtilDateAdaptor currentWall( int pYear, int pMonth )
127 + {
128 + return offsetWallToMonth( calculateCurrentUTCtoWallOffsetMinutes(), pYear, pMonth );
102 129 }
103 130
104 131 public static UtilDateAdaptor zulu( int pYear, int pMonth )
105 132 {
106 - return new UtilDateAdaptor( UTC( pYear, pMonth, 1, 0, 0, 0, 0 ), TemporalResolution.ToMonth );
133 + return offsetWallToMonth( 0, pYear, pMonth );
134 + }
135 +
136 + public static UtilDateAdaptor offsetWallToYear( int pOffsetMinutes, int pYear )
137 + {
138 + return new UtilDateAdaptor( pOffsetMinutes, TemporalResolution.ToYear, pYear, 0, 0, 0, 0, 0, 0 );
139 + }
140 +
141 + public static UtilDateAdaptor currentWall( int pYear )
142 + {
143 + return offsetWallToYear( calculateCurrentUTCtoWallOffsetMinutes(), pYear );
107 144 }
108 145
109 146 public static UtilDateAdaptor zulu( int pYear )
110 147 {
111 - return new UtilDateAdaptor( UTC( pYear, 1, 1, 0, 0, 0, 0 ), TemporalResolution.ToYear );
148 + return offsetWallToYear( 0, pYear );
112 149 }
113 150
114 151 public static UtilDateAdaptor fromUtilDateToString( String pDateToString )
  @@ -180,7 +217,41 @@
180 217
181 218 public long getUTClong()
182 219 {
183 - return mUTC;
220 + int zYear = mYear;
221 + int zMonth = mMonth;
222 + int zDay = mDay;
223 + int zHour = mHour;
224 + int zMin = mMin;
225 + if ( mUTCtoWallOffsetMinutes != 0 ) // then Adjust the time to UTC
226 + {
227 + zMin -= mUTCtoWallOffsetMinutes; // subtract the offset: to advance those to the west, and retard those to the east.
228 + while ( zMin < 0 )
229 + {
230 + zMin += 60;
231 + if ( --zHour < 0 )
232 + {
233 + zHour += 24;
234 + if ( --zDay < 1 )
235 + {
236 + if ( --zMonth < 1 )
237 + {
238 + zMonth = 12;
239 + zYear--;
240 + }
241 + zDay = Month.daysIn( zYear, zMonth );
242 + }
243 + }
244 + }
245 + while ( 60 <= zMin )
246 + {
247 + zMin -= 60;
248 + if ( 23 < ++zHour )
249 + {
250 + zHour -= 24;
251 + }
252 + }
253 + }
254 + return UTC( zYear, zMonth, zDay, zHour, zMin, mSec, mMilliSec );
184 255 }
185 256
186 257 public Date getWallDate()
  @@ -192,19 +263,19 @@
192 263 public String toString()
193 264 {
194 265 StringBuilder sb = new StringBuilder();
195 - sb.append( Integers.zeroPadIt( 4, getYear() ) );
266 + sb.append( Integers.padIt( 4, getYear() ) );
196 267 sb.append( getTemporalResolution() == TemporalResolution.ToYear ? '|' : '-' );
197 - sb.append( Integers.zeroPadIt( 2, getMonth() ) );
268 + sb.append( Integers.padIt( 2, getMonth() ) );
198 269 sb.append( getTemporalResolution() == TemporalResolution.ToMonth ? '|' : '-' );
199 - sb.append( Integers.zeroPadIt( 2, getDay() ) );
270 + sb.append( Integers.padIt( 2, getDay() ) );
200 271 sb.append( getTemporalResolution() == TemporalResolution.ToDay ? '|' : 't' );
201 - sb.append( Integers.zeroPadIt( 2, getHour() ) );
272 + sb.append( Integers.padIt( 2, getHour() ) );
202 273 sb.append( getTemporalResolution() == TemporalResolution.ToHour ? '|' : ':' );
203 - sb.append( Integers.zeroPadIt( 2, getMin() ) );
274 + sb.append( Integers.padIt( 2, getMin() ) );
204 275 sb.append( getTemporalResolution() == TemporalResolution.ToMin ? '|' : ':' );
205 - sb.append( Integers.zeroPadIt( 2, getSec() ) );
276 + sb.append( Integers.padIt( 2, getSec() ) );
206 277 sb.append( getTemporalResolution() == TemporalResolution.ToSec ? '|' : '.' );
207 - sb.append( Integers.zeroPadIt( 3, getMilliSec() ) );
278 + sb.append( Integers.padIt( 3, getMilliSec() ) );
208 279 int zOffsetMinutes = getUTCtoWallOffsetMinutes();
209 280 if ( zOffsetMinutes == 0 )
210 281 {
  @@ -231,4 +302,9 @@
231 302 }
232 303 return sb.toString();
233 304 }
305 +
306 + public static int calculateCurrentUTCtoWallOffsetMinutes()
307 + {
308 + return new UtilDateAdaptor( 0, null ).getUTCtoWallOffsetMinutes();
309 + }
234 310 }