Fork me on GitHub
#clojure
<
2022-11-24
>
M J14:11:31

Quick/urgent question I am getting a time in UTC: 2022-11-24T11:50:26Z How can I convert it to IST (Israel Standard Time)?

danboykis14:11:05

Are you using java.time.Instant to get the original UTC?

M J14:11:23

Using moment preferably

M J14:11:55

This is the code I am trying to convert (.utc moment date) I already take the date from the backend

danboykis14:11:22

Sorry, I don't know how moment works 🤷

jjttjj14:11:34

Here it is using java.time:

(import [java.time ZoneId Instant])

(-> (Instant/parse "2022-11-24T11:50:26Z")
    (.atZone (ZoneId/of "Asia/Jerusalem")))

jjttjj14:11:22

Moment is js right?

localshred14:11:26

you'll want to install the additional moment-timezone package

M J14:11:31

Okay lets say I wanna use this exactly how do I convert it to clojure (sorry Im a beginner): ((date, "America/New_York"))

M J14:11:03

So I saw the link for timezones, hw do I translate it to Clojure exactly I dont know

localshred14:11:42

(moment/tz date "America/New_York")

localshred14:11:18

or (.tz moment date "America/New_York")

localshred14:11:42

the moment reference may be different depending on how you required the libraries

M J14:11:24

Both give me same result: (println (.utc moment date))

(println (.tz moment date "Israel/Jerusalem"))

localshred14:11:32

When I run that I get

localshred14:11:36

Moment Timezone has no data for Israel/Jerusalem. See .

localshred14:11:26

The TZ name is just "Israel"

M J14:11:33

Okay thanks turns out its a bug with moment.js How do I do this then?

moment(startdate).add(2, 'hours')

localshred14:11:57

I just used the fn and regex matched against israel

localshred14:11:20

(.add (moment startdate) 2 "hours")

M J14:11:41

Yeah I found it. Its a bug with ".fromNow date"

M J14:11:55

Whenever we use .fromNow it doesnt take timezone into acount

localshred14:11:58

I'd suggest reading this post that can help you understand how to access properties and call functions on js objects in clojurescript https://www.verypossible.com/insights/clojurescript-and-javascript-interoperability-a-comprehensive-guide

M J15:11:15

Thanks alot everyone!

👍 1
ericdallo19:11:28

Kind of newbie question, but I didn't find anything mention about it on https://github.com/bbatsov/clojure-style-guide: What I should use on imports, brackets or parenthesis

(ns foo (:import (java.util Date)))
;; or
(ns foo (:import [java.util Date]))

ericdallo19:11:00

I feel like parens is the recommended one, but can't remember where I saw that

ericdallo19:11:16

I'm improving clojure-lsp to add imports automatically, and not sure I should add a flag for those or just go with the most recommended way...

borkdude19:11:23

I use brackets for all imports, java and clojure

2
ericdallo19:11:11

So no tradeoffs, it's just visually in the end? maybe we should mention it somewhere on the style guide

borkdude19:11:46

I think How to ns by Sierra mentioned parens, because the first symbol was special, but you could also say: avoid parens since they remind of a function call. 🤷

ericdallo19:11:44

oh, there was where I saw that mention, thanks Yeah, TBH I don't have a personal preference, so I was expecting someone say: always use that because of X, kind of annoying need to support both by a flag 😔

mpenet19:11:55

I personally prefer ( in that case

borkdude19:11:09

I feel a configuration option coming up ;)

dharrigan19:11:15

I use brackets too

mpenet19:11:21

i think it varies quite a bit depending on ppl

borkdude19:11:41

At least we agree it shouldn't be a set ;)

😂 1
dharrigan19:11:02

It is defined in the style guide (via examples), i.e., https://guide.clojure.style/#sort-requirements-and-imports

ericdallo19:11:52

I see no :import mention on that example @U11EL3P9U

dharrigan19:11:00

see just pasted

borkdude19:11:06

I always use [foo Bar] , never foo.Bar even when I import a single class

dharrigan19:11:26

