Skip to content

Commit

Permalink
Replace UtcOffset with java.time.ZoneOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Nov 27, 2018
1 parent 5de0275 commit 7d18cb0
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 171 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/TimeZone.java
Expand Up @@ -96,7 +96,7 @@ public final int getOffset(final int era, final int year, final int month, final
final Observance observance = vTimeZone.getApplicableObservance(new DateTime(cal.getTime()));
if (observance != null) {
final TzOffsetTo offset = observance.getProperty(Property.TZOFFSETTO);
return (int) offset.getOffset().getOffset();
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
}
return 0;
}
Expand All @@ -108,10 +108,10 @@ public int getOffset(long date) {
final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
if (observance != null) {
final TzOffsetTo offset = observance.getProperty(Property.TZOFFSETTO);
if (offset.getOffset().getOffset() < getRawOffset()) {
if ((offset.getOffset().getTotalSeconds() * 1000L) < getRawOffset()) {
return getRawOffset();
} else {
return (int) offset.getOffset().getOffset();
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
}
}
return 0;
Expand Down Expand Up @@ -192,7 +192,7 @@ private static int getRawOffset(VTimeZone vt) {
if (latestSeasonalTime != null) {
final TzOffsetTo offsetTo = latestSeasonalTime.getProperty(Property.TZOFFSETTO);
if (offsetTo != null) {
return (int) offsetTo.getOffset().getOffset();
return (int) (offsetTo.getOffset().getTotalSeconds() * 1000L);
}
}
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/fortuna/ical4j/model/UtcOffset.java
Expand Up @@ -47,6 +47,7 @@
*
* @author Ben Fortuna
*/
@Deprecated
public class UtcOffset implements Serializable {

private static final long serialVersionUID = 5883111996721531728L;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/ZoneOffsetAdapter.java
@@ -0,0 +1,39 @@
package net.fortuna.ical4j.model;

import java.io.Serializable;
import java.time.ZoneOffset;

/**
* Support adapter for {@link java.time.ZoneOffset} to output in iCalendar format.
*/
public class ZoneOffsetAdapter implements Serializable {

private final ZoneOffset offset;

public ZoneOffsetAdapter(ZoneOffset offset) {
this.offset = offset;
}

public ZoneOffset getOffset() {
return offset;
}

@Override
public String toString() {
String retVal = "";
if (offset != null) {
int hours = Math.abs(offset.getTotalSeconds()) / (60 * 60);
if (offset.getTotalSeconds() < 0) {
hours = -hours;
}
int minutes = Math.abs(offset.getTotalSeconds()) % (60 * 60) / 60;
int seconds = Math.abs(offset.getTotalSeconds()) % (60 * 60) % 60;
if (seconds > 0) {
retVal = String.format("%+03d%02d%02d", hours, minutes, seconds);
} else {
retVal = String.format("%+03d%02d", hours, minutes);
}
}
return retVal;
}
}
Expand Up @@ -322,7 +322,7 @@ private DateTime calculateOnset(String dateStr) throws ParseException {

private DateTime applyOffsetFrom(DateTime orig) {
DateTime withOffset = new DateTime(true);
withOffset.setTime(orig.getTime() - getOffsetFrom().getOffset().getOffset());
withOffset.setTime(orig.getTime() - (getOffsetFrom().getOffset().getTotalSeconds() * 1000L));
return withOffset;
}
}
21 changes: 11 additions & 10 deletions src/main/java/net/fortuna/ical4j/model/property/TzOffsetFrom.java
Expand Up @@ -37,6 +37,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.ZoneOffset;

/**
* $Id$
Expand All @@ -51,7 +52,7 @@ public class TzOffsetFrom extends Property {

private static final long serialVersionUID = 450274263165493502L;

private UtcOffset offset;
private ZoneOffsetAdapter offset;

/**
* Default constructor.
Expand Down Expand Up @@ -80,32 +81,32 @@ public TzOffsetFrom(final ParameterList aList, final String aValue) {
/**
* @param anOffset a timezone offset in milliseconds
*/
public TzOffsetFrom(final UtcOffset anOffset) {
public TzOffsetFrom(final ZoneOffset anOffset) {
super(TZOFFSETFROM, new Factory());
offset = anOffset;
offset = new ZoneOffsetAdapter(anOffset);
}

/**
* @param aList a list of parameters for this component
* @param anOffset a timezone offset in milliseconds
*/
public TzOffsetFrom(final ParameterList aList, final UtcOffset anOffset) {
public TzOffsetFrom(final ParameterList aList, final ZoneOffset anOffset) {
super(TZOFFSETFROM, aList, new Factory());
offset = anOffset;
offset = new ZoneOffsetAdapter(anOffset);
}

/**
* @return Returns the offset.
*/
public final UtcOffset getOffset() {
return offset;
public final ZoneOffset getOffset() {
return offset.getOffset();
}

/**
* {@inheritDoc}
*/
public final void setValue(final String aValue) {
offset = new UtcOffset(aValue);
offset = new ZoneOffsetAdapter(ZoneOffset.of(aValue));
}

/**
Expand All @@ -121,8 +122,8 @@ public final String getValue() {
/**
* @param offset The offset to set.
*/
public final void setOffset(final UtcOffset offset) {
this.offset = offset;
public final void setOffset(final ZoneOffset offset) {
this.offset = new ZoneOffsetAdapter(offset);
}

@Override
Expand Down

0 comments on commit 7d18cb0

Please sign in to comment.