diff --git a/info.rkt b/info.rkt new file mode 100644 index 0000000..60789c7 --- /dev/null +++ b/info.rkt @@ -0,0 +1,20 @@ +#lang info + +(define pkg-authors '(hnmdijkema)) +(define version "0.1.0") +(define license 'Apache-2.0) +(define pkg-desc "A few utility functions around the gregor date module") + +(define scribblings + '( + ("scribblings/gregor-utils.scrbl" () (library) "gregor-utils") + ) + ) + +(define deps + '("base" "gregor")) + +(define build-deps + '("racket-doc" + "rackunit-lib" + "scribble-lib")) diff --git a/main.rkt b/main.rkt new file mode 100644 index 0000000..8bd2dfe --- /dev/null +++ b/main.rkt @@ -0,0 +1,80 @@ +(module gregor-utils racket/base + (require (prefix-in g: gregor) + (prefix-in g: gregor/time) + tzinfo + racket/string + ) + + (provide date->moment + moment->date + racket-date? + ) + + (define (date*->moment dt) + (unless (date*? dt) + (error "dt must be of type date*")) + ;; As date* will be localtime or UTC, we're working + ;; with what we got. We can recognize UTC, but the + ;; timezone name will be something OS specific, e.g. + ;; on a dutch windows system: West-Europa (zomertijd) + ;; which is reported e.g. by windows powershell command + ;; get-timezone: + ;; + ;; Id : W. Europe Standard Time + ;; DisplayName : (UTC+01:00) Amsterdam, Berlijn, Bern, Rome, Stockholm, Wenen + ;; StandardName : West-Europa (standaardtijd) + ;; DaylightName : West-Europa (zomertijd) + ;; BaseUtcOffset : 01:00:00 + ;; SupportsDaylightSavingTime : True + ;; + ;; but, as the racket standard date functions only + ;; work with UTC and localtime, we can request the current timezone + ;; tzinfo's (system-tzid) for localtime + ;; and use UTC for univeral time. + (let ((dt-tz (string-downcase (string-trim (date*-time-zone-name dt))))) + (g:moment (date-year dt) + (date-month dt) + (date-day dt) + (date-hour dt) + (date-minute dt) + (date-second dt) + (date*-nanosecond dt) + #:tz (if (string=? dt-tz "utc") + "UTC" + (system-tzid)) + ) + ) + ) + + (define (date->moment dt) + (if (date*? dt) + (date*->moment dt) + (g:moment (date-year dt) + (date-month dt) + (date-day dt) + (date-hour dt) + (date-minute dt) + (date-second dt) + #:tz (date-time-zone-offset dt))) + ) + + (define (moment->date m) + (make-date* (g:->seconds m) + (g:->minutes m) + (g:->hours m) + (g:->day m) + (g:->month m) + (g:->year m) + (g:->wday m) + (g:->yday m) + #f + (g:->utc-offset m) + 0 + (g:->timezone m) + ) + ) + + (define (racket-date? dt) + (date? dt)) + + ); end of module \ No newline at end of file diff --git a/scribblings/gregor-utils.scrbl b/scribblings/gregor-utils.scrbl new file mode 100644 index 0000000..c755261 --- /dev/null +++ b/scribblings/gregor-utils.scrbl @@ -0,0 +1,60 @@ +#lang scribble/manual + +@(require + scribble/example + scribble/core + (for-label racket/base + racket/string + )) + +(define myeval + (make-base-eval '(require gregor gregor-utils))) + +@title[#:tag "gregor-utils"]{Some utility functions around the gregor date module} + +@author[@author+email["Hans Dijkema" "hans@dijkewijk.nl"]] + +@defmodule[gregor-utils]{This module provides some utility functions around the gregor date module} + +@section{Utility functions} + +@defproc[(date->moment [dt (or/c date? date*?)]) moment?]{ + Converts a racket @racket{date} or @racket{date*} structure to a gregor @racket{moment}. + When it converts, it will assumes two kinds of dates. + + @itemlist[ + @item{The first being a standard racket date structure with a time zone offset.} + @item{The second for a date* structure, that will hold a named local timezone or 'UTC'.} + ] + + This function will check if the date* structure is UTC time or local timezone. If it is + local timezone, it will use the function @racket{(system-tzid)} of tzinfo to determine the + local timezone id, that can be used to create a new gregor @racket{moment}. + + As date* will be either localtime or UTC, we're working with what we got. We can recognize UTC, but the + timezone name will be something OS specific, e.g. on a dutch windows system: "West-Europa (zomertijd)". + This is reported e.g. in the windows powershell command: + +@#reader scribble/comment-reader +[racketblock +> get-timezone: + + Id : W. Europe Standard Time + DisplayName : (UTC+01:00) Amsterdam, Berlijn, Bern, Rome, Stockholm, Wenen + StandardName : West-Europa (standaardtijd) + DaylightName : West-Europa (zomertijd) + BaseUtcOffset : 01:00:00 + SupportsDaylightSavingTime : True +] + +But, as the racket standard date functions only work with UTC and localtime, we can request the current timezone +tzinfo's @racket{(system-tzid)} for localtime and use UTC for univeral time. +} + +@defproc[(moment->date [m moment?]) date*?]{ + Converts a gregor @racket{moment} to a standard racket @racket{date*}. +} + +@defproc[(racket-date? [d any/c?]) boolean?]{ + Returns #t, if d is of racket's standard type @racket{date} or racket{date*}. +}