Indeed, I use [foo bar] for import too 🙂

mpenet19:11:31

even in clojure itself it all mixed up

🤯 1
ericdallo19:11:31

Yeah I saw that one, but didn't convinced me as it doesn't explicitly tell about those

ericdallo19:11:17

yeah, ATM clojure-lsp is using full-package-import sym so no vectors or list 😂 (I think I did that in the past to not need to choose between any of those 😂)

ericdallo19:11:02

Yeah, indeed there are more examples showing [

ericdallo19:11:26

so maybe go with the [ by default and add a option to use (

dharrigan19:11:42

👍 (for me!)

3
mpenet19:11:01

in core, the same person uses one or the other depending on the ns 🙂

👍 1
mpenet19:11:15

could be from contrib migration

mpenet19:11:27

in 1.1 (?) days

borkdude19:11:18

clojure core contributions usually don't change style things, not even whitespace. you can't just hit format in emacs and then write a patch (found this out the painful way of course)

☝️ 1
ericdallo19:11:27

Nope, I know that exists but I have no idea what will happen TBH 😅 , it's not officially supported and IIRC it's not recommended anymore right?

mpenet19:11:28

it's quite uncommon tho, I think I have only seen ztellman use that

mpenet20:11:04

yeah, maybe it just works by accident

🤞 1
mpenet20:11:51

I believe it's not recommended

borkdude20:11:12

ah nested libspecs, yeah avoid those ;)

borkdude20:11:23

clj-kondo does understand them, but they are painful to work with

borkdude20:11:50

it is supported by clojure, not by accident. SCI / bb also supports them (because of compatibility)

phill00:11:25

"Programming Clojure" 2e by Halloway & Bedra uses parentheses on p.44 and square brackets on p.72

valerauko00:11:46

i wanted to use those in clojurescript because typing my-app.components.pages or my-app.api.middleware 67 times is a pain but sadly it's not in cljs 😞

ghadi01:11:11

I do [] and have heard Rich say the same

👍 2
👀 2
mpenet13:11:28

something I noticed yesterday while playing with both styles + clojure-lsp: [a.b c\nd] will get c indented under a, and with (...) c and d will form a column

(:import (io.helidon.common.http Http$HeaderValue
                                 Http$Status))

(:import [io.helidon.common.http Http$HeaderValue
          Http$Status))

mpenet13:11:08

I find the former more readable personally, is that something we can/could customize if one would like to use [] and have the same indentation as with ()

ericdallo13:11:02

the latter should hot happen, clojure-lsp should move all to the same line or all to the next line, BTW I made some improvements on that yesterday, maybe test with nightly #C032YH7P0R2

mpenet13:11:43

great, I ll have a look, thanks

mpenet13:11:05

on clojure-lsp 2022.11.24-22.12.04-nightly it's unchanged

mpenet13:11:27

oh no nevermind

mpenet13:11:56

clean namespace shuffles things like you said, my remark was about format-buffer

ericdallo13:11:38

hum, so cljfmt is doing that

mpenet13:11:03

I suppose it's possible to fix with a custom indent rule in cljfmt

ericdallo13:11:54

maybe, one thing I'm considering doing is calling cljfmt format after a clean-ns, to keep consistency, needs more research to avoid issues like that one

mpenet13:11:48

it's tricky. if you have format buffer on-save-hook or something it gets a bit heavy

ericdallo13:11:48

ah, my idea was to do that programaticaly on clojure-lsp side:

1. user reuqests clean-ns refactoring on editor 
2. clojure-lsp does the clean-ns
3. clojure-lsp call cljfmt passing only the ns code to format
4. clojure-lsp returns the clean-ns code for editor apply
The idea was to add this 3. during the clean-ns existing refactoring

mpenet13:11:34

you'd have to settle on a "bunching" style for when there are multiple classes aliased for a package tho

mpenet13:11:46

whereas now it just accepts whatever happily

mpenet13:11:13

within some formating rules still

ericdallo14:11:33

yes, we would need to check if there is any config we always want to send + the specified by user on settings.