aboutsummaryrefslogtreecommitdiff
path: root/module/datetime.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-03-20 01:24:22 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2020-03-20 01:24:22 +0100
commit8827202bfb40e0d4be487349d99fc9d7024be2bc (patch)
tree0407e9c3f67c4ec17ea25da0ce403ebcbd5966a2 /module/datetime.scm
parentmore html cleanup. (diff)
downloadcalp-8827202bfb40e0d4be487349d99fc9d7024be2bc.tar.gz
calp-8827202bfb40e0d4be487349d99fc9d7024be2bc.tar.xz
Introduce propper datetime->tm and vice versa.
Diffstat (limited to 'module/datetime.scm')
-rw-r--r--module/datetime.scm65
1 files changed, 37 insertions, 28 deletions
diff --git a/module/datetime.scm b/module/datetime.scm
index f79100d7..3a84e9a2 100644
--- a/module/datetime.scm
+++ b/module/datetime.scm
@@ -93,39 +93,48 @@
(or time (make-time hour minute second))
tz))
+
+
+;; NOTE there isn't any stable way to craft the tm objects.
+;; I could call mktime on some date, and replace the fields
+;; with the set-tm:*, but that is worse that breaking the API.
+(define (datetime->tm datetime)
+ (let ((t (get-time% datetime))
+ (d (get-date datetime)))
+ (vector (second t)
+ (minute t)
+ (hour t)
+ (day d)
+ (1- (month d))
+ (- (year d) 1900)
+ 0 0 ; wday & yday (ignored)
+ -1 ; DST unknown
+ 0 ; UTC offset (ignored)
+ #f ; TZ name
+ )))
+
+(define (tm->datetime tm)
+ (datetime year: (+ 1900 (tm:year tm))
+ month: (1+ (tm:mon tm))
+ day: (tm:mday tm)
+ hour: (tm:hour tm)
+ minute: (tm:min tm)
+ second: (tm:sec tm)))
+
+
;; datetime → datetime
;; Takes a datetime in any timezone, and renormalize it to local time
;; (as defined by TZ). This means that given UTC 10:00 new years day
;; would return 11:00 new years day if ran in sweden.
(define-public (get-datetime dt)
- (let ((t (get-time% dt))
- (d (get-date dt)))
- ;; NOTE there isn't any stable way to craft the tm objects.
- ;; I could call mktime on some date, and replace the fields
- ;; with the set-tm:*, but that is worse that breaking the API.
- (let ((v (vector (second t)
- (minute t)
- (hour t)
- (day d)
- (1- (month d))
- (- (year d) 1900)
- 0 0 ; wday & yday (ignored)
- -1 ; DST unknown
- 0 ; UTC offset (ignored)
- #f ; TZ name
- )))
- (let ((tm
- (localtime ; localtime convertion since the returned tm object is
- (car ; in the parsed timezone.
- (cond [(not (tz dt)) (mktime v)]
- [(string=? "local" (tz dt)) (mktime v)]
- [else (mktime v (tz dt))])))))
- (datetime year: (+ 1900 (tm:year tm))
- month: (1+ (tm:mon tm))
- day: (tm:mday tm)
- hour: (tm:hour tm)
- minute: (tm:min tm)
- second: (tm:sec tm))))))
+ (let ((v (datetime->tm dt)))
+ (let ((tm
+ (localtime ; localtime convertion since the returned tm object is
+ (car ; in the parsed timezone.
+ (cond [(not (tz dt)) (mktime v)]
+ [(string=? "local" (tz dt)) (mktime v)]
+ [else (mktime v (tz dt))])))))
+ (tm->datetime tm))))
;; Deprecated
;; datetime → time