Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java,v diff -u -r1.13 -r1.14 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java 14 Apr 2005 04:43:59 -0000 1.13 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java 14 Apr 2005 05:51:52 -0000 1.14 @@ -10,6 +10,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; import org.lamsfoundation.lams.learningdesign.exception.ActivityBehaviorException; import org.lamsfoundation.lams.learningdesign.strategy.ScheduleGateActivityStrategy; +import org.lamsfoundation.lams.util.DateUtil; /** @@ -251,14 +252,13 @@ */ public Date getRealGateOpenTime() { - - Calendar openTime = new GregorianCalendar(TimeZone.getDefault()); - long offset = openTime.get(Calendar.ZONE_OFFSET)+openTime.get(Calendar.DST_OFFSET); + Calendar openTime = new GregorianCalendar(TimeZone.getDefault()); //compute the real opening time based on the if(isScheduledByTimeOffset()) openTime.add(Calendar.MINUTE,getGateStartTimeOffset().intValue()); else if(isScheduledByDateTime()) - openTime.setTime(new Date(getGateStartDateTime().getTime()+offset)); + openTime.setTime(DateUtil.convertFromUTCToLocal(TimeZone.getDefault(), + getGateStartDateTime())); else throw new ActivityBehaviorException("No way of scheduling has " + "been setup - this usually should be done at authoring " + @@ -286,7 +286,8 @@ if(isScheduledByTimeOffset()) closeTime.add(Calendar.MINUTE,getGateEndTimeOffset().intValue()); else if(isScheduledByDateTime()) - closeTime.setTime(new Date(getGateEndDateTime().getTime()+offset)); + closeTime.setTime(DateUtil.convertFromUTCToLocal(TimeZone.getDefault(), + getGateEndDateTime())); else throw new ActivityBehaviorException("No way of scheduling has " + "been setup - this usually should be done at authoring " + Index: lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java 14 Apr 2005 05:51:52 -0000 1.1 @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * ***********************************************************************/ + +package org.lamsfoundation.lams.util; + +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + + +/** + * Date utility class that helps the conversion between time + * + * @author Jacky Fang + * @since 2005-4-14 + * @version 1.1 + * + */ +public class DateUtil +{ + + /** + * Convert your local time to Universal Time Coordinator. + * @param time your local time + * @return the date UTC time which is the same as GMT. + */ + public static Date convertToUTC(Date time) + { + TimeZone gmt = TimeZone.getTimeZone("Etc/GMT"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); + sdf.setTimeZone(gmt); + String str = sdf.format(time); + Timestamp gmtLocal = Timestamp.valueOf(str); + + return new Date(gmtLocal.getTime()); + } + + /** + * Convert from UTC time to your local time. Note: it is your + * responsibility to pass in the correct UTC date. + * + * @param localTimeZone your local time zone. + * @param UTCDate the utc date. + * @return your local date time. + */ + public static Date convertFromUTCToLocal(TimeZone localTimeZone,Date UTCDate) + { + Calendar canlendar = new GregorianCalendar(localTimeZone); + long offset = canlendar.get(Calendar.ZONE_OFFSET)+canlendar.get(Calendar.DST_OFFSET); + + return new Date(UTCDate.getTime()+offset); + + } +} Index: lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/test/java/org/lamsfoundation/lams/learningdesign/Attic/TestScheduleGateActivity.java,v diff -u -r1.1 -r1.2 --- lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java 14 Apr 2005 04:43:59 -0000 1.1 +++ lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java 14 Apr 2005 05:51:52 -0000 1.2 @@ -22,14 +22,11 @@ package org.lamsfoundation.lams.learningdesign; -import java.text.SimpleDateFormat; import java.util.Calendar; -import java.util.Date; -import java.sql.Timestamp; import java.util.GregorianCalendar; -import java.util.TimeZone; import org.apache.log4j.Logger; +import org.lamsfoundation.lams.util.DateUtil; import junit.framework.TestCase; @@ -67,12 +64,8 @@ //convert current system time to utc Calendar now = new GregorianCalendar(); - TimeZone gmt = TimeZone.getTimeZone("Etc/GMT"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); - sdf.setTimeZone(gmt); - String str = sdf.format(now.getTime()); - Timestamp gmtNow = Timestamp.valueOf(str); - now.setTime(new Date(gmtNow.getTime())); + + now.setTime(DateUtil.convertToUTC(now.getTime())); gateByDateTime.setGateStartDateTime(now.getTime()); now.add(Calendar.MINUTE,END_TIME_OFFSET.intValue()); @@ -110,12 +103,25 @@ public void testGetRealGateCloseTime() { assertNotNull("verify real open time",gateByTimeOffset.getRealGateCloseTime()); - log.info("gate open time offset -- "+gateByTimeOffset.getGateEndTimeOffset()); - log.info("Gate Close time by offset --"+gateByTimeOffset.getRealGateCloseTime()); + log.info("gate close time offset -- "+gateByTimeOffset.getGateEndTimeOffset()); + log.info("gate close time by offset --"+gateByTimeOffset.getRealGateCloseTime()); assertNotNull("verify real close time by date time",gateByDateTime.getRealGateCloseTime()); log.info("gate close date time(UTC) -- "+gateByDateTime.getGateEndDateTime()); log.info("Real gate close time by date time -- "+gateByDateTime.getRealGateCloseTime()); } + public void testGetRealCloseTimeWithLongOffset() + { + Calendar cal = new GregorianCalendar(); + cal.setTime(gateByDateTime.getGateEndDateTime()); + cal.add(Calendar.MINUTE,END_TIME_OFFSET.intValue()*24); + gateByDateTime.setGateEndDateTime(cal.getTime()); + + assertNotNull("verify real close time by date time",gateByDateTime.getRealGateCloseTime()); + + log.info("gate close date time(UTC) -- "+gateByDateTime.getGateEndDateTime()); + log.info("Real gate close time by date time -- "+gateByDateTime.getRealGateCloseTime()); + + } }