yamlscript

2024-12-17T17:01:00.106559Z

How can I get the current time in YAMLscript?

Ingy döt Net 2024-12-26T17:33:45.684289Z

@danielmartincraig I finished what I think I will do for now re data/time (in upcoming 0.1.88 release). I added the java-time.api library which you can access using the java-time namespace. I also added std/now . Here's some things you can do:

$ ys -pe 'java-time/instant()'
#object[java.time.Instant 0x5db4dbd5 "2024-12-26T17:31:30.014227Z"]
$ ys -pe 'java-time/instant().str()'
"2024-12-26T17:31:40.491337Z"
$ ys -pe 'java-time/instant():S'
"2024-12-26T17:31:47.035015Z"
$ ys -pe 'now()'
#object[java.time.Instant 0x3127c400 "2024-12-26T17:31:56.568229Z"]
$ ys -pe 'now():S'
"2024-12-26T17:32:01.390080Z"
$ ys -pe 'now().split(/[^\d]/)'
["2024" "12" "26" "17" "32" "13" "162033"]
$ ys -pe 'now().split(/[^\d]/).take(3).join("-")'
"2024-12-26"
$ ys -pe 'now().split(/[^\d]/).drop(3).take(3).join(":")'
"17:32:43"

Ingy döt Net 2024-12-26T17:35:00.459709Z

I'm wondering if std/now should return utc or zoned...

Ingy döt Net 2024-12-26T17:36:16.921009Z

Maybe both with:

now(:utc)
now(:zoned)
Might be best to default to zoned since it would cause the least surprise (likely).

2024-12-26T17:39:49.396199Z

if std/now defaults to UTC I think that would be best.

2024-12-26T17:40:52.576609Z

UTC has the advantage of being the same on my dev machine and in the cloud. It also is more predictable when daylight savings is coming up

2024-12-26T17:43:59.166499Z

but a function called localnow could be a nice convenience for those that really just want local time

Ingy döt Net 2024-12-26T17:48:33.036799Z

aye, but now() is a minimal convenience function for quick scripts and one liners. You probably want to be using java-time in real code:

$ ys -pe 'java-time/instant():S'
"2024-12-26T17:45:38.323953Z"
$ ys -pe 'java-time/local-date-time():S'
"2024-12-26T09:45:50.906428"
$ ys -pe 'java-time/zoned-date-time():S'
"2024-12-26T09:46:13.306874-08:00[America/Vancouver]"
If I just want the time here:
$ ys -pe 'str(now())'
feels like it should be my tz...

Ingy döt Net 2024-12-26T17:49:11.131059Z

Ironically I'm not in America/Vancouver I'm in Toronto, 3 hours east

Ingy döt Net 2024-12-26T17:49:28.231349Z

this is due to some java caching thing 😕

2024-12-26T17:49:54.826619Z

Oh cool, I'm interested in attending UBC

Ingy döt Net 2024-12-26T17:50:29.204859Z

I'm usually in Seattle

Ingy döt Net 2024-12-26T17:50:37.911879Z

UBC++ Vancouver++ 🙂

Ingy döt Net 2024-12-26T17:50:47.920599Z

I used to live in Vancouver

Ingy döt Net 2024-12-26T17:52:28.580359Z

ok well I'll default to UTC then and add

now(:local)
now(:zoned)
now(:utc) 

👍 1
2024-12-26T17:57:03.003229Z

That's the more cloud-forward thing to do in my opinion

Ingy döt Net 2024-12-26T18:02:12.183359Z

Easy to redefine (in a local context) if you want 🙂

$ ys -e 'now =: \(std/now :zoned)' -e 'say: "Now is $now()"'
Now is 2024-12-26T10:01:36.131175-08:00[America/Vancouver]

Ingy döt Net 2024-12-26T18:18:58.020989Z

@danielmartincraig datetime stuff committed and pushed to: https://github.com/yaml/yamlscript/tree/devel You can build/install ys from there with:

make -C ys install

2024-12-26T18:48:55.131839Z

awesome, thanks!

Ingy döt Net 2024-12-17T17:47:56.181689Z

That's a good question. Playing around with it at the airport. Looks like java.util.Date and java.time.* are not yet exposed. I can fix/release that tonight. This works now:

$ ys -pe 'System/currentTimeMillis().quot(1000)'
1734457315
for formatted date you could do this while you wait:
$ ys -pe 'sh("date").out:chomp'
"Tue Dec 17 09:47:47 AM PST 2024"
😄

Ingy döt Net 2024-12-17T17:49:43.639479Z

It would be nice to have a clean datetime api in ys. Any ideas?

2024-12-17T17:51:31.540289Z

I'm a huge fan of java.time.*

2024-12-17T17:51:54.132989Z

I would be happy just to have java.time.* exposed

2024-12-17T17:52:28.218449Z

by contrast, I really dislike java.util.Date and find that it usually leads to problems

2024-12-17T17:53:10.792709Z

Safe travels!

2024-12-17T17:53:21.318829Z

sorry to take your time while you're on the go

