Back

parse (clj)

(source)

protocol

(parse _)
Parse is not in the main api because it is slow and may give surprising behaviour. Various tick functions in the public api (e.g between) still accept strings as arguments and will attempt to parse the args into an applicable date/time entity before carrying out the main work of the function. Calling these functions with strings is not recommended but has been kept for backward compatibility. Parse to most applicable instance. Do not use this function if you know the expected format of the string that you want to parse. This is partly because for example t/instant, t/date etc will be much faster, but also because if the string you pass it is not in the format you expect, this function may still convert it into some entity that you weren't expecting. If you have a string in a non-standard format, use a formatter and the parse fn of they entity you want. For example: (cljc.java-time.local-date/parse "20200202" (t/formatter "yyyyMMdd"))

Examples

tick
(ns tick.api-test
  (:require
    [clojure.test
     :refer [deftest is testing run-tests]
     :refer-macros [deftest is testing run-tests]]
    [tick.core :as t]
    [tick.locale-en-us]
    [tick.protocols :as p]
    [cljc.java-time.clock]
    [cljc.java-time.instant]
    [cljc.java-time.day-of-week]
    [cljc.java-time.month]
    [cljc.java-time.year]
    #?@(:cljs [[java.time :refer [Instant]]]))
  #?(:clj
     (:import [java.time Instant])))

(deftest epoch-test
  (is (= (cljc.java-time.instant/parse "1970-01-01T00:00:00Z") (t/epoch))))

(deftest parse-test
  (is (t/date? (t/parse-date "2020/02/02" (t/formatter "yyyy/MM/dd"))))
  (is (t/year? (t/parse-year "20" (t/formatter "yy"))))
  (is (t/year-month? (t/parse-year-month "20/02" (t/formatter "yy/MM"))))
  (is (t/date-time? (t/parse-date-time "2020/02/02:2002" (t/formatter "yyyy/MM/dd:HHmm"))))
  (is (t/time? (t/parse-time "2002" (t/formatter "HHmm"))))
  (is (t/zoned-date-time? (t/parse-zoned-date-time "2020/02/02:2002:Z"
                            (t/formatter "yyyy/MM/dd:HHmm:VV"))))
  (is (t/offset-date-time? (t/parse-offset-date-time "2020/02/02:2002:-08:30"
                             (t/formatter "yyyy/MM/dd:HHmm:VV")))))
tick
(ns tick.internals-test
  (:require [clojure.test
             :refer [deftest is testing run-tests]
             :refer-macros [deftest is testing run-tests]]
            [tick.core :as t]
            [tick.protocols :as p]))

(deftest parse-test
  (testing "(time \"4pm\")"
    (is (t/time? (p/parse "4pm")))
    (is (= "16:00" (str (p/parse "4pm"))))))