Clojure:用 babashka 获得当前时间字符串的依赖性最小的方法是什么?

Clojure: what's the way to have current time string with babashka with least dependency?

用下面的表达式,

bb '(new java.util.Date)'
#inst "2020-07-18T14:35:16.663-00:00"

我想得到一个字符串,稍微格式化为:

"2020-07-18 UTC 14:35:16"

有了Babashka (bb),我希望能有最少的依赖。 否则我可以使用clj-time等来达到目的。

或者我是否应该只使用 OS 的日期实用程序来代替,因为我正在编写脚本?

在 babashka 中,您可以使用 java.time 包:

(import 'java.time.format.DateTimeFormatter
        'java.time.LocalDateTime)

(def date (LocalDateTime/now))
(def formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
(.format date formatter) ;;=> "2020-07-18 18:04:04"

出于 shell 脚本目的,我定义了自己的便利 bash/zsh 函数:

      function iso-date() {
        date "+%Y-%m-%d"
      }
      function iso-date-short() {
        date "+%Y%m%d"
      }
      function iso-time() {
        date "+%H:%M:%S"
      }
      function iso-time-short() {
        date "+%H%M%S"
      }
      function iso-date-time() {
        echo "$(iso-date)t$(iso-time)"
      }
      function iso-date-time-nice() {
        echo "$(iso-date) $(iso-time)"
      }
      function iso-date-time-str() {
        echo "$(iso-date-short)-$(iso-time-short)"
      }

结果:

    ~/cool > iso-date
    2020-07-18

    ~/cool > iso-date-short
    20200718

    ~/cool > iso-time      
    13:40:50

    ~/cool > iso-time-short
    134059

    ~/cool > iso-date-time
    2020-07-18t13:41:05

    ~/cool > iso-date-time-nice
    2020-07-18 13:41:10

    ~/cool > iso-date-time-str 
    20200718-134114

虽然我真的很喜欢 Babashka(和一般的 GraalVM!),但额外的安装步骤对于简单的东西来说太过分了。


See this repo 用于通用 GraalVM 演示和 Clojure 模板项目,然后您可以随时“自己动手”!