Man spart sich den Parser (Stringfield etc) und verwendet stattdessen einfach eine Maske, ähnlich FormatDate().
Unten im Code sind ein paar Beispiele.
Wird ggf. noch erweitert^^
Code: Alles auswählen
Procedure DateFromString(dateString.s, format.s, strict = #False)
Protected *c_date.character, *c_format.character
Protected c_date.c, c_format.c
Protected sizeOfChar = SizeOf(character)
Protected.s year, month, day, hour, minute, second
Protected pos.i, year_int.i
*c_date = @dateString
*c_format = @format
While *c_date\c <> 0
pos+1
c_date=*c_date\c
c_format=*c_format\c
Select c_format
Case 'Y','y' : year = year + Chr(c_date)
Case 'M','m' : month = month + Chr(c_date)
Case 'D','d' : day = day + Chr(c_date)
Case 'H','h' : hour = hour + Chr(c_date)
Case 'I','i' : minute = minute + Chr(c_date)
Case 's','s' : second = second + Chr(c_date)
Default
If strict = #True
If c_date <> c_format
Debug "STRICT: Error at position "+Str(pos)+": <"+Chr(c_date)+"> in DATE should be <"+Chr(c_format)+">"
ProcedureReturn -1
EndIf
EndIf
EndSelect
*c_date = *c_date+sizeOfChar
*c_format = *c_format+sizeOfChar
Wend
year_int.i = Val(year)
If Len(year)=2
If year_int>69
year = "19" + year
Else
year = "20"+year
EndIf
year_int.i = Val(year)
EndIf
If year_int<1970
Debug "Date must be greather than Jan 1st 1970"
ProcedureReturn -1
EndIf
If year_int>2037
Debug "Date must not be greater than Dec 31st 1937" ;eigentlich 19. Jan. 2038, laut Hilfe, aber die paar Tage braucht auch niemand...
ProcedureReturn -1
EndIf
ProcedureReturn Date(year_int, Val(month), Val(day), Val(hour), Val(minute), Val(second))
EndProcedure
date = DateFromString("01.17.89 19:17'33", "MM.DD.YY HH:ii'ss")
If date>-1
Debug FormatDate("%DD.%MM.%YYYY - %hh:%ii:%ss", date)
EndIf
date = DateFromString("01.17.89 19:17'33", "MM~DDuYY!HH-ii*ss")
If date>-1
Debug FormatDate("%DD.%MM.%YYYY - %hh:%ii:%ss", date)
EndIf
date = DateFromString("01.17.89 19:17'33", "MM~DDuYY!HH-ii*ss", 1) ; strict mode
If date>-1
Debug FormatDate("%DD.%MM.%YYYY - %hh:%ii:%ss", date)
EndIf
date = DateFromString("01.17.2089 19:17'33", "MM~DDuYY!HH-ii*ss") ;fehlerhafte ausgabe durch 4-stelliges Jahr im Datum, aber nur YY in der Maske
If date>-1
Debug FormatDate("%DD.%MM.%YYYY - %hh:%ii:%ss", date) + " - fehlerhafte Ausgabe"
EndIf
date = DateFromString("01.17.2089 19:17'33", "MM.DD.YY HH:ii'ss", #True) ;strict mode
If date>-1
Debug FormatDate("%DD.%MM.%YYYY - %hh:%ii:%ss", date)
EndIf