DateTime and TimeZone equality

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

DateTime and TimeZone equality

Elliot Metsger
Hi All,

First, thanks for JodaTime.  I couldn't live without it!  I'm
surprised I haven't come across this earlier but I have a test that
I'd expect to pass, but doesn't:


    @Test
    public void testDateTimeEquality() throws Exception {
        final DateTime now = DateTime.now().toDateTimeISO();
        final DateTime parsed = DateTime.parse(now.toDateTimeISO().toString());
        assertEquals(now.getZone(), parsed.getZone());
        assertEquals(now, parsed);
    }

It fails with:
java.lang.AssertionError:
Expected :America/New_York
Actual   :-05:00

I've tried this test a few different ways and I am not sure how, or if
it is possible, to get the parsed DateTime to be equal to the instance
produced by DateTime.now().  Any ideas?  I'm using JodaTime 2.0.

Thanks,
Elliot

------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DateTime and TimeZone equality

jodastephen
On 13 January 2012 18:13, Elliot Metsger <[hidden email]> wrote:

>    @Test
>    public void testDateTimeEquality() throws Exception {
>        final DateTime now = DateTime.now().toDateTimeISO();
>        final DateTime parsed = DateTime.parse(now.toDateTimeISO().toString());
>        assertEquals(now.getZone(), parsed.getZone());
>        assertEquals(now, parsed);
>    }
>
> It fails with:
> java.lang.AssertionError:
> Expected :America/New_York
> Actual   :-05:00
>
> I've tried this test a few different ways and I am not sure how, or if
> it is possible, to get the parsed DateTime to be equal to the instance
> produced by DateTime.now().  Any ideas?  I'm using JodaTime 2.0.

Parsing in Joda-Time has awkward corner cases around offsets and
zones. The parse() method was meant to help but its behaviour is also
a bit weird in some ways (and not well docced). The parse method
parses any offset (such as +02:00) in the text and creates a time-zone
fixed at that offset.

So, the options are:
DateTime.parse(string) - parses the offset and creates a fixed offset zone
DateTime.parse(string).withZone(null) - parses and then converts to
the default zone (or any other you specify)
new DateTime(string) - parses to the default zone

Thus, #2 and #3 above probably do what you want.

I'm not sure that I can alter the parse method without breaking
someone else, nor am I 100% sure I'd want to change its behaviour.

Stephen

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...