1 package org.simantics.xml.sax.base.datatypes.literal;
\r
3 import java.text.ParseException;
\r
4 import java.text.SimpleDateFormat;
\r
5 import java.util.Calendar;
\r
6 import java.util.Date;
\r
7 import java.util.TimeZone;
\r
9 import org.simantics.databoard.Bindings;
\r
10 import org.simantics.databoard.binding.Binding;
\r
11 import org.simantics.databoard.util.Bean;
\r
13 public class DateTime extends Bean implements org.simantics.xml.sax.base.datatypes.adt.DateTime {
\r
15 public static final Binding BINDING = Bindings.getBindingUnchecked(DateTime.class);
\r
21 public byte minutes;
\r
22 public byte seconds;
\r
29 public DateTime(int year, int month, int day, int hours, int minutes, int seconds) {
\r
30 this(year, month, day, hours, minutes, seconds, java.lang.Integer.MIN_VALUE);
\r
33 public DateTime(int year, int month, int day, int hours, int minutes, int seconds, int offset) {
\r
35 if (month <= 0 || month > 12) throw new IllegalArgumentException("Month must be between 1 - 12, got " + month);
\r
36 if (day <= 0 || day > 31) throw new IllegalArgumentException("Day must be between 1 - 31, got " + day);
\r
37 if (hours < 0 || hours > 23) throw new IllegalArgumentException("Hours must be between 0 - 23, got " + hours);
\r
38 if (minutes < 0 || minutes > 59) throw new IllegalArgumentException("Minutes must be between 0 - 59, got " + minutes);
\r
39 if (seconds < 0 || seconds > 59) throw new IllegalArgumentException("Seconds must be between 0 - 59, got " + seconds);
\r
41 this.month = (byte)month;
\r
42 this.day = (byte)day;
\r
43 this.hours = (byte)hours;
\r
44 this.minutes = (byte)minutes;
\r
45 this.seconds = (byte)seconds;
\r
46 this.offset = offset;
\r
50 public int getYear() {
\r
55 public int getMonth() {
\r
60 public int getDate() {
\r
65 public int getHours() {
\r
70 public int getMinutes() {
\r
75 public int getSeconds() {
\r
80 public int getTimezoneOffset() {
\r
85 public Date getJavaDate() {
\r
86 Calendar c = Calendar.getInstance();
\r
87 c.set(year, month, day, hours, minutes, seconds);
\r
88 if (offset != Integer.MIN_VALUE) {
\r
89 c.setTimeZone(TimeZone.getTimeZone(TimeZone.getAvailableIDs(offset*60*1000)[0]));
\r
95 public int compare(org.simantics.xml.sax.base.datatypes.adt.DateTime o) {
\r
96 // FIXME: take account time offset
\r
97 if (getYear() != o.getYear())
\r
98 return getYear() - o.getYear();
\r
99 if (getMonth() != o.getMonth())
\r
100 return getMonth() - o.getMonth();
\r
101 if (getDate() != o.getDate())
\r
102 return getDate() - o.getDate();
\r
103 if (getHours() != o.getHours())
\r
104 return getHours() - o.getHours();
\r
105 if (getMinutes() != o.getMinutes())
\r
106 return getMinutes() - o.getMinutes();
\r
107 if (getSeconds() != o.getSeconds())
\r
108 return getSeconds() - o.getSeconds();
\r
112 public static DateTime parseDateTime(String dateString) {
\r
113 // FIXME: XML specification allows any number of second decimals!
\r
114 // FIXME: endOfDayFrag handling is missing.
\r
115 String[] formats = new String[]{"yyyy-MM-dd'T'HH:mm:ssXXX", "yyyy-MM-dd'T'HH:mm:ssSSSSSSSXXX","yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssSSSSSSS"};
\r
116 boolean[] timezone = new boolean[]{true, true,false,false};
\r
117 for (int i = 0; i< formats.length; i++) {
\r
118 String format = formats[i];
\r
120 SimpleDateFormat dateFormat = new SimpleDateFormat(format);
\r
121 java.util.Date n = dateFormat.parse(dateString);
\r
123 return new DateTime(n.getYear()+1900, n.getMonth()+1, n.getDate(), n.getHours(),n.getMinutes(), n.getSeconds(), n.getTimezoneOffset());
\r
125 return new DateTime(n.getYear()+1900, n.getMonth()+1, n.getDate(), n.getHours(),n.getMinutes(), n.getSeconds());
\r
126 } catch (ParseException e) {
\r
131 throw new IllegalArgumentException("Time is not in proper format " + dateString);
\r