JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

os/mktime

core-api


    cfunction
    src/core/os.c on line 2014, column 1

    (os/mktime date-struct &opt local)

    Get the broken down date-struct time expressed as the number of 
    seconds since January 1, 1970, the Unix epoch. Returns a real 
    number. Date is given in UTC unless `local` is truthy, in which 
    case the date is computed for the local timezone.

    Inverse function to os/date.


3 examplesSign in to add an example
Loading...
(defn- parse-timestamp
  ``Parse a date or datetime string into a unix epoch int``
  [s]
  (if (string/find " " s)
    # datetime: "yyyy-MM-dd HH:mm"
    (let [[date-part time-part] (string/split " " s)
          [y m d]               (string/split "-" date-part)
          [hh mm]               (string/split ":" time-part)]
      (os/mktime {:year         (scan-number y)
                  :month        (scan-number m)
                  :month-day    (scan-number d)
                  :hours        (scan-number hh)
                  :minutes      (scan-number mm)
                  :seconds      0}))
    # date: "yyyy-MM-dd"
    (let [[y m d] (string/split "-" s)]
      (os/mktime {:year      (scan-number y)
                  :month     (scan-number m)
                  :month-day (scan-number d)
                  :hours 0 :minutes 0 :seconds 0}))))
veqqqPlayground
# Due to: https://github.com/swlkr/janetdocs/issues/50

(defn parse-date
  "Parses YYYY-MM-DD date format to time"
  [date]
  (let [[year month day] (map scan-number (string/split "-" date))]
    (os/mktime {:year year :month (dec month) :month-day (dec day)})))

(print (parse-date "2021-01-01")) # 1609459200
(print (os/strftime "%c" (parse-date "2021-01-01"))) # Fri Jan  1 00:00:00 2021
veqqqPlayground
(os/time)  # => 1593838384
(os/date)  # => {:month 6 :dst false :year-day 185 :seconds 8 :minutes 53 :week-day 6 :year 2020 :hours 4 :month-day 3}
(os/mktime (os/date))  # => 1593838390
cellularmitosisPlayground