X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/clj/clojure/instant.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

(ns clojure.instant
(:import [java.util Calendar Date GregorianCalendar TimeZone]
[java.sql Timestamp]))
[clojure.lang TimeStamp]))


(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -214,8 +214,8 @@ with invalid arguments."
(.setTimeZone (java.util.TimeZone/getTimeZone "GMT"))))))

(defn- print-timestamp
"Print a java.sql.Timestamp as RFC3339 timestamp, always in UTC."
[^java.sql.Timestamp ts, ^java.io.Writer w]
"Print a clojure.lang.TimeStamp as RFC3339 timestamp, always in UTC."
[^clojure.lang.TimeStamp ts, ^java.io.Writer w]
(let [^java.text.DateFormat utc-format (.get thread-local-utc-timestamp-format)]
(.write w "#inst \"")
(.write w (.format utc-format ts))
Expand All @@ -224,12 +224,12 @@ with invalid arguments."
(.write w (format ".%09d-00:00" (.getNanos ts)))
(.write w "\"")))

(defmethod print-method java.sql.Timestamp
[^java.sql.Timestamp ts, ^java.io.Writer w]
(defmethod print-method clojure.lang.TimeStamp
[^clojure.lang.TimeStamp ts, ^java.io.Writer w]
(print-timestamp ts w))

(defmethod print-dup java.sql.Timestamp
[^java.sql.Timestamp ts, ^java.io.Writer w]
(defmethod print-dup clojure.lang.TimeStamp
[^clojure.lang.TimeStamp ts, ^java.io.Writer w]
(print-timestamp ts w))


Expand Down Expand Up @@ -259,16 +259,15 @@ milliseconds since the epoch, UTC."
offset-sign offset-hours offset-minutes)))