Ingy döt Net 2024-12-17T17:58:08.971629Z

np, and thanks. I will definitely expose java.time classes. Hopefully they are not very big. Should go out today. I also want a std/datetime that handles common datetime ops. But that can wait until I like it. Looking at https://momentjs.com/docs/ for inspiration. I remember liking it.

❤️ 1
Ingy döt Net 2024-12-17T18:04:45.908179Z

And guarded by a feature item. https://github.com/babashka/babashka/blob/c89ac0bd7fdd95c715bf0f4bfd85943049e34599/src/babashka/impl/classes.clj#L429 How do people typically do what you want in bb?

Ingy döt Net 2024-12-17T18:09:23.641399Z

$ bb '(java.time.LocalDate/now)'
#object[java.time.LocalDate 0x69ac4fb1 "2024-12-17"]
works. @danielmartincraig what's the scope of your needs for this currently?

2024-12-17T20:06:51.979029Z

It's not very urgent; there was a task I needed to do that seemed like a great choice for bringing yamlscript to work, but I was able to fall back to Python instead

2024-12-17T20:07:36.540429Z

so my needs are covered for now

2024-12-17T20:08:23.282169Z

The other thing I observed is that YAMLscript seems like it can't do a string to string alphabetical comparison with > or <

2024-12-17T20:08:54.547179Z

like, "abc" > "aaa" would not be a valid comparison in ys

2024-12-17T20:10:30.981249Z

I was attempting compare datestrings alphabetically (not advisable, but I was just trying everything I could think of)

Ingy döt Net 2024-12-17T21:48:59.922809Z

I can make "abc" > "aaa" work and I don't have a problem with that since "abc" + "aaa" already does work

Ingy döt Net 2024-12-17T21:52:09.462839Z

Infix + is polymorphic, where prefix + is normal clojure:

$ ys -ce '(a + b)'
(add+ a b)
$ ys -ce '(+ a b)'
(+ a b)

Ingy döt Net 2024-12-17T21:52:21.730459Z

if perf matters

Ingy döt Net 2024-12-17T21:53:51.770329Z

That's a general YS principal. You can make YS compile how you want.

2024-12-17T21:54:48.378579Z

OK gotcha thanks for the explanation

Ingy döt Net 2024-12-17T21:55:34.396199Z

Here's a good example:

$ ys -pe 'range().take(5)'
(0 1 2 3 4)
$ ys -pe 'range().take(5 _)'
(0 1 2 3 4)
$ ys -ce 'range().take(5)'
(+take (range) 5)
$ ys -ce 'range().take(5 _)'
(take 5 (range))

Ingy döt Net 2024-12-17T21:59:34.367089Z

like as-> threading you can state where the LHS goes, but for most core functions you don't need to. You'll pay a runtime perf penalty by not using _ , but generally you probably don't care. You don't need to remember the arg order of take.

$ ys -pe '5.take(range())'
(0 1 2 3 4)
also just does the right thing.

Ingy döt Net 2024-12-17T22:00:28.099129Z

if you care about take over +take you add the _.

Ingy döt Net 2024-12-17T22:01:18.913149Z

I almost never use the _ in my YS programming.

Ingy döt Net 2024-12-17T22:02:19.346659Z

@danielmartincraig I have at least 4 hours to kill on this layover, so I'll work on those things now.

2024-12-17T22:17:45.186879Z

Interesting, I'm sure the penalty is very minor in this case though right? I don't think it should be too bothersome in general

Ingy döt Net 2024-12-17T22:31:53.644779Z

say that collis set to range() and num is set to 5 ... there's so many ways to take 5 in ys:

(take num coll)
take(num coll)
coll.take(num)
num.take(coll)
coll.take(num _)
num.take(_ coll)
take: num coll
take num: coll
take num coll:
call('take' num coll)
-"take".call(num coll)
call: -'take' num coll
it's worth using ys -c once in a while to see how these forms compile to lisp/clojure

Ingy döt Net 2024-12-17T22:34:59.846159Z

the ys compiler can be made smarter in the future to generate faster code when it knows the argument types at compile time.

Ingy döt Net 2024-12-17T22:36:22.504959Z

> I'm sure the penalty is very minor in this case though right? but yeah, I rarely need to think about it.

Ingy döt Net 2024-12-17T22:51:49.430839Z

https://clojurians.slack.com/archives/C0GLTDB2T/p1733605641742229?thread_ts=1733549106.914809&amp;cid=C0GLTDB2T is from #adventofcode where I showed YS side-by-side with the Clojure it compiles to. Using a little YS script to format it: https://github.com/ingydotnet/advent-of-code/blob/main/bin/sbs In general YS has ways to make code really minimal. I of course realize the benefits of coding things in all lisp (I'm a Clojure programmer after all) but for YS it makes more sense to offer alternate forms that fit well into YAML. When I'm writing YS code, lisp/clojure is my assembly language! 😄

Ingy döt Net 2024-12-17T23:07:12.139719Z

Having written other transpilers in the past (but not to lisp) I would say that lisp is a great language to compile to.