openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fazi <faisal.ans...@gmail.com>
Subject Re: How do I persist timestamp in UTC timezone?
Date Wed, 18 Mar 2009 19:02:06 GMT

Thank you very much everyone for your detailed explanations. I took Fay’s
suggestion by setting the VM default timezone to UTC  <<
TimeZone.setDefault(TimeZone.getTimeZone("UTC")) >>. It allowed me to
read/write the timestamps correctly in UTC. 

However, I was under the impression that Calendar support is provided so its
timezone can be used when date is formatted for persistence, that does not
seem to be the case. Setting the VM default timezone to UTC works just as
well for Date objects so I switched the data type from Calendar to Date (I
do no thave incentive to use Calendar objects anymore).  

I ended up debugging the code a little to see how Calendar instance is
persisted. Based on what I found I see that Calendar timezone does not
factor in during persistence. Below is some explanation on the insight I got
after debugging, plus a suggestion for Calendar persistence I thought to
share in case if it adds value.

When persisting Calendar objects the application takes the following path
(openJPA version 1.1.0):

1.	The control first goes into 
org.apache.openjpa.jdbc.DBDictionary.setCalendar()method. This method is a
wrapper around setDate() method. Here the Calendar.getTime() value is passed
into the setDate() method, Calendar timezone information gets left behind: 
                        setDate(stmnt, idx, val.getTime(), col);

2.	If the table column type is TIMESTAMP (as in my case), the setDate()
method makes the setTimestamp() call (line 942). Here the Calendar value is
passed in as null (we are now working with Date object only):
                       setTimestamp(stmnt, idx, new
Timestamp(val.getTime()), null, col);

3.	The setTimestamp() method sets the date in PreparedStatement at line
1111:
                       stmnt.setTimestamp(idx, val);

If Calendar was not null then the application would have set that in
PreparedStatement as well (line 1113), in which case the user defined
timezone value would have been used by the PreparedStatement when formatting
the date.
                       stmnt.setTimestamp(idx, val, cal);
	

So I think to take advantage of the Calendar timezone the setCalendar()
method can be changed from a wrapper of setDate() to an implementation that
is similar to setDate() implementation. The only difference would be that
the user provided Calendar value is passed into the setTimestamp() method
which will eventually makes its way into the PreparedStatement. The
implementation could look something like this (the differences between
setDate() and the proposed setCalendar method are marked by >>> markers): 

    /**
     * Set the given value as a parameter to the statement.
     */
    public void setCalendar(PreparedStatement stmnt, int idx, Calendar val,
        Column col)
        throws SQLException {
        if (col != null && col.getType() == Types.DATE)
            setDate(stmnt, idx, new java.sql.Date(val.getTime()), null,
col);
        else if (col != null && col.getType() == Types.TIME)
            setTime(stmnt, idx, new Time(val.getTime()), null, col);
        else if (val instanceof Timestamp)
>>>         setTimestamp(stmnt, idx,(Timestamp) val, val, col);   
        else
>>>         setTimestamp(stmnt, idx, new Timestamp(val.getTime()), val,
>>> col);    }


Please note here that the argument ‘val’ (which is a Calendar) is being
passed into the setTimestamp() method (instead of null), which will make its
way into the PreparedStatement.  

I only looked the ‘write’ side of this problem, I did not debug to see how
the application is reading back the timestamp and whether it has information
on which timezone to use when reading back the date (so we get back the
correct milliseconds). This part is pending investigation, I will update it
if I find more information on that. 

Thanks

-fazi


