Skip to content

Commit

Permalink
v2.2.17
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Zheng committed Mar 7, 2016
1 parent 910751e commit 0c570c2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 40 deletions.
10 changes: 5 additions & 5 deletions README.md
@@ -1,20 +1,20 @@
# hara.time.joda-time

[![Build Status](https://travis-ci.org/zcaudate/hara.time.joda.png?branch=master)](https://travis-ci.org/zcaudate/hara)
[![Build Status](https://travis-ci.org/zcaudate/hara.time.joda.png?branch=master)](https://travis-ci.org/zcaudate/hara.time.joda)

joda time extensions for [hara.time](https://github.com/zcaudate/hara)
[joda time](http://www.joda.org/joda-time/) extensions for [hara.time](https://github.com/zcaudate/hara)

## Installation:

In project.clj, add to dependencies:

```clojure
[im.chit/hara.time "2.2.16"]
[im.chit/hara.time.joda "2.2.16"]
[im.chit/hara.time "2.2.17"]
[im.chit/hara.time.joda "2.2.17"]
```
## Usage

Please see the main documentation for [hara.time](http://docs.caudate.me/hara/hara-time.html), this library adds `org.joda.time.DateTime` to the avaliable list of datetime representations supported by the `hara.time` framework.
Please see the main documentation for [hara.time](http://docs.caudate.me/hara/hara-time.html), for the complete API this library adds `org.joda.time.DateTime` to the avaliable list of datetime representations supported by the `hara.time` framework.

```clojure
(require '[hara.time :as t]
Expand Down
5 changes: 3 additions & 2 deletions project.clj
@@ -1,10 +1,11 @@
(defproject im.chit/hara.time.joda "2.2.16"
(defproject im.chit/hara.time.joda "2.2.17"
:description "joda time extensions for hara.time"
:url "https://www.github.com/zcaudate/hara.time.joda"
:license {:name "The MIT License"
:url "http://http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.7.0"]
[joda-time "2.9.2"]]
:profiles {:dev {:dependencies [[midje "1.6.3"]
[im.chit/hara.time "2.2.16"]]
[im.chit/hara.time "2.2.17"]
[im.chit/hara.io.scheduler "2.2.17"]]
:plugins [[lein-midje "3.1.3"]]}})
57 changes: 29 additions & 28 deletions src/hara/time/joda/data.clj
@@ -1,6 +1,7 @@
(ns hara.time.joda.data
(:require [hara.protocol.time :as time]
[hara.protocol.string :as string]
(:require [hara.protocol
[string :as string]
[time :as time]]
[hara.time.data.coerce :as coerce])
(:import [org.joda.time DateTime DateTimeZone]
[org.joda.time.format DateTimeFormatter]))
Expand All @@ -12,19 +13,34 @@
(extend-type DateTimeZone
string/IString
(string/-to-string [tz]
(.getID tz)))

(defmethod time/-timezone DateTimeZone
[s _]
(DateTimeZone/forID s))
(.getID tz))
(string/-to-string-meta [tz]
{:type DateTime}))

(defmethod string/-from-string DateTimeZone
[s _]
(DateTimeZone/forID s))

(defn from-map [{:keys [millisecond second minute hour day month year timezone]}]
(DateTime. ^int year ^int month ^int day ^int hour ^int minute ^int second ^int millisecond
^DateTimeZone (coerce/coerce-zone timezone {:type DateTimeZone})))

(defmethod time/-time-meta DateTime
[_]
{:base :instant
:formatter {:type DateTimeFormatter}
:parser {:type DateTimeFormatter}
:map {:from {:fn from-map}}})

(extend-type DateTime
time/IInstant
(-to-long [t] (.getMillis t))
(-has-timezone? [t] true)
(-get-timezone [t] (string/-to-string (.getZone t)))
(-with-timezone [t tz]
(.withZone
t
^DateTimeZone (coerce/coerce-zone tz {:type DateTimeZone})))

time/IRepresentation
(-millisecond [t _] (.getMillisOfSecond t))
Expand All @@ -37,25 +53,10 @@
(-year [t _] (.getYear t)))

(defmethod time/-from-long DateTime
[long {:keys [timezone]}]
(DateTime. ^Long long ^DateTimeZone (coerce/coerce-zone timezone {:type DateTimeZone})))

(defn from-map [{:keys [millisecond second minute hour day month year timezone]}]
(DateTime. ^int year ^int month ^int day ^int hour ^int minute ^int second ^int millisecond
^DateTimeZone (coerce/coerce-zone timezone {:type DateTimeZone})))
[^Long long {:keys [timezone]}]
(DateTime. long ^DateTimeZone (coerce/coerce-zone timezone {:type DateTimeZone})))

(defmethod time/-time-meta DateTime
[_]
{:base :instant
:formatter {:type DateTimeFormatter}
:parser {:type DateTimeFormatter}
:rep {:from {:fn from-map}
:to {:fn {:millisecond time/-millisecond
:second time/-second
:minute time/-minute
:hour time/-hour
:day time/-day
:day-of-week time/-day-of-week
:month time/-month
:year time/-year
:timezone (fn [^DateTime t opts] (.getZone t))}}}})
(defmethod time/-now DateTime
[{:keys [timezone]}]
(.withZone (DateTime.)
(coerce/coerce-zone timezone {:type DateTimeZone})))
21 changes: 16 additions & 5 deletions test/hara/time/joda_test.clj
Expand Up @@ -11,8 +11,8 @@
(-> (t/from-long 0 {:type DateTime :timezone "Asia/Kolkata"})
(t/to-map))
=> {:type org.joda.time.DateTime,
:timezone "Asia/Kolkata",
:year 1970, :month 1, :day 1, :day-of-week 4,
:timezone "Asia/Kolkata", :long 0
:year 1970, :month 1, :day 1,
:hour 5, :minute 30, :second 0, :millisecond 0})

(fact "See if it works together well"
Expand All @@ -37,7 +37,18 @@
{:type DateTime
:timezone "GMT"})
(t/to-map))
=> {:type org.joda.time.DateTime,
=> {:type org.joda.time.DateTime, :long 599619600000
:timezone "Etc/GMT",
:year 1989, :month 1, :day 1, :day-of-week 7,
:hour 1, :minute 0, :second 0, :millisecond 0})
:year 1989, :month 1, :day 1,
:hour 1, :minute 0, :second 0, :millisecond 0}


(-> (t/parse "00 00 01 01 01 1989 +0000"
"ss mm HH dd MM yyyy Z"
{:type DateTime
:timezone "Asia/Kolkata"})
(t/to-map))
=> {:type org.joda.time.DateTime, :long 599619600000
:timezone "Asia/Kolkata",
:year 1989, :month 1, :day 1,
:hour 6, :minute 30, :second 0, :millisecond 0})
30 changes: 30 additions & 0 deletions test/hara/time/scheduler_test.clj
@@ -0,0 +1,30 @@
(ns hara.time.scheduler-test
(:use midje.sweet)
(:require [hara.time :as t]
[hara.time.joda]
[hara.io.scheduler :as scheduler]
[hara.concurrent.ova :as ova]))

(def sch2
(scheduler/scheduler {:l1 (fn [t params] (println t params))
:l2 (fn [t params] (println t params))}
{:l1 {:schedule "* * * * * * *"
:params {:data "foo"}}
:l2 {:schedule "/2 * * * * * *"
:params {:data "bar"}}}
{:clock {:type org.joda.time.DateTime
:timezone "GMT"}}))

(comment (scheduler/start! sch2)


(scheduler/add-task sch2 :hello {:handler (fn [t params] (println params))
:schedule "* * * * * * *"
:params {:data "foo"}})

(scheduler/delete-task sch2 :hello)

(scheduler/empty-tasks sch2)

(scheduler/stop! sch2))

0 comments on commit 0c570c2

Please sign in to comment.