Fork me on GitHub
#clojure-uk
<
2018-04-10
>
danm08:04:32

elloello

danm08:04:43

@mccraigmccraig Good point. My local cycle club does a loop towards the south east of Manchester and we always end up in the Peaks somewhere. Some lovely views and 'lovely' hills

mccraigmccraig08:04:18

great off-road too... i did a ladybower reservoir circuit some time last year, which was a lot more entertaining than anything i've met down here

danm08:04:12

I own an old hardtail MTB and do go out occasionally, but given how badly I faired when someone else in the club took a few of us out for a 'beginner ride' where he was on a single speed rigid I think it's road riding for me

mccraigmccraig09:04:15

haha, you get used to the off-road stuff quite quickly, then someone suggests you go on a trip to BPW and the next thing you know you are practising wheelies and manuals (still can't do 'em, but i am practising 😬 ) and jumps and stuff...

mccraigmccraig09:04:26

plus i get that snowboard-like feeling of absolute flow-concentration off-road, which i never seem to get on-road

chrjs10:04:56

Morning all.

mccraigmccraig10:04:53

(from a blurry distance anyway)

danm10:04:02

That reminds me, I have a few episodes of Ash vs Evil Dead not watched yet

jasonbell10:04:30

@mccraigmccraig It's near enough 🙂

😂 4
Aleksander10:04:00

morning everyone!

maleghast10:04:18

Hello All 🙂

maleghast10:04:14

Does anyone in here know how to do the following using clojure.java-time? I want to calculate the number of months between 2 dates. So for example, if num-months existed as a function:

(num-months (clojure.java-time/local-date 2018 7 1) (clojure.java-time/local-date 2018 9 30))

maleghast10:04:10

Can I literally subtract the more recent date from the more distant date and then use:

(months ...)

Sam H10:04:32

oops just realised that’s for clj-time

maleghast11:04:20

@shan - Yeah I can find LOADS of stuff using clj-time / Joda

maleghast11:04:25

Thanks anyway 🙂

maleghast11:04:57

(I could use clj-time / Joda for this in parallel to clojure.java-time, but I'd rather stick to one paradigm then that wins imho)

Rachel Westmacott12:04:32

I use java.time quite a bit, and would probably drop down to java interop to get the number of months between two dates. eg. (.until local-date-a local-date-b ChronoUnit/MONTHS)

maleghast14:04:47

@peterwestmacott - Thanks very much, sorry I went into a long meeting

maleghast14:04:02

I am going to give that a try now 🙂

maleghast14:04:58

Hmmm, I am getting an error that ChronoUnit does not exist as a symbol

maleghast14:04:26

Do I need to add something to my namespace to add my ability to use that..?

guy14:04:41

you might need to import the java class?

guy14:04:48

where is ChronoUnit from

maleghast14:04:49

At the moment I am using the wrapper clojure.java-time

maleghast14:04:13

@guy - Yeah, that's my thinking, but I don't have the Java knowledge to know the answer.

guy14:04:27

like this maybe?

guy14:04:45

(:import [java.time.chrone ChronoUnit]) perhaps?

guy14:04:52

thats my guess

maleghast14:04:15

Yeah, I reckon so... Would that go in my (ns ...) at the top of the file

guy14:04:24

yeah click the link above

guy14:04:35

its an example of importing java classes i believe

maleghast14:04:38

Ah ok, thanks...

👍 4
danm14:04:17

I'm always vaguely surprised when Clojure devs haven't had to do work around the Java ecosystem and importing and using Java libs, but thinking about it 99% of what we've done with it is either Kafka or datetime 😉

maleghast14:04:42

@peterwestmacott - Do you know off the top of your head where I can (:import ...) ChronoUnit from, please?

maleghast14:04:16

I am going to be going to look at the JavaDocs, but if you happen__ to know then that's easier 😉

Rachel Westmacott14:04:53

it’s in the java.time.temporal package

Rachel Westmacott14:04:08

(sorry, was not paying attention to Slack for a bit)

maleghast14:04:40

No worries @peterwestmacott - thanks very much

Rachel Westmacott14:04:17

one of the perks of using IntelliJ is finding Java stuff is trivial

4
maleghast14:04:30

Weird results however... Notionally I would expect that 2018/07/01 -> 2018/09/30 would be 3 months, but Java says that it's 2

maleghast14:04:05

I think that I get why it's 2, mathematically, but it's a real pain...

Rachel Westmacott14:04:21

if you want to do your own rounding, then you could try ChronoUnit/DAYS and round it manually

Rachel Westmacott14:04:36

at some point you have to decide what a ‘Month’ is though before you settle on an approach

Rachel Westmacott14:04:44

is it ‘30 days’?

maleghast14:04:47

*nods* Yeah, I suppose so.

maleghast14:04:10

I want to be able to do Calendar months, i.e. "How many months is this on a calendar?"

Rachel Westmacott14:04:22

well how many is it?

maleghast14:04:26

(the way that humans understand calendars.)

Rachel Westmacott14:04:30

it looks like two whole months and then some days to me

maleghast14:04:46

3months, it's all of July, all of August and all of September

maleghast14:04:10

notionally, the way someone looks at a calendar and gets to num of months

Rachel Westmacott14:04:17

it isn’t all of september (imho)

Rachel Westmacott14:04:32

intervals are usually exclusive at one end

maleghast14:04:37

I am working on a UI, where there will be a start date and an end date.

maleghast14:04:24

A non-programmer will see 1st July to 30th September as start of 1st of July to end of 30th September

Rachel Westmacott14:04:48

if your 30th September is meant to represent the end of the month, then maybe you want to either a) include a time component and make it the instant of midnight on the first day of the next month, or b) increment by one day prior to your month calculation

maleghast14:04:05

I get that there is a mathematical answer that is more accurate, but non-programmer, users are not seeing that mathematical reality

maleghast14:04:47

I think that adding 1 day to the end-date in the background (i.e. hidden from the user) is the way to get accurate date maths in this case.

Rachel Westmacott14:04:58

the trouble is that even with the right date/time libraries you still have to think about dates and times

maleghast14:04:15

*nods* Yeah, I think so. 🙂

Rachel Westmacott14:04:21

that probably is the ‘good enough’ solution here

maleghast14:04:54

I've had a better idea, @peterwestmacott - take the days out of the equation and let the user only choose year and month

Rachel Westmacott14:04:24

if that’s all the precision you need then that sounds good

Rachel Westmacott14:04:43

There’s a java.time class for that

Rachel Westmacott14:04:48

java.time.YearMonth

maleghast15:04:32

Yeah, this works:

(.until (jt/local-date 2018 7) (jt/local-date 2018 10) ChronoUnit/MONTHS)

🎉 8
parrot 4
maleghast15:04:51

That gives 3 as the answer, which is exactly what I wanted. 🙂