001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 */ 012package org.nuxeo.ecm.automation.client.model; 013 014import java.text.ParseException; 015import java.util.Calendar; 016import java.util.Date; 017import java.util.GregorianCalendar; 018import java.util.TimeZone; 019 020/** 021 * Parse / format ISO 8601 dates. 022 * 023 * @author "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>" 024 */ 025public class DateParser { 026 027 public static Calendar parse(String str) throws ParseException { 028 if (str == null) { 029 return null; 030 } 031 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 032 cal.clear(); 033 int len = str.length(); 034 if (len == 0) { // empty string 035 // TODO throw error? 036 return cal; 037 } 038 int i = 0; 039 i = readYear(cal, str, i); 040 i = readCharOpt('-', cal, str, i); 041 if (i == -1) { 042 return cal; 043 } 044 i = readMonth(cal, str, i); 045 i = readCharOpt('-', cal, str, i); 046 if (i == -1) { 047 return cal; 048 } 049 i = readDay(cal, str, i); 050 i = readCharOpt('T', cal, str, i); 051 if (i == -1) { 052 return cal; 053 } 054 i = readHours(cal, str, i); 055 i = readCharOpt(':', cal, str, i); 056 if (i == -1) { 057 return cal; 058 } 059 i = readMinutes(cal, str, i); 060 if (isChar(':', str, i)) { 061 i = readSeconds(cal, str, i + 1); 062 if (isChar('.', str, i)) { 063 i = readMilliseconds(cal, str, i + 1); 064 } 065 } 066 if (i > -1) { 067 readTimeZone(cal, str, i); 068 } 069 return cal; 070 } 071 072 public static Date parseW3CDateTime(String str) { 073 if (str == null) { 074 return null; 075 } 076 try { 077 return parse(str).getTime(); 078 } catch (ParseException e) { 079 throw new IllegalArgumentException("Failed to parse ISO 8601 date: " + str, e); 080 } 081 } 082 083 /** 084 * 2011-10-23T12:00:00.00Z 085 * 086 * @param date 087 * @return 088 */ 089 public static String formatW3CDateTime(Date date) { 090 if (date == null) { 091 return null; 092 } 093 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 094 cal.setTime(date); 095 StringBuilder buf = new StringBuilder(32); 096 return buf.append(cal.get(Calendar.YEAR)).append('-').append(pad(cal.get(Calendar.MONTH) + 1)).append('-').append( 097 pad(cal.get(Calendar.DATE))).append('T').append(pad(cal.get(Calendar.HOUR_OF_DAY))).append(':').append( 098 pad(cal.get(Calendar.MINUTE))).append(':').append(pad(cal.get(Calendar.SECOND))).append('Z').toString(); 099 } 100 101 public static String formatW3CDateTimeMs(Date date) { 102 if (date == null) { 103 return null; 104 } 105 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 106 cal.setTime(date); 107 StringBuilder buf = new StringBuilder(32); 108 return buf.append(cal.get(Calendar.YEAR)).append('-').append(pad(cal.get(Calendar.MONTH) + 1)).append('-').append( 109 pad(cal.get(Calendar.DATE))).append('T').append(pad(cal.get(Calendar.HOUR_OF_DAY))).append(':').append( 110 pad(cal.get(Calendar.MINUTE))).append(':').append(pad(cal.get(Calendar.SECOND))).append('.').append( 111 pad3(cal.get(Calendar.MILLISECOND))).append('Z').toString(); 112 } 113 114 private final static String pad(int i) { 115 return i < 10 ? "0".concat(String.valueOf(i)) : String.valueOf(i); 116 } 117 118 private final static String pad3(int i) { 119 if (i < 10) { 120 return "00".concat(String.valueOf(i)); 121 } else if (i < 100) { 122 return "0".concat(String.valueOf(i)); 123 } else { 124 return String.valueOf(i); 125 } 126 } 127 128 private final static int readYear(Calendar cal, String str, int off) throws ParseException { 129 if (str.length() >= off + 4) { 130 cal.set(Calendar.YEAR, Integer.parseInt(str.substring(off, off + 4))); 131 return off + 4; 132 } 133 throw new ParseException("Invalid year in date '" + str + "'", off); 134 } 135 136 private final static int readMonth(Calendar cal, String str, int off) throws ParseException { 137 if (str.length() >= off + 2) { 138 cal.set(Calendar.MONTH, Integer.parseInt(str.substring(off, off + 2)) - 1); 139 return off + 2; 140 } 141 throw new ParseException("Invalid month in date '" + str + "'", off); 142 } 143 144 private final static int readDay(Calendar cal, String str, int off) throws ParseException { 145 if (str.length() >= off + 2) { 146 cal.set(Calendar.DATE, Integer.parseInt(str.substring(off, off + 2))); 147 return off + 2; 148 } 149 throw new ParseException("Invalid day in date '" + str + "'", off); 150 } 151 152 private final static int readHours(Calendar cal, String str, int off) throws ParseException { 153 if (str.length() >= off + 2) { 154 cal.set(Calendar.HOUR, Integer.parseInt(str.substring(off, off + 2))); 155 return off + 2; 156 } 157 throw new ParseException("Invalid hours in date '" + str + "'", off); 158 } 159 160 private final static int readMinutes(Calendar cal, String str, int off) throws ParseException { 161 if (str.length() >= off + 2) { 162 cal.set(Calendar.MINUTE, Integer.parseInt(str.substring(off, off + 2))); 163 return off + 2; 164 } 165 throw new ParseException("Invalid minutes in date '" + str + "'", off); 166 } 167 168 private final static int readSeconds(Calendar cal, String str, int off) throws ParseException { 169 if (str.length() >= off + 2) { 170 cal.set(Calendar.SECOND, Integer.parseInt(str.substring(off, off + 2))); 171 return off + 2; 172 } 173 throw new ParseException("Invalid seconds in date '" + str + "'", off); 174 } 175 176 /** 177 * Return -1 if no more content to read or the offset of the expected TZ 178 * 179 * @param cal 180 * @param str 181 * @param off 182 * @return 183 * @throws ParseException 184 */ 185 private final static int readMilliseconds(Calendar cal, String str, int off) throws ParseException { 186 int e = str.indexOf('Z', off); 187 if (e == -1) { 188 e = str.indexOf('+', off); 189 if (e == -1) { 190 e = str.indexOf('-', off); 191 } 192 } 193 String ms = e == -1 ? str.substring(off) : str.substring(off, e); 194 // need to normalize the ms fraction to 3 digits. 195 // If less than 3 digits right pad with 0 196 // If more than 3 digits truncate to 3 digits. 197 int mslen = ms.length(); 198 if (mslen > 0) { 199 int f = 0; 200 switch (mslen) { 201 case 1: 202 f = Integer.parseInt(ms) * 100; 203 break; 204 case 2: 205 f = Integer.parseInt(ms) * 10; 206 break; 207 case 3: 208 f = Integer.parseInt(ms); 209 break; 210 default: // truncate 211 f = Integer.parseInt(ms.substring(0, 3)); 212 break; 213 } 214 cal.set(Calendar.MILLISECOND, f); 215 } 216 return e; 217 } 218 219 private static final boolean isChar(char c, String str, int off) { 220 return str.length() > off && str.charAt(off) == c; 221 } 222 223 private static final int readCharOpt(char c, Calendar cal, String str, int off) { 224 if (str.length() > off) { 225 if (str.charAt(off) == c) { 226 return off + 1; 227 } 228 } 229 return -1; 230 } 231 232 private final static boolean readTimeZone(Calendar cal, String str, int off) throws ParseException { 233 int len = str.length(); 234 if (len == off) { 235 return false; 236 } 237 char c = str.charAt(off); 238 if (c == 'Z') { 239 return true; 240 } 241 off++; 242 boolean plus = false; 243 if (c == '+') { 244 plus = true; 245 } else if (c != '-') { 246 throw new ParseException("Only Z, +, - prefixes are allowed in TZ", off); 247 } 248 int h = 0; 249 int m = 0; 250 int d = len - off; 251 if (d == 2) { 252 h = Integer.parseInt(str.substring(off, off + 2)); 253 } else if (d == 5) { 254 h = Integer.parseInt(str.substring(off, off + 2)); 255 m = Integer.parseInt(str.substring(off + 3, off + 5)); 256 // we do not check for ':'. we assume it is in the correct format 257 } else { 258 throw new ParseException("Invalid TZ in \"" + str + "\"", off); 259 } 260 261 if (plus) { 262 cal.add(Calendar.HOUR, -h); 263 cal.add(Calendar.MINUTE, -m); 264 } else { 265 cal.add(Calendar.HOUR, h); 266 cal.add(Calendar.MINUTE, m); 267 } 268 269 return true; 270 } 271 272}