Subversion Repositories XServices

Compare Revisions

Ignore whitespace Rev 97 → Rev 114

/xservices/trunk/src/java/net/brutex/xservices/ws/impl/DateServiceImpl.java
18,107 → 18,108
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
 
import javax.jws.WebService;
 
import net.brutex.xservices.types.DateFormatType;
import net.brutex.xservices.types.DateInfoExtendedType;
import net.brutex.xservices.types.DateInfoType;
import net.brutex.xservices.types.DateTimeUnits;
import net.brutex.xservices.types.TimeZoneType;
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
)
@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 DateInfoType getDate() throws XServicesFault {
GregorianCalendar c = new GregorianCalendar();
DateInfoType dateinfo = new DateInfoType(c, TimeZone.getDefault());
return dateinfo;
}
 
public DateInfoExtendedType getDateExtended() throws XServicesFault {
GregorianCalendar c = new GregorianCalendar();
DateInfoExtendedType dateinfo = new DateInfoExtendedType(c,
TimeZone.getDefault());
return dateinfo;
}
 
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;
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 getInTimezone(Date date, String timezone)
throws XServicesFault {
if (!isValidTimezone(timezone))
throw new XServicesFault(ERR_INVALIDTIMEZONE);
TimeZone targetzone = TimeZone.getTimeZone(timezone);
String targetstring = DateFormatType.ISO8601.format(date, null, targetzone);
return targetstring;
}
public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
return formatDateAdvanced(cal, format.format());
 
public String formatDate(Date cal, DateFormatType format)
throws XServicesFault {
return format.format(cal, null, null);
}
public String formatDateAdvanced(GregorianCalendar cal, String format)
 
public String formatDateAdvanced(Date cal, String format)
throws XServicesFault {
String result= null;
String result = null;
SimpleDateFormat f = new SimpleDateFormat(format);
result = f.format(cal);
return result;
}
 
public Date parseDate(String s, DateFormatType format, String timezone)
throws XServicesFault {
if (timezone == null | timezone.equals(""))
timezone = TimeZone.getDefault().getID();
if (!isValidTimezone(timezone))
throw new XServicesFault(ERR_INVALIDTIMEZONE);
try {
SimpleDateFormat f = new SimpleDateFormat(format);
result = f.format(cal.getTime());
} catch (IllegalArgumentException e) {
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
return format.parse(s, null, TimeZone.getTimeZone(timezone));
} catch (ParseException e) {
throw new XServicesFault(e);
}
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 {
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);
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) {
} catch (IllegalArgumentException e) {
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
} catch (ParseException e) {
throw new XServicesFault("Cannot parse date: "+ e.getMessage());
throw new XServicesFault("Cannot parse date: " + e.getMessage());
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone(timezone));
125,18 → 126,16
cal.setTime(date);
return cal;
}
public BigInteger dateTimeDiff(GregorianCalendar fromCal,
GregorianCalendar toCal) throws XServicesFault {
long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
 
public BigInteger dateTimeDiff(Date fromCal, Date toCal)
throws XServicesFault {
long diff = toCal.getTime() - fromCal.getTime();
BigInteger d = new BigInteger(String.valueOf(diff), 10);
return d;
}
public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
 
public BigInteger dateTimeDiff2(Date fromCal, Date toCal, DateTimeUnits unit)
throws XServicesFault {
BigInteger d = dateTimeDiff(fromCal, toCal);
switch (unit) {
case SECONDS:
150,13 → 149,16
break;
case DAYS:
d = d.divide(new BigInteger("86400000"));
break;
case YEARS:
d = d.divide(new BigInteger("31536000000"));
break;
}
return d;
}
public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
throws XServicesFault {
 
public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value,
DateTimeUnits unit) throws XServicesFault {
switch (unit) {
case SECONDS:
cal.add(GregorianCalendar.SECOND, value.intValue());
170,16 → 172,19
case DAYS:
cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
break;
case YEARS:
cal.add(GregorianCalendar.YEAR, 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)) {
for (String s : TimeZone.getAvailableIDs()) {
if (s.equals(id)) {
yes = true;
break;
}
187,4 → 192,11
return yes;
}
 
public List<TimeZoneType> getTimezones() {
List<TimeZoneType> output = new ArrayList<TimeZoneType>();
for (String s : TimeZone.getAvailableIDs()) {
output.add(new TimeZoneType(TimeZone.getTimeZone(s)));
}
return output;
}
}