xv

ASOD: Date Calculation

Oct 18, 2011 02:36

This is pathetically simple, but I might develop it into something more useful.

{(current date) - (1 * weeks), (current date) - (4 * weeks)}

math, applescript, date

Leave a comment

Comments 4

xv October 19 2011, 06:46:44 UTC
This is a little more useful. It's taking my week code strings and converting it into an ordinary date:

set date_code to "41-T"

set {x, y, week_code} to {items 1 thru -3 of date_code as text as number, item -1 of date_code, "MTWRFAU"}

repeat with i from 1 to length of week_code
if y is item i of week_code then exit repeat
end repeat

set z to (date ("1/1/" & year of (current date))) + (x * weeks) + ((i + 1) * days)

-- sample result: date "Tuesday, October 18, 2011 12:00:00 AM"

Needs more work, though.

Reply

xv October 19 2011, 06:54:52 UTC
Derivative script. This one finds intervals and translates them to regular dates. This has dependencies though, and I'm not certain it will always work. (e.g. in other years)

set date_code to "41-T"

set {x, y, week_code} to {items 1 thru -3 of date_code as text as number, item -1 of date_code, "MTWRFAU"}

repeat with i from 1 to length of week_code
if y is item i of week_code then exit repeat
end repeat

set full_date to (date ("1/1/" & year of (current date))) + (x * weeks) + ((i + 1) * days)

set {z, interval} to {return, {1, 4, 12, 24, 52}}

repeat with i in interval
if (x - i) > 0 then set z to z & x - i & "-" & y & tab & (full_date - (i * weeks)) & return
end repeat

get z

Reply

sample result xv October 19 2011, 06:55:20 UTC
"
40-T Tuesday, October 11, 2011 12:00:00 AM
37-T Tuesday, September 20, 2011 12:00:00 AM
29-T Tuesday, July 26, 2011 12:00:00 AM
17-T Tuesday, May 3, 2011 12:00:00 AM
"

Reply

xv October 19 2011, 07:02:59 UTC
Correction, correction, correction. Disregard all above. I clearly didn't understand my math. This version should have the offset correctly adjusted. Remember, though, this is a static adjustment that will break from year to year and in special circumstances. I need to put in some code that gets the correct definition of the ISO standard involved.

set date_code to "42-T"

set {x, y, week_code} to {items 1 thru -3 of date_code as text as number, item -1 of date_code, "MTWRFAU"}

repeat with i from 1 to length of week_code
if y is item i of week_code then exit repeat
end repeat

set full_date to (date ("1/1/" & year of (current date))) + (x * weeks) + ((i - 6) * days)

set {z, interval} to {return, {1, 4, 12, 24, 52}}

repeat with i in interval
if (x - i) > 0 then set z to z & x - i & "-" & y & tab & (full_date - (i * weeks)) & return
end repeat

get z

Reply


Leave a comment

Up