Date of following week

(imported topic written by SystemAdmin)

While coming up with a relevance recently, I realised that part of it would probably make a good opportunity for people to try to come up with a reasonably succinct solution.

Golf, anyone? (I certainly don’t condone the whole golfing process in working code, as some code can be quite unreadable even without condensing it to the point where, in comparison, line noise is easier to understand.)

Given the types of queries that come up in this forum, I’d say this is a fairly reasonable one to find a solution for, as no tuples or sets are necessary (although Ben or Zak might prove me wrong). Go ahead and fire up your brain and give it a try.

Problem: Using the returned time value of ‘Now’, come up with the Now formatted equivalent of the following weeks Thursday (same time is fine). So, if it was Tuesday, you’d only bump it forward 2 days, but on Wednesday you would add 8 days (Thursday, add 7, etc.).

Tue, 04 Jan 2011 09:01:23 +0000 -> Thu, 06 Jan 2011 09:01:23 +0000

Wed, 05 Jan 2011 09:12:34 +0000 -> Thu, 13 Jan 2011 09:12:34 +0000

-Jim

(imported comment written by SystemAdmin)

How about…

if (Thursday - (current day_of_week) < (2 * day)) then (now + ((Thursday - (current day_of_week)) + (7 * day))) else (now + (Thursday - (current day_of_week)))

(imported comment written by NoahSalzman)

Is that a birdie or an eagle?

(imported comment written by SystemAdmin)

Well, I was going to wait for a few more guesses before saying anything, but I think cirdermarks effort is a strong start at 159.

(I did a small refactor to part of his relevance and got it down to 149, but I’ll wait for other people to post before I put it up.)

(imported comment written by NoahSalzman)

I think we should be counting objects not chars… but whatever. :slight_smile:

  • See (substrings separated by “,”) vs (split) conversation

(imported comment written by BenKus)

Well… This is just the same solution that cidermark wrote with a bit of refactoring… But it is only 77 characters:

(if it/day<2 then now+it+7*day else now+it) of (Thursday-current day_of_week)

Ben

(imported comment written by SystemAdmin)

Ben, are you going to take the crown?

This is the one I had used previously:

(it;it+1day;it+2day;it+3day;it+4day;it+5day;it+6day)whose(it as string starts with “Wed”)of(now+2*day)

…but perhaps I have a new one waiting in the wings…(who can say?)

Anyone else going to have a try? This has to be more fun that all the ‘real world’ issues that keep cropping up on this forum.

-Jim

-Jim

(imported comment written by SystemAdmin)

I’m a little confused. So the results are supposed to give the Thursday of NEXT week or tomorrow (since today is Weds)? What if today is Thursday? Cidermark & Ben’s examples show the Thursday of next week, but the description of the puzzle seems to indicate whatever is the next upcoming Thursday.

Paul

(imported comment written by JackCoates91)

Hey Paul, you mean like this?

it whose (it as string starts with “Thu”) of (now+it*day) of (1;2;3;4;5;6;7)

77 characters.

(imported comment written by NoahSalzman)

“Th” instead of “Thu”? :slight_smile:

(imported comment written by SystemAdmin)

I figured that if I posted my initial code, it might give a clue to how I shortened mine, so I’ll show my final answer(and I’ll add that I had used ‘Th’ before Noah added his comment):

(now+(2+it)*day)whose(it as string starts with"Th")of(1;2;3;4;5;6;7)

68

Paul - I was thinking there would be an offset to manage whether the target day was for this week or next week, but that shouldn’t make a big difference in the calculations, as it would be quite easy to adjust. Apologies if my spec was misleading.

(imported comment written by SystemAdmin)

Good one. I was on the right track in my debugger, but for some reason I kept doing

elements of set of (1;2;3;4;5;6;7)

rather than just using

(1;2;3;4;5;6;7)

. Duh.

At first I was trying to use a time/date range forward 1 week and extract the one that starts with “Th”. But I wasn’t able to figure out how to pull out all the dates of a date range. I still think there should be a way to specify things like

times of (now & week)

.

Then it would have worked using

times whose (it as string starts with “Th”) of (now & (6*day))

Alas, maybe a feature request for the future. :slight_smile:

Paul

(imported comment written by JackCoates91)

I’d like to see

(1-7)

work…

(imported comment written by NoahSalzman)

As of version 8:

Q: integers in (1,7)

A: 1

A: 2

A: 3

A: 4

A: 5

A: 6

A: 7

(imported comment written by JackCoates91)

and indeed, this works:

it whose (it as string starts with “Th”) of (now+it*day) of integers in (1,7)

Though the integers keyword takes away any golfing advantage, at least it’s cleaner to read. Also, now that it’s Thursday I should note that if you want it to capture today as being the closest Thursday, you should use

(0,6)

instead of

(1,7)

.

(imported comment written by JackCoates91)

icing on the cake… here’s a real world use case. The Tuesday which is two weeks after Patch Tuesday is when we want a maintenance window to open:

it whose (it as string starts with “Tu”) of (first tuesday of current month_and_year + it * day) of integers in (15,21)

7 days after first tuesday is second tuesday.

14 days after is third tuesday, which we know we don’t want, so offset to 15.

21 days is as far as we want to go.

I checked that this rounds the horn into next month and year.

(imported comment written by SystemAdmin)

Hmm… If we could one day easily generate a large list of dates, I wonder which would be better to request as a future enhancement…

times in (now,now+week)

– similar to integers in (1,7)

or

times of (now & week)

– building on a time range object

Ok, now we have to start coming up with relevance to calculate Easter :stuck_out_tongue:

Then start asking for a holidays object like

date of holiday whose (name of it = “Easter”) of year of current date

Haha…

Paul

(imported comment written by JackCoates91)

holiday awareness is a big deal in maintenance windows and other service management type activities…

(imported comment written by Bill.Ehardt)

JackCoates

icing on the cake… here’s a real world use case. The Tuesday which is two weeks after Patch Tuesday is when we want a maintenance window to open:
it whose (it as string starts with “Tu”) of (first tuesday of current month_and_year + it * day) of integers in (15,21)
7 days after first tuesday is second tuesday.
14 days after is third tuesday, which we know we don’t want, so offset to 15.
21 days is as far as we want to go.
I checked that this rounds the horn into next month and year.

why would you want to do that as opposed to:

first tuesday of month_and_year of current date+3*week

(imported comment written by JackCoates91)

um, yeah, I guess that is better :slight_smile: