From fedbf0f27dd6ac7cefe72f360899dd1e1fb5b964 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Fri, 22 Feb 2008 16:52:35 +0000 Subject: [PATCH] tweak date handling math to reduce rounding issues git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@234 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../healthmarketscience/jackcess/Column.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index 1de4b16..199fa06 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -62,14 +62,17 @@ public class Column implements Comparable { public static final Object AUTO_NUMBER = ""; /** - * Access starts counting dates at Jan 1, 1900. Java starts counting - * at Jan 1, 1970. This is the # of days between them for conversion. + * Access stores numeric dates in days. Java stores them in milliseconds. */ - private static final double DAYS_BETWEEN_EPOCH_AND_1900 = 25569d; + private static final double MILLISECONDS_PER_DAY = + (24L * 60L * 60L * 1000L); + /** - * Access stores numeric dates in days. Java stores them in milliseconds. + * Access starts counting dates at Jan 1, 1900. Java starts counting + * at Jan 1, 1970. This is the # of millis between them for conversion. */ - private static final double MILLISECONDS_PER_DAY = 86400000d; + private static final long MILLIS_BETWEEN_EPOCH_AND_1900 = + 25569L * (long)MILLISECONDS_PER_DAY; /** * Long value (LVAL) type that indicates that the value is stored on the same page @@ -639,10 +642,8 @@ public class Column implements Comparable { { // seems access stores dates in the local timezone. guess you just hope // you read it in the same timezone in which it was written! - double dTime = buffer.getDouble(); - dTime *= MILLISECONDS_PER_DAY; - dTime -= (DAYS_BETWEEN_EPOCH_AND_1900 * MILLISECONDS_PER_DAY); - long time = (long)dTime; + long time = (long)(buffer.getDouble() * MILLISECONDS_PER_DAY); + time -= MILLIS_BETWEEN_EPOCH_AND_1900; time -= getTimeZoneOffset(time); return new Date(time); } @@ -659,9 +660,8 @@ public class Column implements Comparable { // hope you read it in the same timezone in which it was written! long time = ((Date)value).getTime(); time += getTimeZoneOffset(time); - - double dTime = (((double)time) / MILLISECONDS_PER_DAY) + - DAYS_BETWEEN_EPOCH_AND_1900; + time += MILLIS_BETWEEN_EPOCH_AND_1900; + double dTime = ((double)time) / MILLISECONDS_PER_DAY; buffer.putDouble(dTime); } } -- 2.39.5