Menu

Reading a timestamp then writing it out changes the value

Help
2017-08-04
2017-08-04
  • Richard See

    Richard See - 2017-08-04

    I see from the documentation that timeZoneName defaults to UTC.

    So, if reading a timestamp that has no timezone specified, does it get read in assuming UTC? And upon writing it out, should it not also be written assuming the same UTC? Keeping the same time zone assumption for both reading and writing would preserve the value.

    But currently, it appears to be read in as UTC, but written out as local time zone, thus causing the value to change.

    Example file is "time_types.csv":

    colTimestamp
    2017-08-04 14:12:37.0
    

    Sample code:

    Properties props = new Properties();
    props.put("columnTypes", "Timestamp");
    Connection conn = DriverManager.getConnection("jdbc:relique:csv:.", props);
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet results = stmt.executeQuery("select colTimestamp from time_types");
    boolean writeHeaderLine = true;
    CsvDriver.writeToCsv(results, System.out, writeHeaderLine);
    conn.close();
    

    Expected Result (should match input file exactly):

    colTimestamp
    2017-08-04 14:12:37.0
    

    Actual Result (off by 7 hours, since I am in Pacific Daylight Time):

    colTimestamp
    2017-08-04 07:12:37.0
    

    Workaround:

    props.put("timeZoneName", java.util.TimeZone.getDefault().getID());
    

    Changing the timeZoneName to the above gets the behavior I am looking for. But I was still interested in your thoughts on this. Is this a bug (timezones for reading and writing do not match), or by design (timezone for reading is configurable, but writing is always local)?

    Thanks!

     

    Last edit: Richard See 2017-08-04
  • Simon Chenery

    Simon Chenery - 2017-08-06

    This is a bug in CsvJdbc. The timeZoneName of the database connection should be used when writing timestamps in CsvDriver.writeToCsv().

    I have logged this as bug 128, "CsvDriver.writeToCsv does not use connection timeZoneName".

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.