cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Anderson" <sander...@airvana.com>
Subject RE: Storing GMT dates?
Date Tue, 26 Feb 2008 19:01:33 GMT
Calender.getTime() returns a Date, which doesn't have any concept of a timezone other than
the one you are in. You probably are going to have to get the timezone's offset from GMT and
convert it yourself. I've had to do this in the past, so you're welcome to learn from my mistakes:
take a look at http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/util/TimeFormatter.java
and search for public static Date fileTime(long ft)

-----Original Message-----
From: Alexander Lamb (dev) [mailto:alamb@mac.com] 
Sent: Tuesday, February 26, 2008 9:41 AM
To: user@cayenne.apache.org
Subject: Re: Storing GMT dates?

But that's what I am doing.

I did create a Calendar object initialized as a GMT "now". Then I simply do a "getTime()"
on it.

Now, when I print the result of the getTime() I get the local time, but that's because of
the locale. That's why I am suspecting Cayenne to printout the Date object and the local being
what it is, I end up storing a local time. I know the database has no knowledge of time zones,
and that's why I want to store GMT times (and therefore also avoid the daylight saving times
passage problems).

So it summarizes to "how do I tell cayenne to use GMT when generating the SQL to insert my
Date" (and this, only in some areas). ?

Alex


Le 26 févr. 08 à 15:01, Michael Gentry a écrit :

> Sorry Alexander, I meant to reply to this.
>
> Most databases, to my knowledge, do not store dates with a time zone 
> associated with them.  I haven't played too much with storing things 
> in GMT or any other specific time zone, but I would imagine that 
> you'll need a helper method to do the time zone translations for you.
> For example, override the get/set methods in your subclass to convert 
> from/to GMT.  (This means you'll have to physically convert the time, 
> not just apply the time zone offset.)
>
> Maybe someone else will correct me if I'm wrong on this, but that is 
> the first approach I would try.
>
> /dev/mrg
>
> On Tue, Feb 26, 2008 at 8:25 AM, Alexander Lamb (dev) <alamb@mac.com> 
> wrote:
>> Maybe I can rephrase the question: does Cayenne do any kind of 
>> transformation when storing Date objects in TIMESTAMP fields?
>>
>> If so, how can I control this?
>>
>> Thanks,
>>
>> Alex
>>
>> Le 25 févr. 08 à 16:17, Alexander Lamb a écrit :
>>
>>
>>
>>> Hello,
>>>
>>> Using Cayenne 3.0M3, MySQL 5.
>>>
>>> I have a field which is a TIMESTAMP. It maps to a Java.util.Date 
>>> class in Java.
>>>
>>> If I do a myObject.setMyTimestamp(new Date()) I will store the local 
>>> date.
>>>
>>> I would like to store a GMT date.
>>>
>>> I tried doing this:
>>>
>>> Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
>>>
>>> myObject.setMyTimestamp(cal.getTime());
>>>
>>> but again, I am back to the local time. Indeed, a trace shows "cal"
>>> is correctly set at the GMT value, but cal.getTime() is back to the 
>>> local time (or at least the printout is, but it is as if Cayenne 
>>> took the formated value and not the actual date value).
>>>
>>> How can I solve this problem?
>>>
>>> Thanks,
>>>
>>> Alex
>>> --
>>> Alexander Lamb
>>> Founding Associate
>>> RODANOTECH Sàrl
>>>
>>> 4 ch. de la Tour de Champel
>>> 1206 Geneva
>>> Switzerland
>>>
>>> Tel:  022 347 77 37
>>> Fax: 022 347 77 38
>>>
>>> http://www.rodanotech.ch
>>>
>>>
>>>
>>
>>


Mime
View raw message