1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.util.L10N; 34 import com.caucho.util.QDate; 35 36 39 public class Period { 40 static L10N L = new L10N(Period.class); 41 42 public static final long SECOND = 1000L; 43 public static final long MINUTE = 60L * SECOND; 44 public static final long HOUR = 60L * MINUTE; 45 public static final long DAY = 24L * HOUR; 46 47 public static final long MONTH = DAY * 30L; 48 49 public static final long YEAR = DAY * 365L; 50 public static final long INFINITE = (Long.MAX_VALUE / 2 / 1000) * 1000; 51 public static final long FOREVER = INFINITE; 52 53 private static final QDate _localCalendar = QDate.createLocal(); 54 55 private long _period; 56 57 public Period() 58 { 59 } 60 61 public Period(long period) 62 { 63 _period = period; 64 } 65 66 69 public long getDefaultUnits() 70 { 71 return 1000; 72 } 73 74 77 public void addText(String text) 78 throws ConfigException 79 { 80 _period = toPeriod(text, getDefaultUnits()); 81 } 82 83 86 public long getPeriod() 87 { 88 return _period; 89 } 90 91 105 public static long toPeriod(String value) 106 throws ConfigException 107 { 108 return toPeriod(value, 1000); 109 } 110 111 125 public static long toPeriod(String value, long defaultUnits) 126 throws ConfigException 127 { 128 if (value == null) 129 return 0; 130 131 long sign = 1; 132 long period = 0; 133 134 int i = 0; 135 int length = value.length(); 136 137 if (length > 0 && value.charAt(i) == '-') { 138 sign = -1; 139 i++; 140 } 141 142 while (i < length) { 143 long delta = 0; 144 char ch; 145 146 for (; i < length && (ch = value.charAt(i)) >= '0' && ch <= '9'; i++) 147 delta = 10 * delta + ch - '0'; 148 149 if (length <= i) 150 period += defaultUnits * delta; 151 else { 152 ch = value.charAt(i++); 153 switch (ch) { 154 case 's': 155 period += 1000 * delta; 156 break; 157 158 case 'm': 159 if (i < value.length() && value.charAt(i) == 's') { 160 i++; 161 period += delta; 162 } 163 else 164 period += 60 * 1000 * delta; 165 break; 166 167 case 'h': 168 period += 60L * 60 * 1000 * delta; 169 break; 170 171 case 'D': 172 period += DAY * delta; 173 break; 174 175 case 'W': 176 period += 7L * DAY * delta; 177 break; 178 179 case 'M': 180 period += 30L * DAY * delta; 181 break; 182 183 case 'Y': 184 period += 365L * DAY * delta; 185 break; 186 187 default: 188 throw new ConfigException(L.l("Unknown unit `{0}' in period `{1}'. Valid units are:\n '10ms' milliseconds\n '10s' seconds\n '10m' minutes\n '10h' hours\n '10D' days\n '10W' weeks\n '10M' months\n '10Y' years", 189 String.valueOf(ch), value)); 190 } 191 } 192 } 193 194 period = sign * period; 195 196 203 204 return period; 205 } 206 207 214 public static long periodEnd(long now, long period) 215 { 216 return periodEnd(now, period, _localCalendar); 217 } 218 219 226 public static long periodEnd(long now, long period, QDate cal) 227 { 228 if (period < 0) 229 return Long.MAX_VALUE; 230 else if (period == 0) 231 return now; 232 233 if (period < 30 * DAY) { 234 synchronized (cal) { 235 cal.setGMTTime(now); 236 237 long localTime = cal.getLocalTime(); 238 239 localTime = localTime + (period - (localTime + 4 * DAY) % period); 240 241 cal.setLocalTime(localTime); 242 243 return cal.getGMTTime(); 244 } 245 } 246 247 if (period % (30 * DAY) == 0) { 248 int months = (int) (period / (30 * DAY)); 249 250 synchronized (cal) { 251 cal.setGMTTime(now); 252 long year = cal.getYear(); 253 int month = cal.getMonth(); 254 255 cal.setLocalTime(0); 256 257 cal.setDate(year, month + months, 1); 258 259 return cal.getGMTTime(); 260 } 261 } 262 263 if (period % (365 * DAY) == 0) { 264 long years = (period / (365 * DAY)); 265 266 synchronized (cal) { 267 cal.setGMTTime(now); 268 long year = cal.getYear(); 269 270 cal.setLocalTime(0); 271 272 long newYear = year + (years - year % years); 273 274 cal.setDate(newYear, 0, 1); 275 276 return cal.getGMTTime(); 277 } 278 } 279 280 synchronized (cal) { 281 cal.setGMTTime(now); 282 283 long localTime = cal.getLocalTime(); 284 285 localTime = localTime + (period - (localTime + 4 * DAY) % period); 286 287 cal.setLocalTime(localTime); 288 289 return cal.getGMTTime(); 290 } 291 } 292 293 public String toString() 294 { 295 return "Period[" + _period + "]"; 296 } 297 } 298 | Popular Tags |