0,0 → 1,190 |
/* |
* Copyright 2011 Brian Rosenberger (Brutex Network) |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
package net.brutex.xservices.ws.impl; |
|
import java.math.BigInteger; |
import java.text.ParseException; |
import java.text.SimpleDateFormat; |
import java.util.Date; |
import java.util.GregorianCalendar; |
import java.util.TimeZone; |
|
import javax.jws.WebService; |
|
import net.brutex.xservices.types.DateFormatType; |
import net.brutex.xservices.types.DateTimeUnits; |
import net.brutex.xservices.util.BrutexNamespaces; |
import net.brutex.xservices.ws.DateService; |
import net.brutex.xservices.ws.XServicesFault; |
|
|
/** |
* @author Brian Rosenberger |
* |
*/ |
@WebService( |
targetNamespace = BrutexNamespaces.WS_XSERVICES, |
endpointInterface = "net.brutex.xservices.ws.DateService", |
serviceName = DateService.SERVICE_NAME |
) |
public class DateServiceImpl implements DateService { |
|
private static String ERR_INVALIDFORMAT = "Invalid format pattern."; |
private static String ERR_INVALIDTIMEZONE = "Invalid timezone."; |
|
public GregorianCalendar getDate(String timezone) throws XServicesFault { |
if (! isValidTimezone(timezone) ) { |
String valid_ids = ""; |
String[] tid = TimeZone.getAvailableIDs(); |
for (String s : tid) { |
valid_ids += s + "\n"; |
} |
throw new XServicesFault("Please supply a valid timezone id or none. Valid timezones are:\n" + valid_ids, |
new Exception( )); |
} |
if (timezone == null || timezone.length()<1 ) timezone = "GMT0"; |
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone)); |
return c; |
} |
|
|
public BigInteger getTimestamp() { |
Date d = new Date(); |
long l = d.getTime(); |
return new BigInteger(Long.toString(l)); |
} |
|
public BigInteger getTimestamp2() { |
Date d = new Date(); |
long l = d.getTime()/1000; |
return new BigInteger(Long.toString(l)); |
} |
|
|
public GregorianCalendar getInTimezone(GregorianCalendar cal, |
String timezone) throws XServicesFault { |
if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE); |
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone)); |
c.setTimeInMillis(cal.getTimeInMillis()); |
return c; |
} |
|
|
public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault { |
return formatDateAdvanced(cal, format.format()); |
} |
|
|
public String formatDateAdvanced(GregorianCalendar cal, String format) |
throws XServicesFault { |
String result= null; |
try { |
SimpleDateFormat f = new SimpleDateFormat(format); |
result = f.format(cal.getTime()); |
} catch (IllegalArgumentException e) { |
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage()); |
} |
return result; |
} |
|
|
public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault { |
return parseDateAdvanced(s, format.format(), timezone); |
} |
|
|
public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault { |
SimpleDateFormat f = null; |
Date date = null; |
if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID(); |
if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE); |
|
try { |
f = new SimpleDateFormat(format); |
date = f.parse(s); |
} catch(IllegalArgumentException e) { |
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage()); |
} catch (ParseException e) { |
throw new XServicesFault("Cannot parse date: "+ e.getMessage()); |
} |
GregorianCalendar cal = new GregorianCalendar(); |
cal.setTimeZone(TimeZone.getTimeZone(timezone)); |
cal.setTime(date); |
return cal; |
} |
|
|
public BigInteger dateTimeDiff(GregorianCalendar fromCal, |
GregorianCalendar toCal) throws XServicesFault { |
long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis(); |
BigInteger d = new BigInteger(String.valueOf(diff), 10); |
return d; |
} |
|
|
public BigInteger dateTimeDiff2(GregorianCalendar fromCal, |
GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault { |
BigInteger d = dateTimeDiff(fromCal, toCal); |
switch (unit) { |
case SECONDS: |
d = d.divide(new BigInteger("1000")); |
break; |
case MINUTES: |
d = d.divide(new BigInteger("60000")); |
break; |
case HOURS: |
d = d.divide(new BigInteger("3600000")); |
break; |
case DAYS: |
d = d.divide(new BigInteger("86400000")); |
} |
return d; |
} |
|
|
public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit) |
throws XServicesFault { |
switch (unit) { |
case SECONDS: |
cal.add(GregorianCalendar.SECOND, value.intValue()); |
break; |
case MINUTES: |
cal.add(GregorianCalendar.MINUTE, value.intValue()); |
break; |
case HOURS: |
cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue()); |
break; |
case DAYS: |
cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue()); |
break; |
default: |
cal.add(GregorianCalendar.MILLISECOND, value.intValue()); |
} |
return cal; |
} |
|
private boolean isValidTimezone(String id) { |
boolean yes = false; |
for( String s: TimeZone.getAvailableIDs()) { |
if(s.equals(id)) { |
yes = true; |
break; |
} |
} |
return yes; |
} |
|
} |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |