From 399fab0d33beb3ed1bbd6e383633eebbc59c95be Mon Sep 17 00:00:00 2001 From: Jacopo Cappellato Date: Tue, 19 Jun 2007 08:08:53 +0000 Subject: [PATCH] Applied patch by Adrian Crum to implement User Time Zone Selection Capability. OFBIZ-1079 git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@548646 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/ofbiz/base/util/UtilDateTime.java | 99 +++++++++---------- framework/common/config/general.properties | 5 +- .../src/org/ofbiz/common/CommonEvents.java | 25 +++++ .../common/webcommon/includes/header.ftl | 17 +++- framework/security/entitydef/entitymodel.xml | 1 + .../ofbiz/widget/screen/ScreenRenderer.java | 3 +- 6 files changed, 95 insertions(+), 55 deletions(-) diff --git a/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java b/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java index cb1e2429ac..d6adb618ce 100644 --- a/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java +++ b/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java @@ -24,8 +24,11 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; @@ -192,13 +195,7 @@ public class UtilDateTime { } public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp, int daysLater) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); - tempCal.add(Calendar.DAY_OF_MONTH, daysLater); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(0); - return retStamp; + return getDayStart(stamp, daysLater, TimeZone.getDefault(), Locale.getDefault()); } public static java.sql.Timestamp getNextDayStart(java.sql.Timestamp stamp) { @@ -210,13 +207,7 @@ public class UtilDateTime { } public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp, int daysLater) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59); - tempCal.add(Calendar.DAY_OF_MONTH, daysLater); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(999999999); - return retStamp; + return getDayEnd(stamp, daysLater, TimeZone.getDefault(), Locale.getDefault()); } /** @@ -237,15 +228,7 @@ public class UtilDateTime { return getYearStart(stamp, daysLater, 0, yearsLater); } public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater, int monthsLater, int yearsLater) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0); - tempCal.add(Calendar.YEAR, yearsLater); - tempCal.add(Calendar.MONTH, monthsLater); - tempCal.add(Calendar.DAY_OF_MONTH, daysLater); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(0); - return retStamp; + return getYearStart(stamp, daysLater, monthsLater, yearsLater, TimeZone.getDefault(), Locale.getDefault()); } public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater) { return getYearStart(stamp, (daysLater == null ? 0 : daysLater.intValue()), @@ -267,14 +250,7 @@ public class UtilDateTime { } public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp, int daysLater, int monthsLater) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), 1, 0, 0, 0); - tempCal.add(Calendar.DAY_OF_MONTH, daysLater); - tempCal.add(Calendar.MONTH, monthsLater); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(0); - return retStamp; + return getMonthStart(stamp, daysLater, monthsLater, TimeZone.getDefault(), Locale.getDefault()); } /** @@ -292,27 +268,11 @@ public class UtilDateTime { } public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp, int daysLater, int weeksLater) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); - tempCal.add(Calendar.DAY_OF_MONTH, daysLater); - tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek()); - tempCal.add(Calendar.WEEK_OF_MONTH, weeksLater); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(0); - return retStamp; + return getWeekStart(stamp, daysLater, weeksLater, TimeZone.getDefault(), Locale.getDefault()); } public static java.sql.Timestamp getWeekEnd(java.sql.Timestamp stamp) { - Calendar tempCal = Calendar.getInstance(); - tempCal.setTime(new java.util.Date(stamp.getTime())); - tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); - tempCal.add(Calendar.WEEK_OF_MONTH, 1); - tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek()); - tempCal.add(Calendar.SECOND, -1); - java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime()); - retStamp.setNanos(0); - return retStamp; + return getWeekEnd(stamp, TimeZone.getDefault(), Locale.getDefault()); } public static java.util.Calendar toCalendar(java.sql.Timestamp stamp) { @@ -744,8 +704,7 @@ public class UtilDateTime { * @return A int containing the week number */ public static int weekNumber(Timestamp input) { - Calendar calendar = Calendar.getInstance(); - return weekNumber(input, calendar.getFirstDayOfWeek()); + return weekNumber(input, TimeZone.getDefault(), Locale.getDefault()); } public static int weekNumber(Timestamp input, int startOfWeek) { @@ -945,4 +904,42 @@ public class UtilDateTime { return dateFormat.format(stamp); } + protected static List availableTimeZoneList = null; + /** Returns a List of available TimeZone objects. + * @see java.util.TimeZone + */ + public static List availableTimeZones() { + if (availableTimeZoneList == null) { + synchronized(UtilDateTime.class) { + if (availableTimeZoneList == null) { + availableTimeZoneList = new LinkedList(); + List idList = null; + String tzString = UtilProperties.getPropertyValue("general", "timeZones.available"); + if (tzString != null && tzString.length() > 0) { + idList = StringUtil.split(tzString, ","); + } else { + idList = Arrays.asList(TimeZone.getAvailableIDs()); + } + for (Iterator i = idList.iterator(); i.hasNext();) { + TimeZone curTz = TimeZone.getTimeZone((String)i.next()); + availableTimeZoneList.add(curTz); + } + } + } + } + return availableTimeZoneList; + } + + /** Returns a TimeZone object based upon a time zone ID. Method defaults to + * server's time zone if tzID is null or empty. + * @see java.util.TimeZone + */ + public static TimeZone toTimeZone(String tzId) { + if (UtilValidate.isEmpty(tzId)) { + return TimeZone.getDefault(); + } else { + return TimeZone.getTimeZone(tzId); + } + } + } diff --git a/framework/common/config/general.properties b/framework/common/config/general.properties index 7dddbcb1e8..742fd2eb08 100644 --- a/framework/common/config/general.properties +++ b/framework/common/config/general.properties @@ -26,9 +26,12 @@ currency.uom.id.default=USD # -- the default decimal format for currency currency.decimal.format=##0.00 -# -- locales made available separated by comma's +# -- locales made available separated by commas #locales.available=en,fr,nl +# -- time zone IDs made available separated by commas +#timeZones.available=US/Eastern,US/Central,US/Mountain,US/Pacific,US/Alaska,US/Hawaii + # -- the default country for drop downs country.geo.id.default=USA diff --git a/framework/common/src/org/ofbiz/common/CommonEvents.java b/framework/common/src/org/ofbiz/common/CommonEvents.java index 3e4ec2334a..7d9cdaefeb 100644 --- a/framework/common/src/org/ofbiz/common/CommonEvents.java +++ b/framework/common/src/org/ofbiz/common/CommonEvents.java @@ -187,6 +187,31 @@ public class CommonEvents { } } } + return setSessionTimeZone(request, response); + } + + /** Simple event to set the user's per-session time zone setting. */ + public static String setSessionTimeZone(HttpServletRequest request, HttpServletResponse response) { + String tzString = request.getParameter("tzId"); + if (UtilValidate.isNotEmpty(tzString)) { + + // update the UserLogin object + GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); + if (userLogin == null) { + userLogin = (GenericValue) request.getSession().getAttribute("autoUserLogin"); + } + + if (userLogin != null) { + GenericValue ulUpdate = GenericValue.create(userLogin); + ulUpdate.set("lastTimeZone", tzString); + try { + ulUpdate.store(); + userLogin.refreshFromCache(); + } catch (GenericEntityException e) { + Debug.logWarning(e, module); + } + } + } return "success"; } diff --git a/framework/common/webcommon/includes/header.ftl b/framework/common/webcommon/includes/header.ftl index d7caa29db1..c208ab7fb6 100644 --- a/framework/common/webcommon/includes/header.ftl +++ b/framework/common/webcommon/includes/header.ftl @@ -68,19 +68,32 @@ under the License. ${uiLabelMap.CommonWelcome}!

-

${Static["org.ofbiz.base.util.UtilDateTime"].nowTimestamp().toString()}

+ <#assign nowStamp = Static["org.ofbiz.base.util.UtilDateTime"].nowTimestamp()> + <#assign timeZone = Static["org.ofbiz.base.util.UtilDateTime"].toTimeZone((userLogin.lastTimeZone)?default(""))> +

${Static["org.ofbiz.base.util.UtilDateTime"].timeStampToString(nowStamp, timeZone, locale)}

+
+ <#assign displayStyle = Static["java.util.TimeZone"].LONG> +
diff --git a/framework/security/entitydef/entitymodel.xml b/framework/security/entitydef/entitymodel.xml index c19868fdf8..27ab87f191 100644 --- a/framework/security/entitydef/entitymodel.xml +++ b/framework/security/entitydef/entitymodel.xml @@ -71,6 +71,7 @@ under the License. + diff --git a/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java b/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java index b58e1eb77e..830129ffb5 100644 --- a/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java +++ b/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java @@ -123,8 +123,9 @@ public class ScreenRenderer { // make sure the "nullField" object is in there for entity ops; note this is nullField and not null because as null causes problems in FreeMarker and such... context.put("nullField", GenericEntity.NULL_FIELD); - // get all locale information + // get all locale & time zone information context.put("availableLocales", UtilMisc.availableLocales()); + context.put("availableTimeZones", UtilDateTime.availableTimeZones()); context.put("parameters", parameters); context.put("delegator", delegator);