Re: Anyone know of a good datetime importing func or program? by Jeroen
Jeroen
Tue Jul 01 18:32:14 CDT 2003
Small error in the year calculation..
added monthnames
added 2digit year conversion
added errorhandling
todo? timezones?
function getDateTime
* convert date from text to datetime
PARAMETERS pcLayout,pcString
* pcLayout contains the basic layout in letters
* c century (only if seperated from year)
* y year
* m month
* d day
* h hour
* n miNute
* s second
* u millisecond
* p check PM/AM
* x ignore (or any other undefined char)
* examples
* xmdhnsy Mon Jun 02 20:48:42 2003
* ymdhnsu 2003-06-09 00:00:01.000
* hnsuxxmdy 23:04:59.575 UTC Sat Jan 8 2003
* dmyhnsp 1/1/2003 1:1:3pm
* dmyhnsp 11/11/2003 11:11:23pm
#DEFINE seperators " /-:."
local lnYear,lnMonth,lnDay,lnHour,lnMinute,lnSecond,lnMilli
local llCheckPM,lcWord,ltResult,lcOnError
*cleanup pcLayout
pcLayout=CHRTRAN(LOWER(pcLayout),' '+seperators,'') && remove
unnecesary characters
IF 'p'$pcLayout
llCheckPM=.t.
pclayout=CHRTRAN(pcLayout,'p','') && remove from layout
ENDIF &&llCheckPM defaults to .f. in the declaration
lnYear=VAL(GETWORDNUM(pcString,AT('y',pcLayout),seperators))+;
VAL(GETWORDNUM(pcString,AT('c',pcLayout),seperators))*100
DO CASE
CASE lnYear<20 && rollover year
lnYear=lnYear+2000
CASE lnYear<100
lnYear=lnYear+1900
ENDCASE
lcWord=GETWORDNUM(pcString,AT('m',pcLayout),seperators)
IF VAL(lcWord)>0
lnMonth=VAL(lcWord)
ELSE
* get month number from monthnames
lcWord=LOWER(LEFT(lcWord,3))
DO case
CASE INLIST(lcWord,'jan')
lnMonth=1
CASE INLIST(lcWord,'feb')
lnMonth=2
CASE INLIST(lcWord,'mar','maa','mrt')
lnMonth=3
CASE INLIST(lcWord,'apr')
lnMonth=4
CASE INLIST(lcWord,'may','mei')
lnMonth=5
CASE INLIST(lcWord,'jun')
lnMonth=6
CASE INLIST(lcWord,'jul')
lnMonth=7
CASE INLIST(lcWord,'aug')
lnMonth=8
CASE INLIST(lcWord,'sep')
lnMonth=9
CASE INLIST(lcWord,'oct','okt')
lnMonth=10
CASE INLIST(lcWord,'nov')
lnMonth=11
CASE INLIST(lcWord,'dec')
lnMonth=12
OTHERWISE
lnMonth=0
ENDCASE
ENDIF
lnDay=VAL(GETWORDNUM(pcString,AT('d',pcLayout),seperators))
lnHour=VAL(GETWORDNUM(pcString,AT('h',pcLayout),seperators))
IF llcheckPM AND 'pm'$LOWER(pcString)
lnHour=lnHour+12
endif
lnMinute=VAL(GETWORDNUM(pcString,AT('n',pcLayout),seperators))
lnSecond=VAL(GETWORDNUM(pcString,AT('s',pcLayout),seperators))
lnMilli=VAL(GETWORDNUM(pcString,AT('u',pcLayout),seperators))
lcOnError=ON('error') && remember errorhandler
ON ERROR ltResult={/:} && if error, then return empty string
ltResult=DATETIME(lnYear,lnMonth,lnDay,lnHour,lnMinute,lnSecond)+lnMilli/1000
ON ERROR &lcOnError
RETURN ltResult