From 868477640206e55887d5c017d1e47a58ef64dc47 Mon Sep 17 00:00:00 2001 From: Russ Tyndall Date: Tue, 6 Feb 2018 12:22:20 -0500 Subject: [PATCH] Created time-to-localtime * allows conversion of utc back to localtime for eg: printing * exported time-to-utc, time-to-localtime * added tests roundtripping times between the two re ADWolf:#1408 --- sql/package.lisp | 2 ++ sql/time.lisp | 10 ++++++++++ tests/test-time.lisp | 12 +++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sql/package.lisp b/sql/package.lisp index 8915b06..9ccb074 100644 --- a/sql/package.lisp +++ b/sql/package.lisp @@ -573,6 +573,8 @@ #:gregorian-to-mjd #:mjd-to-gregorian #:iso-timestring + #:time-to-utc + #:time-to-localtime )) (:documentation "This is the INTERNAL SQL-Interface package of CLSQL.")) diff --git a/sql/time.lisp b/sql/time.lisp index a7674a8..39d9e4d 100644 --- a/sql/time.lisp +++ b/sql/time.lisp @@ -158,6 +158,16 @@ (setf (time-is-utc? newt) T) newt))) +(defun time-to-localtime (in) + "Ensures that if we have a time thats not in UTC, treat it as a localtime, + and convert to UTC" + (if (not (time-is-utc? in)) + in + (let ((newt + (time+ in (make-duration :second (- (%universal-ts-offset in)))))) + (setf (time-is-utc? newt) nil) + newt))) + (defun make-time (&key (year 0) (month 1) (day 1) (hour 0) (minute 0) (second 0) (usec 0) (offset nil)) (let* ((mjd (gregorian-to-mjd month day year)) diff --git a/tests/test-time.lisp b/tests/test-time.lisp index d631340..b130b99 100644 --- a/tests/test-time.lisp +++ b/tests/test-time.lisp @@ -90,7 +90,17 @@ (deftest :time/iso-parse/8 (let* ((time1 (parse-timestring "2010-01-23T14:56:32-05:30"))) (decode-time time1)) - 0 32 26 20 23 1 2010 6 t) + 0 32 26 20 23 1 2010 6 t) + +(deftest :time/utc-round-trip/1 + (let* ((time1 (parse-timestring "2010-01-23T14:56:32"))) + (decode-time (time-to-localtime (time-to-utc time1)))) + 0 32 56 14 23 1 2010 6 nil) + +(deftest :time/utc-round-trip/2 + (let* ((time1 (parse-timestring "2010-01-23T14:56:32Z"))) + (decode-time (time-to-utc (time-to-localtime time1)))) + 0 32 56 14 23 1 2010 6 t) (deftest :time/print-parse/1 ;;make sure when we print and parse we get the same time. -- 2.34.1