Paul Copeland wrote:
> 
> Fay -
> 
> Yes! you got it.  And that is why java.sql.Timestamp or java.util.Date 
> values interoperate correctly between timezones without any 
> adjustments.  If I serialize a Date and save it in a file and email you 
> that file, you will get the same Date but the time with toString() will 
> be corrected for your default TimeZone.  Date.getTime() milliseconds for 
> the serialized Date will be the same on both computers.
> 
> If you persist date or timestamp values with mixed TimeZones then the 
> times should show up differently in the database. In other words, if you 
> create a Calendar object 3:00 PM PST and another Calendar object 3:00 PM 
> UTC they should differ by 8 hours when you view them in the database.  
> Changing the OS timezone should have no effect on the underlying UTC 
> Date or Timestamp values persisted with JDBC.
> 
> - Paul
> 
> On 3/17/2009 6:44 PM, Fay Wang wrote:
>> Hi Adam,
>>    Thank you for the clarification. That makes sense. I also observe that
>> the  milliseconds value from the java Data/Calendar object is the same
>> regardless of the timezone setting. So we can not actually "persist
>> timestamp in UTC timezone" in the database with timestamp column, right?
>> (because database timestamp column is timezone indepenent, and also the
>> java timestamp value is already normalized with UTC based on the O/S time
>> zone setting). 
>>
>>
>> --- On Tue, 3/17/09, Adam Hardy <adam.sql@cyberspaceroad.com> wrote:
>>
>>   
>>> From: Adam Hardy <adam.sql@cyberspaceroad.com>
>>> Subject: Re: How do I persist timestamp in UTC timezone?
>>> To: users@openjpa.apache.org
>>> Date: Tuesday, March 17, 2009, 5:15 PM
>>> Hello Fay,
>>>
>>> no reason to worry, it is logical that the time saved to
>>> the database depends on the default time zone in Java. If
>>> you don't set the default time zone, it uses the time in
>>> your operating system.
>>>
>>> I did say that the database field TIMESTAMP default is
>>> without time zone - you can specify that it does save the
>>> timezone, but that is an optional extra and database vendor
>>> specific (I think).
>>>
>>> The issue here is that Java times and dates are
>>> time-zone-independent, because they are held as milliseconds
>>> past 1970-01-01 GMT/UTC
>>>
>>> As Paul said, your database stores time-zone-dependent
>>> values (i.e. times without a timezone) and the time zone
>>> that they are dependent on is the time zone of your
>>> operating system, because JDBC uses that to work out what
>>> the millisecond past 1970 value is. So if you change your OS
>>> timezone, you change all the timestamps in your database -
>>> as far as Java is concerned.
>>>
>>> I figure that this not optimal, since you have to be
>>> careful if you move a database from a host in one timezone
>>> to a host in another timezone, but how often does that
>>> happen?
>>>
>>>
>>> Fay Wang on 17/03/09 23:41, wrote:
>>>     
>>>> To my knowledge, as far as DB2 is concerned, DB2 v9
>>>>       
>>> and below does not have "timestamp with time zone"
>>> data type. The value stored in the DB2 timestamp column is
>>> without time zone information. It is up to the application
>>> to determine the time zone. For example, in my db2 table, I
>>> have the following value 
>>>     
>>>>        2009-03-17-22.05.37.569000 
>>>> stored in my timestamp column.
>>>>
>>>> If I set the default time zone to UTC in my
>>>>       
>>> application, I get the value  back as: 
>>>     
>>>> Created time        : Tue Mar 17 22:05:37 UTC 2009
>>>>
>>>> If I did not set the default time zone to UTC, I get
>>>>       
>>> this value:
>>>     
>>>> Created time        : Tue Mar 17 22:05:37 PDT 2009
>>>>
>>>> A new data type "Timestamp with time zone"
>>>>       
>>> may be introduced in the next DB2 release, but currently
>>> there is no way to store the time zone information in the
>>> timestamp column. DB2 experts, please correct me if I am
>>> wrong.
>>>     
>>>> Having said that, with this statement,     
>>>>       
>>> 
>>> dt.setCreatedTime(Calendar.getInstance(TimeZone.getTimeZone("Etc/UTC")));
>>>     
>>>> when application sets default time zone to UTC, the
>>>>       
>>> timestamp value in the database becomes
>>> "2009-03-17-23.26.53.320000". Without setting the
>>> default time zone, the timestamp value in the database is
>>> "2009-03-17-16.23.27.494000". Let me try a simple
>>> POJO test case to see if this is an openjpa problem or not. 
>>>     
>>>> --- On Tue, 3/17/09, Paul Copeland
>>>>       
>>> <tech@jotobjects.com> wrote:
>>>     
>>>>> From: Paul Copeland <tech@jotobjects.com>
>>>>> Subject: Re: How do I persist timestamp in UTC
>>>>>         
>>> timezone?
>>>     
>>>>> To: users@openjpa.apache.org
>>>>> Date: Tuesday, March 17, 2009, 3:04 PM
>>>>> Of course java.util.Date is already measured in
>>>>>         
>>> milliseconds
>>>     
>>>>> UTC without regard to TimeZone.  So it may seem
>>>>>         
>>> that you are
>>>     
>>>>> converting your Date objects to a different
>>>>>         
>>> timezone, but
>>>     
>>>>> that's not the case.  This is why you can use
>>>>> Calendar.compareTo() with objects in different
>>>>>         
>>> TimeZones.
>>>     
>>>>> By definition - new Date() is the same thing as
>>>>>         
>>> new
>>>     
>>>>> Date(System.currentTimeMillis()) no matter what
>>>>>         
>>> the default
>>>     
>>>>> TimeZone!
>>>>>
>>>>> The link cited by Fazi implies that you have to
>>>>>         
>>> store the
>>>     
>>>>> TimeZone along with the date if you want to load
>>>>>         
>>> the date
>>>     
>>>>> (milliseconds) back into a Calendar object
>>>>>         
>>> representing that
>>>     
>>>>> TimeZone.
>>>>>
>>>>> If you change the default timezone to UTC (or
>>>>>         
>>> Moscow, etc.)
>>>     
>>>>> then all the other Calendar objects that are meant
>>>>>         
>>> to
>>>     
>>>>> represent the default Locale will be wrong!
>>>>>
>>>>> - Paul
>>>>>
>>>>> On 3/17/2009 2:15 PM, Fay Wang wrote:
>>>>>         
>>>>>> Hi Fazi,
>>>>>>    I found that by putting        
>>>>>>           
>>> TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
>>>     
>>>>>>    to make your java app in UTC time zone (see
>>>>>>           
>>> below
>>>     
>>>>> in testDate), openjpa will store the dates in UTC
>>>>>         
>>> in the
>>>     
>>>>> database. 
>>>>>         
>>>>>>     public void testDate(){
>>>>>>    
>>>>>>           
>>> 	TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
>>>     
>>>>>>     	DateTest dt = new DateTest();
>>>>>>     	dt.setId(id);
>>>>>>    
>>>>>>           
>>> 	dt.setCreatedTime(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
>>>     
>>>>>>     	dt.setStartTime(new Date());
>>>>>>
>>>>>>
>>>>>>
>>>>>> -Fay
>>>>>>
>>>>>>
>>>>>> --- On Tue, 3/17/09, fazi
>>>>>>           
>>>>> <faisal.ansari@gmail.com> wrote:
>>>>>         
>>>>>>   
>>>>>>           
>>>>>>> From: fazi <faisal.ansari@gmail.com>
>>>>>>> Subject: How do I persist timestamp in UTC
>>>>>>>             
>>>>> timezone?
>>>>>         
>>>>>>> To: users@openjpa.apache.org
>>>>>>> Date: Tuesday, March 17, 2009, 9:56 AM
>>>>>>> Hi I need to persist all timestamps in UTC
>>>>>>>             
>>> timezone.
>>>     
>>>>> I found
>>>>>         
>>>>>>> this documentation
>>>>>>> which talks about using Calendar by
>>>>>>>             
>>> setting the
>>>     
>>>>> timezone
>>>>>         
>>>>>>> value to the
>>>>>>> desired timezone
>>>>>>>
>>>>>>>             
>>> (http://openjpa.apache.org/builds/1.1.0/apache-openjpa-1.1.0/docs/manual/ref_guide_pc_scos.html#ref_guide_pc_calendar_timezone),
>>>     
>>>>>>> plus this JIRA
>>>>>>>
>>>>>>>             
>>> (https://issues.apache.org/jira/browse/OPENJPA-322) that
>>>     
>>>>>>> gives me some idea on how I can initialize
>>>>>>>             
>>>>> Calendar to
>>>>>         
>>>>>>> insert/update time in
>>>>>>> UTC but nothing has worked so far. The
>>>>>>>             
>>> timestamps
>>>     
>>>>> are
>>>>>         
>>>>>>> always entered and
>>>>>>> read in DB local timezone (PDT). I noticed
>>>>>>>             
>>> that
>>>     
>>>>> the
>>>>>         
>>>>>>> Calendar timezone of the
>>>>>>> retrieved object is set correctly to UTC,
>>>>>>>             
>>> however,
>>>     
>>>>> the time
>>>>>         
>>>>>>> is still in
>>>>>>> local timezone (PDT). I also noticed that
>>>>>>>             
>>> the
>>>     
>>>>> retrieved
>>>>>         
>>>>>>> object has JPA
>>>>>>> implementation of the Calendar object:
>>>>>>>
>>>>>>>             
>>> org.apache.openjpa.util.java$util$GregorianCalendar$proxy. 
>>>     
>>>>>>> We are using openJPA version 1.1.0. I am
>>>>>>>             
>>> copying my artifacts below. The test class
>>>     
>>>>> has two
>>>>>         
>>>>>>> timestamp fields,
>>>>>>> one is for UTC timezone and the other one
>>>>>>>             
>>> is for
>>>     
>>>>> local
>>>>>         
>>>>>>> timezone to show that
>>>>>>> both values are same. Please let me know
>>>>>>>             
>>> if any other information can
>>>     
>>>>> help
>>>>>         
>>>>>>> understand this problem
>>>>>>> better. Any help on this matter will be
>>>>>>>             
>>> greatly
>>>     
>>>>> appreciated.
>>>>>         
>>> ---------------------------------------------------------
>>>     
>>>>>>> TestDate table:
>>>>>>> CREATE TABLE DATETEST (
>>>>>>>                 ID VARCHAR(255) NOT NULL,
>>>>>>>                 CREATEDTIME TIMESTAMP,    
>>>>>>>             
>>>        
>>>     
>>>>>     STARTTIME TIMESTAMP)
>>>>>
>>>>>         
>>> ----------------------------------------------------------
>>>     
>>> ----------------------------------------------------------
>>>     
>>>>>>> JPA class:
>>>>>>>
>>>>>>>             
>>> ----------------------------------------------------------
>>>     
>>>>>>> package com.my.package.entity;
>>>>>>>
>>>>>>> import java.io.Serializable;
>>>>>>> import java.util.Calendar;
>>>>>>> import java.util.Date;
>>>>>>> import java.util.TimeZone;
>>>>>>>
>>>>>>> import javax.persistence.*;
>>>>>>>
>>>>>>>
>>>>>>> @Entity
>>>>>>> @Table(name="DATETEST")
>>>>>>> public class DateTest implements
>>>>>>>             
>>> Serializable  {
>>>     
>>>>>>> 	@Id
>>>>>>> 	@Column(name="ID")
>>>>>>> 	private	 String  id;
>>>>>>>
>>>>>>> 	@Temporal(TemporalType.TIMESTAMP)
>>>>>>> 	@Column(name="CREATEDTIME")
>>>>>>> 	private	 Calendar  createdTime =
>>>>>>>
>>>>>>>             
>>> Calendar.getInstance(TimeZone.getTimeZone("UTC"));
>>>     
>>>>>>> 	@Temporal(TemporalType.TIMESTAMP)
>>>>>>> 	@Column(name="STARTTIME")
>>>>>>> 	private	 Date  startTime;
>>>>>>>
>>>>>>> 	public Calendar getCreatedTime() {
>>>>>>> 		return createdTime;
>>>>>>> 	}
>>>>>>>
>>>>>>> 	public void setCreatedTime(Calendar
>>>>>>>             
>>> createdTime)
>>>     
>>>>> {
>>>>>         
>>>>>>> 		this.createdTime = createdTime;
>>>>>>> 	}
>>>>>>>
>>>>>>> 	public Date getStartTime() {
>>>>>>> 		return startTime;
>>>>>>> 	}
>>>>>>>
>>>>>>> 	public void setStartTime(Date startTime)
>>>>>>>             
>>> {
>>>     
>>>>>>> 		this.startTime = startTime;
>>>>>>> 	}
>>>>>>>
>>>>>>> 	public String getId() {
>>>>>>> 		return id;
>>>>>>> 	}
>>>>>>>
>>>>>>> 	public void setId(String id) {
>>>>>>> 		this.id = id;
>>>>>>> 	}
>>>>>>> }
>>>>>>>
>>>>>>>             
>>> ----------------------------------------------------------
>>>     
>>> ----------------------------------------------------------
>>>     
>>>>>>> JUnit test:
>>>>>>>
>>>>>>>             
>>> ----------------------------------------------------------
>>>     
>>>>>>>    @Test
>>>>>>>    public void testDate()
>>>>>>>    {
>>>>>>>           DateTest dt = new DateTest();
>>>>>>>      dt.setId(id);
>>>>>>>    
>>>>>>>             
>>> dt.setCreatedTime(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
>>>     
>>>>>>>      dt.setStartTime(new Date());
>>>>>>>  	
>>>>>>>      try
>>>>>>>      {
>>>>>>>        //persist
>>>>>>>      }
>>>>>>>      catch (Exception e)
>>>>>>>      {
>>>>>>>        fail(e.getMessage());
>>>>>>>      }
>>>>>>>           // Check result
>>>>>>>      DateTest returned = null;
>>>>>>>      try
>>>>>>>      {
>>>>>>>        returned = //find by id;
>>>>>>>        Calendar createdTimeC =
>>>>>>>             
>>>>> returned.getCreatedTime();
>>>>>         
>>>>>>>       
>>>>>>>             
>>> System.out.println("createdTime type  
>>>     
>>>>>  :
>>>>>         
>>>>>>> " +
>>>>>>> createdTimeC.getClass().getName());
>>>>>>>       
>>>>>>>             
>>> System.out.println("createdTime
>>>     
>>>>> timezone:
>>>>>         
>>>>>>> " +
>>>>>>> createdTimeC.getTimeZone());
>>>>>>>        System.out.println("Created
>>>>>>>             
>>> time      
>>>     
>>>>>  :
>>>>>         
>>>>>>> " +
>>>>>>> createdTimeC.getTime());
>>>>>>>        System.out.println("Start time
>>>>>>>             
>>>        
>>>     
>>>>>  :
>>>>>         
>>>>>>> " +
>>>>>>> returned.getStartTime());
>>>>>>>        System.out.println("Created
>>>>>>>             
>>> time
>>>     
>>>>> (millisecs):
>>>>>         
>>>>>>> " +
>>>>>>> createdTimeC.getTimeInMillis());
>>>>>>>        System.out.println("Start time
>>>>>>>             
>>>>> (millisecs)  :
>>>>>         
>>>>>>> " +
>>>>>>> returned.getStartTime().getTime());
>>>>>>>      }
>>>>>>>      catch (Exception e)
>>>>>>>      {
>>>>>>>        fail(e.getMessage());
>>>>>>>      }
>>>>>>>         } 
>>>>>>>             
>>> ----------------------------------------------------------
>>>     
>>>>>>> The output is:
>>>>>>>
>>>>>>> createdTime type    :
>>>>>>>
>>>>>>>             
>>> org.apache.openjpa.util.java$util$GregorianCalendar$proxy
>>>     
>>>>>>> createdTime timezone:
>>>>>>>
>>>>>>>             
>>> sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
>>>     
>>>>>>> Created time        : Tue Mar 17 09:40:39
>>>>>>>             
>>> PDT 2009
>>>     
>>>>>>> Start time          : Tue Mar 17 09:40:39
>>>>>>>             
>>> PDT 2009
>>>     
>>>>>>> Created time (millisecs): 1237308039662
>>>>>>> Start time (millisecs)  : 1237308039662
>>>>>>>
>>>>>>>
>>>>>>>             
>>> ----------------------------------------------------------
>>>     
>>>>>>> And the DB entry shows:
>>>>>>>
>>>>>>> db2> select * from DATETEST
>>>>>>>
>>>>>>> ID                       CREATEDTIME      
>>>>>>>             
>>>        
>>>     
>>>>>                STARTTIME
>>>>>         
>>>>>>> ---------------   
>>>>>>>             
>>>>> ----------------------------------
>>>>>         
>>> -------------------------------------
>>>     
>>>>>>> utc_test1           
>>>>>>>             
>>> 2009-03-16-16.14.32.380000   
>>>     
>>>>>        2009-03-16-16.14.32.380000
>>>>>         
>>>>>>> 1 record(s) selected.
>>>>>>>
>>>>>>> db2>
>>>>>>>
>>>>>>> -- View this message in context:
>>>>>>>
>>>>>>>             
>>> http://n2.nabble.com/How-do-I-persist-timestamp-in-UTC-timezone--tp2492546p2492546.html
>>>     
>>>>>>> Sent from the OpenJPA Users mailing list
>>>>>>>             
>>> archive
>>>     
>>>>> at
>>>>>         
>>>>>>> Nabble.com.
>>>>>>>     
>>>>>>>             
>>>>>>       
>>>>>>
>>>>>>
>>>>>>           
>>>>
>>>>       
>>
>>
>>       
>>
>>
>>
>>   
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/How-do-I-persist-timestamp-in-UTC-timezone--tp2492546p2499054.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message