PDA

View Full Version : Two oddities with date functions



VaioUserChris
February 2nd, 2006, 07:21 PM
Mike/Ron,

I think I found an error with one of the date functions. When you use the ":findndow" function to get the xth Monday of a month the date it returns is actually the xth Thursday. Likewise the xth Tuesday returns the xth Wednesday.

All other days are fine. Run the following script to see what I'm talking about...
est_date = "7.2.2006 00:00:00"

require 'date'
local d = date:parse( test_date )
print ("our test date =",d)


test_date2 = d:findndow (1,1)
print ("This is supposed to be the first Monday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,2)
print ("This is supposed to be the first Tuesday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,3)
print ("This is supposed to be the first Wednesday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,4)
print ("This is supposed to be the first Thursday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,5)
print ("This is supposed to be the first Friday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,6)
print ("This is supposed to be the first Saturday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (1,7)
print ("This is supposed to be the first Sunday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,1)
print ("This is supposed to be the second Monday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,2)
print ("This is supposed to be the second Tuesday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,3)
print ("This is supposed to be the second Wednesday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,4)
print ("This is supposed to be the second Thursday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,5)
print ("This is supposed to be the second Friday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,6)
print ("This is supposed to be the second Saturday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

test_date2 = d:findndow (2,7)
print ("This is supposed to be the second Sunday of this month:",test_date2,"-- This day is actually a",test_date2.WeekDay)

The second one is more of a question than an error. I noticed that in one of the provided date examples it starts with a date on 12/31 but after converting it using the date function it ends up with 7am on 1/1. But if you specify the hour:minute:second on 12/31 it converts the date to 12/31. Why does it change the date if you don't specify the time? (here's the example)
require 'date'

local test_date1 = "31-12-2000"
local test_date2 = "31-12-2000/00:00:00"

local converted_date1 = date:parse( test_date1 )
local converted_date2 = date:parse( test_date2 )

print ("test date one was:",test_date1," Converted date one is:",converted_date1)
print ("test date two was:",test_date2," Converted date two is:",converted_date2)Chris

Promixis
February 3rd, 2006, 04:04 AM
Chris,

I will try and look at this later today.

VaioUserChris
February 4th, 2006, 03:00 PM
Mike,

I've investigated further and I think I found the error/fix for the first item. I've copied the "findndow" function code from the date.lua script and made one key edit (shown below)...
function findndow2 (n, dow, InputDate)

local x = dow
month = InputDate.Month;
year = InputDate.Year;

if n > 0 then
local d = date:new ({Day =1,Month = month,Year = year});
x = dow - d.DayOfWeek
if x < 0 then
--x = math.abs &#40;x&#41; - 1 -- this line is causing the error
n = n + 1 -- I think it should be this
end
d.Day = d.Day + x + &#40;n - 1&#41; * 7;
return d
else
local d = date&#58;new &#40;&#123;Day =InputDate.daysinmonth &#40;month,year&#41;,Month = month,Year = year&#125;&#41;
x = dow - d.DayOfWeek
if x > 0 then
x = -7+ x
end
d.Day = d.Day + &#40; x + &#40;math.abs&#40;n&#41; - 1&#41; * 7&#41;
return d
end
end


test_date = "4.4.2006 00&#58;00&#58;00"

require 'date'
local d = date&#58;parse&#40; test_date &#41;
print &#40;"our test date =",d&#41;

wkday_names = &#123;"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"&#125;

for ii = -1,4 do
if ii ~= 0 then
for i = 1,7 do
test_date2 = findndow2&#40;ii,i,d&#41;
print &#40;"This is supposed to be the",ii.."th",wkday_names&#91;i&#93;," of this month&#58;",test_date2,"-- This day is actually a",test_date2.WeekDay&#41;
end
end
end

With this change it seems to consistently work correctly...
Chris

VaioUserChris
February 4th, 2006, 03:43 PM
And here's a fix to the parse command which solves the missing hour == date change problem...
-- parses a given date string and creates a DateTime object
function parse&#40; datestr &#41;
local Year, Month, Day, Hour, Minute, Second, i, j, _
local datepattern = "^&#40;%d+&#41;&#91;%.%-_&#93;&#40;%d+&#41;&#91;%.%-_&#93;&#40;%-?%d+&#41;"
local timepattern = "^&#40;%d+&#41;&#58;&#40;%d+&#41;&#58;&#40;%d+&#41;"
-- parse date
i, j, Day, Month, Year = string.find&#40; datestr, datepattern &#41;
if not i then -- no date found, assume toDay
_, _, Day, Month, Year = string.find&#40;
os.date&#40; "%d.%m.%Y" &#41;,
datepattern
&#41;
j = 1
else
j = j + 2 -- skip delimiter
end
-- parse time
_, _, Hour, Minute, Second = string.find&#40; datestr, timepattern, j &#41;
if not Hour then
Second = string.format&#40;"%02d",0&#41;--
_, _, Hour, Minute = string.find&#40; datestr, "^&#40;%d+&#41;&#58;&#40;%d+&#41;$", j &#41;
if not Hour then
if string.find&#40;datestr,"&#91;%.%-_&#93;"&#41; then
-- if the string contains a date delimiter the time must be missing so fill with zeros
Hour = string.format&#40;"%02d",0&#41;
Minute = string.format&#40;"%02d",0&#41;
else
-- if the string did not contain a date delimiter then it must have been a time -- parse time
if string.len &#40;datestr&#41; == 3 then
Hour = string.format&#40;"%02d",string.sub &#40;datestr,1,1&#41;&#41;
else
Hour = string.sub &#40;datestr,1,2&#41;
end
Minute = string.sub &#40;datestr,-2,-1&#41;
end
end
end
return print&#40; Year, Month, Day, Hour, Minute, Second &#41;
end

--require 'date'

local test_date1 = "31-12-2000"
local test_date2 = "31-12-2000/01&#58;00&#58;00"
local test_date3 = "31-12-2000/01.00.00"
local test_date4 = "31-12-2000/23&#58;37"
local test_date5 = "23&#58;37"
local test_date6 = "0337"
local test_date7 = "337"



print &#40;"input&#58;",test_date1&#41;; parse&#40; test_date1 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date2&#41;; parse&#40; test_date2 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date3&#41;; parse&#40; test_date3 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date4&#41;; parse&#40; test_date4 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date5&#41;; parse&#40; test_date5 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date6&#41;; parse&#40; test_date6 &#41;;print&#40;" "&#41;
print &#40;"input&#58;",test_date7&#41;; parse&#40; test_date7 &#41;;print&#40;" "&#41;

Promixis
February 5th, 2006, 06:35 AM
Thanks!

Would you mind uploading the date.lua file here?

VaioUserChris
February 5th, 2006, 07:53 PM
Here you go...

Chris

Promixis
February 7th, 2006, 09:50 AM
thanks!

VaioUserChris
February 7th, 2006, 11:09 AM
No problem. Glad to help.

Chris

Promixis
August 26th, 2006, 07:08 PM
ok, got it this time ;)