package org.mariadb.jdbc;

import static org.junit.Assert.*;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TimestampTest extends BaseTest {

    private Statement stmt;
    private Locale previousFormatLocale;
    private TimeZone previousTimeZone;
    private TimeZone swedishTimeZone;

    @Before
    public void setUp() throws SQLException {
        //Save the previous FORMAT locate so we can restore it later
        previousFormatLocale = Locale.getDefault(Locale.Category.FORMAT);
        //Save the previous timezone so we can restore it later
        previousTimeZone = TimeZone.getDefault();
        
        swedishTimeZone = TimeZone.getTimeZone("Europe/Stockholm");
        TimeZone.setDefault(swedishTimeZone); //Everybody running this test case should be in Sweden
        
        stmt = connection.createStatement();
        
        stmt.execute("drop table if exists timestamptest");
        stmt.execute("create table timestamptest (id int not null primary key auto_increment, tm timestamp)");
    }
    
    @After
    public void tearDown() {
        //Reset the FORMAT locate so other test cases are not disturbed.
        Locale.setDefault(Locale.Category.FORMAT, previousFormatLocale);
        //Reset the timezone so so other test cases are not disturbed.
        TimeZone.setDefault(previousTimeZone);
    }
    
    
    @Test
    public void testGetTimestamp() throws SQLException {
        Calendar _20140101_1320_59 = Calendar.getInstance(TimeZone.getTimeZone("utc"));
        _20140101_1320_59.clear();
        _20140101_1320_59.set(2014, 0, 1, 13, 20, 59);
        
        PreparedStatement ps = connection.prepareStatement("insert into timestamptest values(1, ?)");
        ps.setTimestamp(1, new Timestamp(_20140101_1320_59.getTimeInMillis()));
        ps.execute();
        
        ResultSet rs = stmt.executeQuery("select * from timestamptest");
        while(rs.next()) {
            Timestamp timestamp = rs.getTimestamp(2);
            assertEquals(_20140101_1320_59.getTimeInMillis(), timestamp.getTime());
        }
    }
    
    @Test
    public void testGetTimestampWhenDaylightSavingRemovesHour() throws SQLException {
        //The time 2014-03-30 01:15:00 is valid in UTC
        Calendar quaterPastTwo30thOfMarth2014 = Calendar.getInstance(TimeZone.getTimeZone("utc"));
        quaterPastTwo30thOfMarth2014.clear();
        quaterPastTwo30thOfMarth2014.set(2014, 2, 30, 1, 15, 0);
        
        PreparedStatement ps = connection.prepareStatement("insert into timestamptest values(1, ?)");
        //Explicitly set a time that does not exists in Swedish timezone
        //Note: this query will only work if the server is set to UTC
        ps.setString(1, "2014-03-30 02:15:00");
//        ps.setTimestamp(1, new Timestamp(quaterPastTwo30thOfMarth2014.getTimeInMillis()));
        ps.execute();
        
        ResultSet rs = stmt.executeQuery("select * from timestamptest");
        while(rs.next()) {
            Timestamp timestamp = rs.getTimestamp(2);
            assertEquals(quaterPastTwo30thOfMarth2014.getTimeInMillis(), timestamp.getTime());
        }
    }
}

