001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/datatypes/time/TimeDuration.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.datatypes.time; 037 038 import java.io.Serializable; 039 040 /** 041 * 042 * 043 * @version $Revision: 18195 $ 044 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 045 * @author last edited by: $Author: mschneider $ 046 * 047 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 048 */ 049 public class TimeDuration implements Cloneable, Serializable { 050 051 /** 052 * 053 */ 054 private static final long serialVersionUID = 1L; 055 056 private int years = 0; 057 058 private int month = 0; 059 060 private int days = 0; 061 062 private int hours = 0; 063 064 private int minutes = 0; 065 066 private int seconds = 0; 067 068 private int millis = 0; 069 070 /** 071 * @param years 072 * @param month 073 * @param days 074 */ 075 public TimeDuration( int years, int month, int days ) { 076 this.years = years; 077 this.month = month; 078 this.days = days; 079 } 080 081 /** 082 * @param hours 083 * @param minutes 084 */ 085 public TimeDuration( int hours, int minutes ) { 086 this.hours = hours; 087 this.minutes = minutes; 088 } 089 090 /** 091 * @param years 092 * @param month 093 * @param days 094 * @param hours 095 * @param minutes 096 */ 097 public TimeDuration( int years, int month, int days, int hours, int minutes ) { 098 this.years = years; 099 this.month = month; 100 this.days = days; 101 this.hours = hours; 102 this.minutes = minutes; 103 } 104 105 /** 106 * @param years 107 * @param month 108 * @param days 109 * @param hours 110 * @param minutes 111 * @param seconds 112 * @param millis 113 */ 114 public TimeDuration( int years, int month, int days, int hours, int minutes, int seconds, int millis ) { 115 this.years = years; 116 this.month = month; 117 this.days = days; 118 this.hours = hours; 119 this.minutes = minutes; 120 this.seconds = seconds; 121 this.millis = millis; 122 } 123 124 /** 125 * 126 * @param time 127 * String as defined in W3C XSD simple datatypes section, e.g.: P1Y2M3DT10H30M; 128 * P1Y2MT2H; P0Y1347M0D 129 * @return corresponding <code>TimeDuration</code> instance 130 */ 131 public static TimeDuration createTimeDuration( String time ) { 132 133 int y = 0; 134 int mon = 0; 135 int d = 0; 136 int h = 0; 137 int min = 0; 138 int s = 0; 139 140 // remove leading 'P' 141 time = time.substring( 1 ); 142 if ( !time.startsWith( "T" ) ) { 143 int pos = time.indexOf( 'Y' ); 144 if ( pos > -1 ) { 145 String tmp = time.substring( 0, pos ); 146 y = Integer.parseInt( tmp ); 147 time = time.substring( pos + 1 ); 148 } 149 pos = time.indexOf( 'M' ); 150 if ( pos > -1 ) { 151 String tmp = time.substring( 0, pos ); 152 mon = Integer.parseInt( tmp ); 153 time = time.substring( pos + 1 ); 154 } 155 pos = time.indexOf( 'D' ); 156 if ( pos > -1 ) { 157 String tmp = time.substring( 0, pos ); 158 d = Integer.parseInt( tmp ); 159 time = time.substring( pos + 1 ); 160 } 161 } 162 163 if ( time.length() > 0 ) { 164 // remove leading 'T' 165 time = time.substring( 1 ); 166 int pos = time.indexOf( 'H' ); 167 if ( pos > -1 ) { 168 String tmp = time.substring( 0, pos ); 169 h = Integer.parseInt( tmp ); 170 time = time.substring( pos + 1 ); 171 } 172 pos = time.indexOf( 'M' ); 173 if ( pos > -1 ) { 174 String tmp = time.substring( 0, pos ); 175 min = Integer.parseInt( tmp ); 176 time = time.substring( pos + 1 ); 177 } 178 pos = time.indexOf( 'S' ); 179 if ( pos > -1 ) { 180 String tmp = time.substring( 0, pos ); 181 s = Integer.parseInt( tmp ); 182 time = time.substring( pos + 1 ); 183 } 184 } 185 186 return new TimeDuration( y, mon, d, h, min, s, 0 ); 187 } 188 189 /** 190 * @return Returns the days. 191 * 192 */ 193 public int getDays() { 194 return days; 195 } 196 197 /** 198 * @param days 199 * The days to set. 200 * 201 */ 202 public void setDays( int days ) { 203 this.days = days; 204 } 205 206 /** 207 * @return Returns the hours. 208 * 209 */ 210 public int getHours() { 211 return hours; 212 } 213 214 /** 215 * @param hours 216 * The hours to set. 217 * 218 */ 219 public void setHours( int hours ) { 220 this.hours = hours; 221 } 222 223 /** 224 * @return Returns the millis. 225 * 226 */ 227 public int getMillis() { 228 return millis; 229 } 230 231 /** 232 * @param millis 233 * The millis to set. 234 * 235 */ 236 public void setMillis( int millis ) { 237 this.millis = millis; 238 } 239 240 /** 241 * @return Returns the minutes. 242 * 243 */ 244 public int getMinutes() { 245 return minutes; 246 } 247 248 /** 249 * @param minutes 250 * The minutes to set. 251 * 252 */ 253 public void setMinutes( int minutes ) { 254 this.minutes = minutes; 255 } 256 257 /** 258 * @return Returns the month. 259 * 260 */ 261 public int getMonth() { 262 return month; 263 } 264 265 /** 266 * @param month 267 * The month to set. 268 * 269 */ 270 public void setMonth( int month ) { 271 this.month = month; 272 } 273 274 /** 275 * @return Returns the seconds. 276 * 277 */ 278 public int getSeconds() { 279 return seconds; 280 } 281 282 /** 283 * @param seconds 284 * The seconds to set. 285 * 286 */ 287 public void setSeconds( int seconds ) { 288 this.seconds = seconds; 289 } 290 291 /** 292 * @return Returns the years. 293 * 294 */ 295 public int getYears() { 296 return years; 297 } 298 299 /** 300 * @param years 301 * The years to set. 302 * 303 */ 304 public void setYears( int years ) { 305 this.years = years; 306 } 307 308 /** 309 * returns a duration a milli seconds 310 * 311 * @return a duration a milli seconds 312 */ 313 public long getAsMilliSeconds() { 314 long l = 0; 315 l = l + ( years * 31536000000l ); 316 l = l + ( month * 2592000000l ); 317 l = l + ( days * 86400000l ); 318 l = l + ( hours * 3600000l ); 319 l = l + ( minutes * 60000l ); 320 l = l + ( seconds * 1000l ); 321 l = l + millis; 322 return l; 323 } 324 325 /** 326 * return format: P1Y2M3DT10H30M10S 327 * 328 * @return String corresponding to this <code>TimeDuration</code> 329 */ 330 public String getAsGMLTimeDuration() { 331 StringBuffer sb = new StringBuffer( 150 ); 332 // P1Y2M3DT10H30M 333 sb.append( 'P' ).append( years ).append( 'Y' ).append( month ).append( 'M' ).append( days ).append( "DT" ).append( 334 hours ).append( 335 'H' ).append( 336 minutes ).append( 337 'M' ).append( 338 seconds ).append( 339 'S' ); 340 return sb.toString(); 341 } 342 343 @Override 344 public Object clone() { 345 return new TimeDuration( years, month, days, hours, minutes, seconds, millis ); 346 } 347 }