Skip to content

Commit

Permalink
Support for UTC as default timezone for DateTime instances
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jan 14, 2020
1 parent dff1081 commit c754e47
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/main/java/net/fortuna/ical4j/model/DateTime.java
Expand Up @@ -301,7 +301,7 @@ public DateTime(final String value) throws ParseException {
/**
* Creates a new date-time instance from the specified value in the given
* timezone. If a timezone is not specified, the default timezone (as
* returned by {@link java.util.TimeZone#getDefault()}) is used.
* returned by {@link TimeZones#getDefault()}) is used.
*
* @param value
* a string representation of a date-time
Expand All @@ -313,8 +313,7 @@ public DateTime(final String value) throws ParseException {
public DateTime(final String value, final TimeZone timezone)
throws ParseException {
// setting the time to 0 since we are going to reset it anyway
super(0, Dates.PRECISION_SECOND, timezone != null ? timezone
: java.util.TimeZone.getDefault());
super(0, Dates.PRECISION_SECOND, timezone != null ? timezone : TimeZones.getDefault());
this.time = new Time(getTime(), getFormat().getTimeZone());

try {
Expand Down Expand Up @@ -368,8 +367,7 @@ public DateTime(final String value, final TimeZone timezone)
public DateTime(String value, String pattern, TimeZone timezone)
throws ParseException {
// setting the time to 0 since we are going to reset it anyway
super(0, Dates.PRECISION_SECOND, timezone != null ? timezone
: java.util.TimeZone.getDefault());
super(0, Dates.PRECISION_SECOND, timezone != null ? timezone : TimeZones.getDefault());
this.time = new Time(getTime(), getFormat().getTimeZone());

final DateFormat format = CalendarDateFormatFactory
Expand Down Expand Up @@ -483,7 +481,7 @@ public final void setTimeZone(final TimeZone timezone) {
private void resetTimeZone() {
// use GMT timezone to avoid daylight savings rules affecting floating
// time values..
getFormat().setTimeZone(TimeZone.getDefault());
getFormat().setTimeZone(TimeZones.getDefault());
// getFormat().setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID));
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/fortuna/ical4j/util/TimeZones.java
Expand Up @@ -80,7 +80,14 @@ public static boolean isUtc(final TimeZone timezone) {
return UTC_ID.equals(timezone.getID())
|| IBM_UTC_ID.equals(timezone.getID());
}


public static TimeZone getDefault() {
if ("true".equals(Configurator.getProperty("net.fortuna.ical4j.timezone.utcDefault").orElse("false"))) {
return getUtcTimeZone();
}
return TimeZone.getDefault();
}

/**
* Although timezones are not really applicable to DATE instances in iCalendar, the implementation
* in iCal4j requires the use of a timezone. Dates in iCal4j may be either "floating", in that they
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/ical4j.properties
Expand Up @@ -45,6 +45,7 @@ net.fortuna.ical4j.timezone.update.enabled=false
#net.fortuna.ical4j.timezone.cache.impl=net.fortuna.ical4j.util.MapTimeZoneCache

#net.fortuna.ical4j.timezone.date.floating={true|false}
#net.fortuna.ical4j.timezone.utcDefault={true|false}

#net.fortuna.ical4j.factory.decoder=net.fortuna.ical4j.util.DefaultDecoderFactory

Expand Down

0 comments on commit c754e47

Please sign in to comment.