Skip to content

Commit

Permalink
SEAMINTL-8 Support JDK TimeZone in addition to Joda DateTimeZone
Browse files Browse the repository at this point in the history
Add support for JDK TimeZone classes and provide a clearer distinction between it and Joda DateTimeZone classes
  • Loading branch information
kenfinnigan committed Nov 8, 2010
1 parent 0d9544a commit b1861af
Show file tree
Hide file tree
Showing 25 changed files with 1,015 additions and 294 deletions.
1 change: 1 addition & 0 deletions api/pom.xml
Expand Up @@ -23,6 +23,7 @@
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Expand Up @@ -19,7 +19,7 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.international.timezone;
package org.jboss.seam.international.datetimezone;

import java.util.Locale;
import java.util.TimeZone;
Expand All @@ -31,7 +31,7 @@
*
* @author Ken Finnigan
*/
public abstract class ForwardingTimeZone extends DateTimeZone
public abstract class ForwardingDateTimeZone extends DateTimeZone
{
/**
* Abstract getter for the delegate
Expand All @@ -40,7 +40,7 @@ public abstract class ForwardingTimeZone extends DateTimeZone
*/
protected abstract DateTimeZone delegate();

protected ForwardingTimeZone(String id)
protected ForwardingDateTimeZone(String id)
{
super(id);
}
Expand Down
@@ -0,0 +1,120 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.international.jdktimezone;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
* Delegating TimeZone.
*
* @author Ken Finnigan
*/
public abstract class ForwardingTimeZone extends TimeZone
{
/**
* Abstract getter for the delegate
*
* @return The delegate
*/
protected abstract TimeZone delegate();

protected ForwardingTimeZone(String id)
{
super();
setID(id);
}

@Override
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
{
return delegate().getOffset(era, year, month, day, dayOfWeek, milliseconds);
}

@Override
public int getOffset(long date)
{
return delegate().getOffset(date);
}

@Override
public int getRawOffset()
{
return delegate().getRawOffset();
}

@Override
public String getID()
{
return delegate().getID();
}

@Override
public String getDisplayName(boolean daylight, int style, Locale locale)
{
return delegate().getDisplayName(daylight, style, locale);
}

@Override
public int getDSTSavings()
{
return delegate().getDSTSavings();
}

@Override
public boolean useDaylightTime()
{
return delegate().useDaylightTime();
}

@Override
public boolean inDaylightTime(Date date)
{
return delegate().inDaylightTime(date);
}

@Override
public boolean hasSameRules(TimeZone other)
{
return delegate().hasSameRules(other);
}

@Override
public int hashCode()
{
return delegate().hashCode();
}

@Override
public boolean equals(Object obj)
{
return delegate().equals(obj);
}

@Override
public String toString()
{
return delegate().toString();
}

}
5 changes: 5 additions & 0 deletions impl/pom.xml
Expand Up @@ -40,6 +40,11 @@
<artifactId>ocpsoft-pretty-time</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<optional>true</optional>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
@@ -0,0 +1,97 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.international.datetimezone;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

import org.jboss.seam.international.datetimezone.ForwardingDateTimeZone;
import org.joda.time.DateTimeZone;

/**
* <p>
* Seam component that provides a list of time zones, limited to time zones with
* IDs in the form Continent/Place, excluding deprecated three-letter time zone
* IDs. The time zones returned have a fixed offset from UTC, which takes
* daylight savings time into account. For example, Europe/Amsterdam is UTC+1;
* in winter this is GMT+1 and in summer GMT+2.
* </p>
*
* <p>
* The time zone objects returned are wrapped in an implementation of TimeZone
* that provides a more friendly interface for accessing the time zone
* information. In particular, this type provides a more bean-friend property
* for the time zone id (id than ID) and provides a convenience property named
* label that formats the time zone for display in the UI. This wrapper can be
* disabled by setting the component property wrap to false.
* </p>
*
* @author Peter Hilton, Lunatech Research
* @author Dan Allen
*/
@ApplicationScoped
public class AvailableDateTimeZones
{
private static final String TIMEZONE_ID_PREFIXES = "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";

@Produces
private List<ForwardingDateTimeZone> dateTimeZones = null;

@SuppressWarnings("unchecked")
@PostConstruct
public void init()
{
dateTimeZones = new ArrayList<ForwardingDateTimeZone>();
final Set timeZoneIds = DateTimeZone.getAvailableIDs();
for (Object object : timeZoneIds)
{
String id = (String) object;
if (id.matches(TIMEZONE_ID_PREFIXES))
{
final DateTimeZone dtz = DateTimeZone.forID(id);
dateTimeZones.add(new ForwardingDateTimeZone(id)
{
@Override
protected DateTimeZone delegate()
{
return dtz;
}
});
}
}
Collections.sort(dateTimeZones, new Comparator<ForwardingDateTimeZone>()
{
public int compare(final ForwardingDateTimeZone a, final ForwardingDateTimeZone b)
{
return a.getID().compareTo(b.getID());
}
});
dateTimeZones = Collections.unmodifiableList(dateTimeZones);
}
}
@@ -0,0 +1,94 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.seam.international.datetimezone;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.logging.Logger;
import org.jboss.seam.international.datetimezone.ForwardingDateTimeZone;
import org.jboss.seam.international.timezone.DefaultTimeZoneConfig;
import org.joda.time.DateTimeZone;

/**
* Default DateTimeZone of the application. If configuration of the default DateTimeZone is found that will be used, otherwise
* the JVM default TimeZone.
*
* @author Ken Finnigan
*/

@ApplicationScoped
public class DefaultDateTimeZoneProducer implements Serializable
{
private static final long serialVersionUID = 6181892144731122500L;

@Inject
@DefaultTimeZoneConfig
private Instance<String> defaultTimeZoneId;

private final Logger log = Logger.getLogger(DefaultDateTimeZoneProducer.class);

@Produces
@Named
private DateTimeZone defaultDateTimeZone = null;

@PostConstruct
public void init()
{
if (!defaultTimeZoneId.isUnsatisfied())
{
try
{
String id = defaultTimeZoneId.get();
DateTimeZone dtz = DateTimeZone.forID(id);
defaultDateTimeZone = constructTimeZone(dtz);
}
catch (IllegalArgumentException e)
{
log.warn("DefaultDateTimeZoneProducer: Default TimeZone Id of " + defaultTimeZoneId + " was not found");
}
}
if (null == defaultDateTimeZone)
{
DateTimeZone dtz = DateTimeZone.getDefault();
defaultDateTimeZone = constructTimeZone(dtz);
}
}

private ForwardingDateTimeZone constructTimeZone(final DateTimeZone dtz)
{
return new ForwardingDateTimeZone(dtz.getID())
{
@Override
protected DateTimeZone delegate()
{
return dtz;
}
};
}
}

0 comments on commit b1861af

Please sign in to comment.