1 5 package org.h2.util; 6 7 import java.sql.Date ; 8 import java.sql.SQLException ; 9 import java.sql.Time ; 10 import java.sql.Timestamp ; 11 import java.util.Calendar ; 12 import java.util.GregorianCalendar ; 13 import java.util.TimeZone ; 14 15 import org.h2.message.Message; 16 import org.h2.value.Value; 17 import org.h2.value.ValueDate; 18 import org.h2.value.ValueTime; 19 import org.h2.value.ValueTimestamp; 20 21 public class DateTimeUtils { 22 23 public static Timestamp convertTimestampToCalendar(Timestamp x, Calendar calendar) throws SQLException { 24 if(x != null) { 25 Timestamp y = new Timestamp (getLocalTime(x, calendar)); 26 y.setNanos(x.getNanos()); 28 x = y; 29 } 30 return x; 31 } 32 33 public static Time cloneAndNormalizeTime(Time value) { 34 Calendar cal = Calendar.getInstance(); 35 cal.setTime(value); 36 cal.get(Calendar.HOUR_OF_DAY); 38 cal.set(1970, 0, 1); 39 return new Time (cal.getTime().getTime()); 40 } 41 42 public static Date cloneAndNormalizeDate(Date value) { 43 Calendar cal = Calendar.getInstance(); 44 cal.setTime(value); 45 cal.get(Calendar.YEAR); 47 cal.set(Calendar.MILLISECOND, 0); 48 cal.set(Calendar.SECOND, 0); 49 cal.set(Calendar.MINUTE, 0); 50 cal.set(Calendar.HOUR_OF_DAY, 0); 51 return new Date (cal.getTime().getTime()); 52 } 53 54 public static Value convertDateToUniversal(Date x, Calendar source) throws SQLException { 55 return ValueDate.get(new Date (getUniversalTime(source, x))); 56 } 57 58 public static Value convertTimeToUniversal(Time x, Calendar source) throws SQLException { 59 return ValueTime.get(new Time (getUniversalTime(source, x))); 60 } 61 62 public static Value convertTimestampToUniversal(Timestamp x, Calendar source) throws SQLException { 63 Timestamp y = new Timestamp (getUniversalTime(source, x)); 64 y.setNanos(x.getNanos()); 66 return ValueTimestamp.getNoCopy(y); 67 } 68 69 private static long getUniversalTime(Calendar source, java.util.Date x) throws SQLException { 70 if(source == null) { 71 throw Message.getInvalidValueException("calendar", null); 72 } 73 source = (Calendar )source.clone(); 74 Calendar universal=Calendar.getInstance(); 75 source.setTime(x); 76 convertTime(source, universal); 77 return universal.getTime().getTime(); 78 } 79 80 private static long getLocalTime(java.util.Date x, Calendar target) throws SQLException { 81 if(target == null) { 82 throw Message.getInvalidValueException("calendar", null); 83 } 84 target = (Calendar )target.clone(); 85 Calendar local=Calendar.getInstance(); 86 local.setTime(x); 87 convertTime(local, target); 88 return target.getTime().getTime(); 89 } 90 91 private static void convertTime(Calendar from, Calendar to) { 92 to.set(Calendar.YEAR, from.get(Calendar.YEAR)); 93 to.set(Calendar.MONTH, from.get(Calendar.MONTH)); 94 to.set(Calendar.DAY_OF_MONTH, from.get(Calendar.DAY_OF_MONTH)); 95 to.set(Calendar.HOUR_OF_DAY, from.get(Calendar.HOUR_OF_DAY)); 96 to.set(Calendar.MINUTE, from.get(Calendar.MINUTE)); 97 to.set(Calendar.SECOND, from.get(Calendar.SECOND)); 98 to.set(Calendar.MILLISECOND, from.get(Calendar.MILLISECOND)); 99 } 100 101 public static Date convertDateToCalendar(Date x, Calendar calendar) throws SQLException { 102 return x == null ? null : new Date (getLocalTime(x, calendar)); 103 } 104 105 public static Time convertTimeToCalendar(Time x, Calendar calendar) throws SQLException { 106 return x == null ? null : new Time (getLocalTime(x, calendar)); 107 } 108 109 public static java.util.Date parseDateTime(String s, int type, int errorCode) throws SQLException { 110 if (s == null) { 111 return null; 112 } 113 try { 114 int timeStart = 0; 115 TimeZone tz = null; 116 if(type == Value.TIME) { 117 timeStart = 0; 118 } else { 119 timeStart = s.indexOf(' ') + 1; 120 if(timeStart <= 0) { 121 timeStart = s.indexOf('T') + 1; 123 } 124 } 125 126 int year = 1970, month = 1, day = 1; 127 if (type != Value.TIME) { 128 if(s.startsWith("+")) { 130 s = s.substring(1); 131 } 132 int s1 = s.indexOf('-', 1); 134 int s2 = s.indexOf('-', s1 + 1); 135 if(s1 <= 0 || s2 <= s1) { 136 throw Message.getSQLException(errorCode, s); 137 } 138 year = Integer.parseInt(s.substring(0, s1)); 139 month = Integer.parseInt(s.substring(s1 + 1, s2)); 140 int end = timeStart == 0 ? s.length() : timeStart - 1; 141 day = Integer.parseInt(s.substring(s2 + 1, end)); 142 } 143 int hour = 0, minute = 0, second = 0, nano = 0; 144 if (type != Value.DATE) { 145 int s1 = s.indexOf(':', timeStart); 146 int s2 = s.indexOf(':', s1 + 1); 147 int s3 = s.indexOf('.', s2 + 1); 148 if(s1 <= 0 || s2 <= s1) { 149 throw Message.getSQLException(errorCode, s); 150 } 151 152 if(s.endsWith("Z")) { 153 s = s.substring(0, s.length()-1); 154 } else { 155 int tzstart = s.indexOf('+', s2 + 1); 156 if(tzstart < 0) { 157 tzstart = s.indexOf('-', s2 + 1); 158 } 159 if(tzstart >= 0) { 160 String tzName = "GMT" + s.substring(tzstart); 161 tz = TimeZone.getTimeZone(tzName); 162 if(!tz.getID().equals(tzName)) { 163 throw Message.getSQLException(errorCode, s + " " + tz.getID() + "/" + tzName); 164 } 165 s = s.substring(0, tzstart); 166 } 167 } 168 169 hour = Integer.parseInt(s.substring(timeStart, s1)); 170 minute = Integer.parseInt(s.substring(s1 + 1, s2)); 171 if (s3 < 0) { 172 second = Integer.parseInt(s.substring(s2 + 1)); 173 } else { 174 second = Integer.parseInt(s.substring(s2 + 1, s3)); 175 String n = (s + "000000000").substring(s3 + 1, s3 + 10); 176 nano = Integer.parseInt(n); 177 } 178 } 179 Calendar c; 180 if(tz == null) { 181 c = Calendar.getInstance(); 182 } else { 183 c = Calendar.getInstance(tz); 184 } 185 c.setLenient(false); 186 long time; 187 if(year <= 0) { 188 c.set(Calendar.ERA, GregorianCalendar.BC); 189 c.set(Calendar.YEAR, 1 - year); 190 } else { 191 c.set(Calendar.YEAR, year); 192 } 193 c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, day); 195 c.set(Calendar.HOUR_OF_DAY, hour); 196 c.set(Calendar.MINUTE, minute); 197 c.set(Calendar.SECOND, second); 198 if(type != Value.TIMESTAMP) { 199 c.set(Calendar.MILLISECOND, nano / 1000000); 200 } 201 time = c.getTime().getTime(); 202 switch(type) { 203 case Value.DATE: 204 return new java.sql.Date (time); 205 case Value.TIME: 206 return new java.sql.Time (time); 207 case Value.TIMESTAMP: { 208 Timestamp ts = new Timestamp (time); 209 ts.setNanos(nano); 210 return ts; 211 } 212 default: 213 throw Message.getInternalError("type:"+type); 214 } 215 } catch(IllegalArgumentException e) { 216 throw Message.getSQLException(errorCode, s); 217 } 218 } 219 220 } 221 | Popular Tags |