Subversion Repositories XServices

Rev

Rev 68 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68 Rev 70
Line 42... Line 42...
42
			)
42
			)
43
public class DateServiceImpl implements DateService {
43
public class DateServiceImpl implements DateService {
Line 44... Line 44...
44
 
44
 
45
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
45
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
46
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
46
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
47
	@Override
47
	
48
	public GregorianCalendar getDate(String timezone) throws XServicesFault {		
48
	public GregorianCalendar getDate(String timezone) throws XServicesFault {		
49
		if (! isValidTimezone(timezone) ) {
49
		if (! isValidTimezone(timezone) ) {
50
			String valid_ids = "";
50
			String valid_ids = "";
51
			String[] tid = TimeZone.getAvailableIDs();
51
			String[] tid = TimeZone.getAvailableIDs();
Line 58... Line 58...
58
			if (timezone == null || timezone.length()<1 ) timezone = "GMT0";
58
			if (timezone == null || timezone.length()<1 ) timezone = "GMT0";
59
			GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
59
			GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
60
			return c;
60
			return c;
61
	}
61
	}
Line 62... Line 62...
62
 
62
 
63
	@Override
63
	
64
	public BigInteger getTimestamp() {
64
	public BigInteger getTimestamp() {
65
		Date d = new Date();
65
		Date d = new Date();
66
		long l = d.getTime();
66
		long l = d.getTime();
67
		BigInteger timestamp = new BigInteger(Long.toString(l));
67
		BigInteger timestamp = new BigInteger(Long.toString(l));
68
		return timestamp;
68
		return timestamp;
Line 69... Line 69...
69
	}
69
	}
70
 
70
 
71
	@Override
71
	
72
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
72
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
73
			String timezone) throws XServicesFault {
73
			String timezone) throws XServicesFault {
74
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
74
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
75
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
75
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
76
		c.setTimeInMillis(cal.getTimeInMillis());
76
		c.setTimeInMillis(cal.getTimeInMillis());
Line 77... Line 77...
77
		return c;
77
		return c;
78
	}
78
	}
79
	
79
	
80
	@Override
80
	
Line 81... Line 81...
81
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
81
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
82
		return formatDateAdvanced(cal, format.format());
82
		return formatDateAdvanced(cal, format.format());
83
	}
83
	}
84
	
84
	
85
	@Override
85
	
86
	public String formatDateAdvanced(GregorianCalendar cal, String format)
86
	public String formatDateAdvanced(GregorianCalendar cal, String format)
Line 93... Line 93...
93
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
93
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
94
		}
94
		}
95
		return result;
95
		return result;
96
	}
96
	}
Line 97... Line 97...
97
	
97
	
98
	@Override
98
	
99
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
99
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
100
		return parseDateAdvanced(s, format.format(), timezone);
100
		return parseDateAdvanced(s, format.format(), timezone);
Line 101... Line 101...
101
	}
101
	}
102
 
102
 
103
	@Override
103
	
104
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
104
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
105
		SimpleDateFormat f = null;
105
		SimpleDateFormat f = null;
106
		Date date = null;
106
		Date date = null;
Line 119... Line 119...
119
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
119
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
120
		cal.setTime(date);
120
		cal.setTime(date);
121
		return cal;
121
		return cal;
122
	}
122
	}
Line 123... Line 123...
123
	
123
	
124
	@Override
124
	
125
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
125
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
126
			GregorianCalendar toCal) throws XServicesFault {
126
			GregorianCalendar toCal) throws XServicesFault {
127
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
127
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
128
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
128
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
129
		return d;
129
		return d;
Line 130... Line 130...
130
	}
130
	}
131
	
131
	
132
	@Override
132
	
133
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
133
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
134
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
134
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
135
		BigInteger d = dateTimeDiff(fromCal, toCal);
135
		BigInteger d = dateTimeDiff(fromCal, toCal);
Line 147... Line 147...
147
			d = d.divide(new BigInteger("86400000"));
147
			d = d.divide(new BigInteger("86400000"));
148
		}
148
		}
149
		return d;
149
		return d;
150
	}
150
	}
Line 151... Line 151...
151
	
151
	
152
	@Override
152
	
153
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
153
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
154
			throws XServicesFault {
154
			throws XServicesFault {
155
		switch (unit) {
155
		switch (unit) {
156
		case SECONDS:
156
		case SECONDS: