Author |
Topic |
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 02 May 2002 : 17:37:19
|
Guys,
After some thinking on whether I should raise this as a bug, I decided that I should. The StrToDate function currently in inc_functions.asp fails in (at least some) computers with non US date formats. Just today I helped two people with problems due to the bad behavior of the function. I also had a previous bout with an Events MOD that failed miserably because of the same problem.
Will this be addressed in 3.4? Anyhow, for people with 3.3.03 and non US date formats, here goes my proposal (as copied from an earlier post from the Dev discussions forums):
function StrToDate(strDateTime)
if ChkDateFormat(strDateTime) then
'Testing for server format
If strComp(Month("04/05/2002"),"4")=0 Then
StrToDate = cdate("" & Mid(strDateTime, 5,2) & "/" & Mid(strDateTime, 7,2) & "/" & Mid(strDateTime, 1,4) & " " & Mid(strDateTime, 9,2) & ":" & Mid(strDateTime, 11,2) & ":" & Mid(strDateTime, 13,2) & "")
Else
StrToDate = cdate("" & Mid(strDateTime, 7,2) & "/" & Mid(strDateTime, 5,2) & "/" & Mid(strDateTime, 1,4) & " " & Mid(strDateTime, 9,2) & ":" & Mid(strDateTime, 11,2) & ":" & Mid(strDateTime, 13,2) & "")
End If
else
tmpDate = DatePart("m",strForumTimeAdjust) & "/" & DatePart("d",strForumTimeAdjust) & "/" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust)
StrToDate = "" & tmpDate
end if
end function
This doesn't change a thing for the people currently using US dates format. It helps those who have UK formats. It has the additional advantage of avoiding the need to add two fixes to the the current code (ReadLastHereDate in inc_functions.asp and another small change at forum.asp). These two fixes only address symptoms, not the cause.
------------------------------------------------- Installation Guide | Do's and Dont's | MODs
Edited by - ruirib on 02 May 2002 17:38:29 |
Edited by - Davio on 07 March 2003 11:33:27 |
|
pweighill
Junior Member
United Kingdom
453 Posts |
Posted - 02 May 2002 : 18:35:00
|
Instead of using the cdate function, how about using something like:
StrToDate = DateSerial(CInt(Mid(strDateTime, 1, 4)), CInt(Mid(strDateTime, 5, 2)), CInt(Mid(strDateTime, 7, 2))) + TimeSerial(CInt(Mid(strDateTime, 9, 2)), CInt(Mid(strDateTime, 11, 2)), CInt(Mid(strDateTime, 13, 2)))
|
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
pweighill
Junior Member
United Kingdom
453 Posts |
Posted - 03 May 2002 : 15:08:26
|
The advantage being that DateSerial and TimeSerial work no matter what the date & time formats the server are set to, whereas cdate doesn't.
|
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 03 May 2002 : 16:40:43
|
Well, the purpose of my function is to determine the date format in use. So it seems to me that in that specific situation using DateSerial or cdate doesn't make any difference, since I admit that I determined the format in use with the month comparison...
And to tell you the truth I just changed the already existing function from Snitz, to make it work where it failed. So far I have no knowledge of situations with Snitz users where cdate has failed, but I have nothing against using DateSerial, if your prefer it.
------------------------------------------------- Installation Guide | Do's and Dont's | MODs
Edited by - ruirib on 03 May 2002 16:56:50 |
|
|
HuwR
Forum Admin
United Kingdom
20584 Posts |
Posted - 03 May 2002 : 18:51:08
|
I don't see how your dateserial would help, if you recheck what ruirib posted, you will notice that he parses the strToDate in two different orders to get the correct one, and it is this part which is important since the month and date get swapped when writing the strToDate which is passed to it.
|
|
|
RichardKinser
Snitz Forums Admin
USA
16655 Posts |
Posted - 03 May 2002 : 19:46:32
|
fixed in v3.4 |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 07 May 2002 : 12:29:36
|
Well, yes this needs a new fix. Sorry guys, my own stupidity stopped me from seeing it .
The version posted above still has a problem, when the ChkDateFormat fails. That failure may occur, for example, when the function is called from forum.asp, with strForumTimeAdjust as a parameter. To fix it, the part relative to the situation where ChkDateFormat fails needs to be treated in a similar way to the part where it does not fail, that is, by considering the server date format. So here it is, hopefully, this function final version, with the latest changes in red:
function StrToDate(strDateTime)
if ChkDateFormat(strDateTime) then 'Testing for server format If strComp(Month("04/05/2002"),"4")=0 Then StrToDate = cdate("" & Mid(strDateTime, 5,2) & "/" & Mid(strDateTime, 7,2) & "/" & Mid(strDateTime, 1,4) & " " & Mid(strDateTime, 9,2) & ":" & Mid(strDateTime, 11,2) & ":" & Mid(strDateTime, 13,2) & "") Else StrToDate = cdate("" & Mid(strDateTime, 7,2) & "/" & Mid(strDateTime, 5,2) & "/" & Mid(strDateTime, 1,4) & " " & Mid(strDateTime, 9,2) & ":" & Mid(strDateTime, 11,2) & ":" & Mid(strDateTime, 13,2) & "") End If
else If strComp(Month("04/05/2002"),"4")=0 Then tmpDate = DatePart("m",strForumTimeAdjust) & "/" & DatePart("d",strForumTimeAdjust) & "/" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust) Else tmpDate = DatePart("d",strForumTimeAdjust) & "/" & DatePart("m",strForumTimeAdjust) & "/" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust) End If StrToDate = tmpDate end if end function
------------------------------------------------- Installation Guide | Do's and Dont's | MODs
Edited by - ruirib on 07 May 2002 12:30:32 |
|
|
GauravBhabu
Advanced Member
4288 Posts |
Posted - 11 August 2002 : 08:57:02
|
I think this is more compact and will work in all locales.
function StrToDate(strDateTime) if ChkDateFormat(strDateTime) then StrToDate = cdate("" & Mid(strDateTime, 7,2) & "-" & Monthname(Mid(strDateTime, 5,2)) & "-" & Mid(strDateTime, 1,4) & " " & Mid(strDateTime, 9,2) & ":" & Mid(strDateTime, 11,2) & ":" & Mid(strDateTime, 13,2) & "") else tmpDate = DatePart("d",strForumTimeAdjust) & "-" & Monthname(DatePart("m",strForumTimeAdjust)) & "-" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust) StrToDate = cDate(tmpDate) end if end function
|
CSS and HTML4.01 Compilant Snitz Forum . ForumSquare . Rakesh Jain
It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Prayer Of Forgiveness "I forgive all living beings. May all living beings forgive me! I cherish the friendliness towards all and harbour enmity towards none." -- Aavashyaka Sutra(Translated) |
Edited by - GauravBhabu on 11 August 2002 10:05:26 |
|
|
HuwR
Forum Admin
United Kingdom
20584 Posts |
Posted - 11 August 2002 : 10:07:35
|
? your function is pretty much what it was originally, which is what causes the problems.
The problem is that dates that get swapped are VALID dates, which is why ruirib's function checks an actual date to ascertain what the server is doing |
|
|
GauravBhabu
Advanced Member
4288 Posts |
Posted - 11 August 2002 : 10:22:39
|
quote: Originally posted by HuwR
? your function is pretty much what it was originally, which is what causes the problems.
Only difference in the function posted by me is that it passes the Monthname to cdate function instead of month number.
quote:
The problem is that dates that get swapped are VALID dates, which is why ruirib's function checks an actual date to ascertain what the server is doing
Month and day swapping occurs because the Cdate function processes the dates differently under different locale settings.
cdate function will return 8/11/2002(mm/dd/yyyy) as 8/11/2002(mm/dd/yyyy) or 8/11/2002(dd/mm/yyyy) depending on the locale settings.
But it will always return 11-Aug-2002 as 8/11/2002(mm/dd/yyyy) or 11/8/2002(dd/mm/yyyy).
The function is more compact and there is no need to find the server settings.
|
CSS and HTML4.01 Compilant Snitz Forum . ForumSquare . Rakesh Jain
It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Prayer Of Forgiveness "I forgive all living beings. May all living beings forgive me! I cherish the friendliness towards all and harbour enmity towards none." -- Aavashyaka Sutra(Translated) |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 11 August 2002 : 13:52:33
|
quote: Originally posted by GauravBhabu
Only difference in the function posted by me is that it passes the Monthname to cdate function instead of month number.
Well that makes the difference between this strategy succeeding or failing. |
Snitz 3.4 Readme | Like the support? Support Snitz too |
|
|
HuwR
Forum Admin
United Kingdom
20584 Posts |
Posted - 11 August 2002 : 16:49:06
|
GauravBhabu, I beg to differ.
if you pass it
8/11/2002(mm/dd/yyyy) or 11/8/2002(dd/mm/yyyy).
you will get a different result for monthname depending on the server because datepart 'm' will not be the same, hence the reason ruirib compares an actual date to see whether the function returns it as the correct month or not |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 11 August 2002 : 17:07:07
|
Well Huw, I think you're right if you think in terms of a generic function, to be applied with other dates than Snitz. However, with Snitz dates, the month always comes at the 5th and 6th positions of the string and the day at the 7th and 8th. So I believe that Gaurav's function will always returns the same values as my own's, since cdate will always get a date in the format "dd-MonthName-yyyy" and will output it according to the server date settings. My own testing, both with US and UK server settings seems to confirm that. Remember that StrToDate will always get dates in the Snitz format "YYYYMMDDHHMMSS", so the type of confusion you mentioned won't happen, IMO.
You can keep using "my" version, though. I see no problem with that
|
Snitz 3.4 Readme | Like the support? Support Snitz too |
Edited by - ruirib on 11 August 2002 17:36:54 |
|
|
HuwR
Forum Admin
United Kingdom
20584 Posts |
Posted - 11 August 2002 : 17:41:29
|
quote:
StrToDate will always get dates in the Snitz format "YYYYMMDDHHMMSS"
I wouldn't bet on it. I have seen them saved as YYYYDDMMHHMMSS
and besides, the problem is this line anyway tmpDate = DatePart("d",strForumTimeAdjust) & "-" & Monthname(DatePart("m",strForumTimeAdjust)) & "-" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust)
which uses a date not the forums string format |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 11 August 2002 : 18:06:13
|
quote: Originally posted by HuwR I wouldn't bet on it. I have seen them saved as YYYYDDMMHHMMSS
Well mine would fail there also.
quote:
and besides, the problem is this line anyway tmpDate = DatePart("d",strForumTimeAdjust) & "-" & Monthname(DatePart("m",strForumTimeAdjust)) & "-" & DatePart("yyyy",strForumTimeAdjust) & " " & DatePart("h",strForumTimeAdjust) & ":" & DatePart("n",strForumTimeAdjust) & ":" & DatePart("s",strForumTimeAdjust)
I think that is also taken care of because of the line following those,
StrToDate = cDate(tmpDate)
Since both DatePart and cDate use the server date and time settings, I think this time Gaurav's suggestion really works. I also think it's a more elegant solution and I like elegant solutions.
Besides the elegance of the solution, I think the differences between both are minimal. StrToDate doesn't get called that many times to make a significative difference in performance, the extra call to strComp and the additional if "my" function adds.
|
Snitz 3.4 Readme | Like the support? Support Snitz too |
|
|
Topic |
|
|
|