Code Monkey home page Code Monkey logo

Comments (7)

gersonkm avatar gersonkm commented on July 26, 2024

TimestampColumnDateTimeMapper.fromNonNullValue() also is broken... see this:

        // fixture
        final DateTimeZone AMERICA_SAOPAULO_ZONE = DateTimeZone.forID("America/Sao_Paulo");

        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        Timestamp timestamp = new Timestamp(new DateTime(2013, 10, 22, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE).getMillis());

        // exercise SUT
        DateTime dateTime = mapper.fromNonNullValue(timestamp);

        // verify
        assertThat(dateTime, is(new DateTime(2013, 10, 22, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE))); // fails

from jadira.

gersonkm avatar gersonkm commented on July 26, 2024

All those tests fail! Please, see above.

import org.jadira.usertype.dateandtime.joda.columnmapper.TimestampColumnDateTimeMapper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.TimeZone;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class JadiraTest {

    final DateTimeZone AMERICA_SAOPAULO_ZONE = DateTimeZone.forID("America/Sao_Paulo");
    final DateTimeZone UTC_ZONE = DateTimeZone.UTC;

    @Before
    public void setup() {
        // Mapper (SUT) should ignore this default time zone when .setJavaZone(..) is used!
        // All tests always set a specific JavaZone
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    }


    @Test
    public void toNonNullValue_UTC() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(UTC_ZONE);
        mapper.setDatabaseZone(UTC_ZONE);

        //
        // exercise SUT
        Timestamp timestamp = mapper.toNonNullValue(new DateTime(2013, 10, 22, 0, 0, 0, 0, UTC_ZONE));

        //
        // verify
        assertThat(timestamp.getTime(), is(new DateTime(2013, 10, 22, 0, 0, 0, 0, UTC_ZONE).getMillis()));

    }

    @Test
    public void toNonNullValue_AmericaSaoPaulo() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        //
        // exercise SUT
        Timestamp timestamp = mapper.toNonNullValue(new DateTime(2013, 10, 1, 0, 0, 0, 0, AMERICA_SAOPAULO_ZONE));

        //
        // verify
        assertThat(timestamp.getTime(), is(new DateTime(2013, 10, 1, 0, 0, 0, 0, AMERICA_SAOPAULO_ZONE).getMillis()));

    }

    @Test
    public void toNonNullValue_AmericaSaoPaulo_DaylightSaving() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        //
        // exercise SUT
        Timestamp timestamp = mapper.toNonNullValue(new DateTime(2013, 10, 22, 0, 0, 0, 0, AMERICA_SAOPAULO_ZONE));

        //
        // verify
        assertThat(timestamp.getTime(), is(new DateTime(2013, 10, 22, 0, 0, 0, 0, AMERICA_SAOPAULO_ZONE).getMillis()));

    }


    @Test
    public void fromNonNullValue_UTC() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(UTC_ZONE);
        mapper.setDatabaseZone(UTC_ZONE);

        Timestamp timestamp = new Timestamp(new DateTime(2013, 10, 22, 2, 0, 0, 0, UTC_ZONE).getMillis());

        //
        // exercise SUT
        DateTime dateTime = mapper.fromNonNullValue(timestamp);

        //
        // verify
        assertThat(dateTime, is(new DateTime(2013, 10, 22, 2, 0, 0, 0, UTC_ZONE)));
    }

    @Test
    public void fromNonNullValue_AmericaSaoPaulo() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        Timestamp timestamp = new Timestamp(new DateTime(2013, 10, 1, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE).getMillis());

        //
        // exercise SUT
        DateTime dateTime = mapper.fromNonNullValue(timestamp);

        //
        // verify
        assertThat(dateTime, is(new DateTime(2013, 10, 1, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE)));
    }

    @Test
    public void fromNonNullValue_AmericaSaoPaulo_DaylightSaving() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        Timestamp timestamp = new Timestamp(new DateTime(2013, 10, 22, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE).getMillis());

        //
        // exercise SUT
        DateTime dateTime = mapper.fromNonNullValue(timestamp);

        //
        // verify
        assertThat(dateTime, is(new DateTime(2013, 10, 22, 2, 0, 0, 0, AMERICA_SAOPAULO_ZONE)));
    }

    @Test
    public void fromNonNullValue_AmericaSaoPaulo_DaylightSavingTransitionInstant() throws Exception {

        //
        // fixture
        TimestampColumnDateTimeMapper mapper = new TimestampColumnDateTimeMapper();
        mapper.setJavaZone(AMERICA_SAOPAULO_ZONE);
        mapper.setDatabaseZone(AMERICA_SAOPAULO_ZONE);

        long millisAtTransitionInstant = new DateTime(2013, 10, 19, 23, 59, 59, AMERICA_SAOPAULO_ZONE).getMillis() + 1000;
        Timestamp timestamp = new Timestamp(millisAtTransitionInstant);

        //
        // exercise SUT
        DateTime dateTime = mapper.fromNonNullValue(timestamp);

        //
        // verify
        // At 'time zone offset transition' (2013-10-19 23:59:59 + 1 second), DateTime should be 2013-10-20 01:00:00.
        assertThat(dateTime, is(new DateTime(2013, 10, 20, 1, 0, 0, 0, AMERICA_SAOPAULO_ZONE)));
    }

}

from jadira.

chrisphe avatar chrisphe commented on July 26, 2024

If you change the database zone to UTC the tests all pass. You should only be setting the zone to something other than UTC when you are using the mapper with the JDBC driver to persist to a database with a reference time in that zone.

To verify an issue on this behaviour, we need to extend the test to perform the persistence and retrieval against a specific database product(s) running with the timezone you specified specified.

from jadira.

chrisphe avatar chrisphe commented on July 26, 2024

Which DB are you seeing an issue with?

from jadira.

gersonkm avatar gersonkm commented on July 26, 2024

All data stored in DB (SQL Server 2005) is in BRT (offset -03:00). So, I really have to set the database zone to other than UTC (I know that it's not ideal, but it can't be undone as there are a lot of data already stored there).
The Java Zone is 'America/Sao_Paulo' (offsets -03:00 and -02:00 at DST).

from jadira.

gersonkm avatar gersonkm commented on July 26, 2024

Any chance this could be fixed?

from jadira.

chrisphe avatar chrisphe commented on July 26, 2024

I am preparing 3.1.0.CR9 which changes the implementation of Database Zone. Please review and reopen the issue if it doesn't address the problem you have.

from jadira.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.