(defn- construct-timestamp
"Construct a java.sql.Timestamp, which has nanosecond precision."
"Construct a clojure.lang.TimeStamp, which has nanosecond precision."
[years months days hours minutes seconds nanoseconds
offset-sign offset-hours offset-minutes]
(doto (Timestamp.
(.getTimeInMillis
(.. (TimeStamp.
(.getTimeInMillis
(construct-calendar years months days
hours minutes seconds 0
offset-sign offset-hours offset-minutes)))
;; nanos must be set separately, pass 0 above for the base calendar
(.setNanos nanoseconds)))
(setNanos nanoseconds)))

(def read-instant-date
"To read an instant as a java.util.Date, bind *data-readers* to a map with
Expand All @@ -283,9 +282,8 @@ offset."
(partial parse-timestamp (validated construct-calendar)))

(def read-instant-timestamp
"To read an instant as a java.sql.Timestamp, bind *data-readers* to a
map with this var as the value for the 'inst key. Timestamp preserves
"To read an instant as a clojure.lang.TimeStamp, bind *data-readers* to a
map with this var as the value for the 'inst key. TimeStamp preserves
fractional seconds with nanosecond precision. The timezone offset will
be used to convert into UTC."
(partial parse-timestamp (validated construct-timestamp)))

274 changes: 274 additions & 0 deletions src/jvm/clojure/lang/TimeStamp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/* Time.java -- Wrapper around java.util.Date
Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */

package clojure.lang;

/**
* Used for parsing and formatting this date.
* @deprecated used by deprecated functions
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
*/

/**
* This class is a wrapper around java.util.Date to allow the JDBC
* driver to identify the value as a SQL TimeStamp. Note that this
* class also adds an additional field for nano-seconds, and so
* is not completely identical to <code>java.util.Date</code> as
* the <code>java.sql.Date</code> and <code>java.sql.Time</code>
* classes are.
*
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class TimeStamp extends java.util.Date
{
/**
* @serial
*/
static final long serialVersionUID = 1752345689675676457L;

/*
* nanoseconds
*/
private int nanos;

/**
* Used for parsing and formatting this date.
* @deprecated used by deprecated functions
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static DecimalFormat decimalFormat = new DecimalFormat("000000000");
private static StringBuffer sbuf = new StringBuffer(29);
*/

/**
* This method initializes a new instance of this class with the
* specified time value representing the number of milliseconds since
* Jan 1, 1970 at 12:00 midnight GMT.
*
* @param date The time value to intialize this <code>Time</code> to.
*/
public TimeStamp(long date)
{
// first create the TimeStamp
super(date - (date % 1000));
nanos = (int)(date % 1000) * 1000000;
}

/**
* This method returns a new instance of this class by parsing a
* date in JDBC format into a Java date.
*
* @param str The string to parse.
* @return The resulting <code>java.sql.TimeStamp</code> value.
* @deprecated this is managed by clojure.instant
public static TimeStamp valueOf(String str)
{
int nanos = 0;
int dot = str.indexOf('.');
if (dot != -1)
{
if (str.lastIndexOf('.') != dot)
throw new IllegalArgumentException(str);

int len = str.length() - dot - 1;
if (len < 1 || len > 9)
throw new IllegalArgumentException(str);

nanos = Integer.parseInt(str.substring(dot + 1));
for (int i = len; i < 9; i++)
nanos *= 10;

str = str.substring(0, dot);
}

try
{
java.util.Date d;
synchronized (dateFormat)
{
d = (java.util.Date) dateFormat.parseObject(str);
}

if (d == null)
throw new IllegalArgumentException(str);

TimeStamp ts = new TimeStamp(d.getTime() + nanos / 1000000);
ts.nanos = nanos;
return ts;
}
catch (ParseException e)
{
throw new IllegalArgumentException(str);
}
}
*/

/**
* This method returns this date in JDBC format.
*
* @return This date as a string.
* @deprecated this is managed by clojure.instant
public String toString()
{
synchronized (dateFormat)
{
sbuf.setLength(0);
dateFormat.format(this, sbuf, null);
sbuf.append('.');
decimalFormat.format(nanos, sbuf, null);

int end = sbuf.length() - 1;
while (end > 20 && sbuf.charAt(end) == '0')
end--;

return sbuf.substring(0, end + 1);
}
}
*/

/**
* Return the value of this TimeStamp as the number of milliseconds
* since Jan 1, 1970 at 12:00 midnight GMT.
*/
public long getTime() {
return super.getTime() + (nanos / 1000000);
}

// setTime is done by constructor

/**
* This method returns the nanosecond value for this object.
* @return The nanosecond value for this object.
*/
public int getNanos()
{
return nanos;
}

/**
* This method sets the nanosecond value for this object.
*
* @param nanos The nanosecond value for this object. ! it will discard time < 1 sec
* @return this, to able it in a functional flux.
*/
public TimeStamp setNanos(int nanos)
{
if (nanos > 999999999 || nanos < 0)
throw new IllegalArgumentException("nanos > 999999999 or < 0");

// Warning values < 1 sec will be disacarded
this.nanos = nanos;
return this;
}

/**
* This method these the specified <code>Object</code> for equality
* against this object. This will be true if an only if the specified
* object is an instance of <code>TimeStamp</code> and has the same
* time value fields.
*
* @param obj The object to test against for equality.
*
* @return <code>true</code> if the specified object is equal to this
* object, <code>false</code> otherwise.
*/
public boolean equals(Object obj)
{
return (obj instanceof TimeStamp) &&
equals((TimeStamp) obj);
}

/**
* This method tests the specified timestamp for equality against this
* object. This will be true if and only if the specified object is
* not <code>null</code> and contains all the same time value fields
* as this object.
*
* @param ts The <code>TimeStamp</code> to test against for equality.
*
* @return <code>true</code> if the specified object is equal to this
* object, <code>false</code> otherwise.
*/
public boolean equals(TimeStamp ts)
{
return ts != null &&
ts.getTime() == getTime() &&
ts.getNanos() == getNanos();
}

/**
* Compares this <code>TimeStamp</code> to another one.
*
* @param ts The other TimeStamp.
* @return <code>0</code>, if both <code>TimeStamp</code>'s represent exactly
* the same date, a negative value if this <code>TimeStamp</code> is
* before the specified <code>TimeStamp</code> and a positive value
* otherwise.
* @since 1.2
*/
public int compareTo(TimeStamp ts)
{
int s = super.compareTo((java.util.Date) ts);
if (s != 0)
return s;

// If Date components were equal, then we check the nanoseconds.
return nanos - ts.nanos;
}

/**
* Compares this <code>TimeStamp</code> to another one. This behaves like
* <code>compareTo(TimeStamp)</code>, but it may throw a
* <code>ClassCastException</code>, if the specified object is not of type
* <code>TimeStamp</code>.
*
* @param obj The object to compare with.
* @return <code>0</code>, if both <code>TimeStamp</code>'s represent exactly
* the same date, a negative value if this <code>TimeStamp</code> is
* before the specified <code>TimeStamp</code> and a positive value
* otherwise.
* @exception ClassCastException if obj is not of type TimeStamp.
* @see #compareTo(TimeStamp)
* @since 1.2
*/
public int compareTo(java.util.Date obj)
{
return compareTo((TimeStamp) obj);
}

}
8 changes: 4 additions & 4 deletions test/clojure/test_clojure/reader.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,14 @@
s2 "#inst \"2010-11-12T13:14:15.123\""
s3 "#inst \"2010-11-12T13:14:15.123456789123\""]
(binding [*data-readers* {'inst read-instant-timestamp}]
(testing "read-instant-timestamp produces java.sql.Timestamp"
(is (= java.sql.Timestamp (class (read-string s)))))
(testing "java.sql.Timestamp preserves nanoseconds"
(testing "read-instant-timestamp produces clojure.lang.TimeStamp"
(is (= clojure.lang.TimeStamp (class (read-string s)))))
(testing "clojure.lang.TimeStamp preserves nanoseconds"
(is (= 123456789 (-> s read-string .getNanos)))
(is (= 123456789 (-> s read-string pr-str read-string .getNanos)))
;; truncate at nanos for s3
(is (= 123456789 (-> s3 read-string pr-str read-string .getNanos))))
(testing "java.sql.Timestamp should compare nanos"
(testing "clojure.lang.TimeStamp should compare nanos"
(is (= (read-string s) (read-string s3)))
(is (not= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-date}]
Expand Down
X Tutup