date functions missing pm and am

Just starting out? Need help? Post your questions and find answers here.
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

date functions missing pm and am

Post by wichtel »

Hallo,
I've written a program to create reports out of log files.
On german logs it works fine. On american logs it won't because of the date functions.

e.g. I try to read the string below and convert it to a date
german: time 29.04.2003 14:56:47

american:
time 5/6/2003 4:29:48 PM

The date function do not support AM/PM and I have problems with the missing leading zeros.
Is there a library for american style dates ?

Michael
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: date functions missing pm and am

Post by PB »

> e.g. I try to read the string below and convert it to a date
> german: time 29.04.2003 14:56:47

Are you saying that the log files you're reading has the dates in the format
shown above, and you'd like to display them as American dates?
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Should be easy enough, if you detect the AM/PM just add 12 hours if necessary... You would probably have to parse the string yourself as the ParseDate() command seems to only work on 2 digit values for the day and month post....
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Thanks,
done.

extract:

Code: Select all


;Europe=0
;Americas=1
#region=0

#am=0
#pm=3600*12

CompilerIf #region=0
  datimask="%dd.%mm.%yyyy %hh:%ii:%ss"
  region="EMEA"
CompilerElse  
  datimask="%mm/%dd/%yyyy %hh:%ii:%ss"
  region="US"
CompilerEndIf  

Procedure.s MyFormatDate(datim.l)
temp.s
result.s
  CompilerIf #region=0
    result=FormatDate(datimask,datim)
  CompilerEndIf
  CompilerIf #region=1
    result=FormatDate(datimask,datim)
    temp=Left(StringField(result,2," "),2)
    If Val(temp)>12
      datim-#pm
      result=FormatDate(datimask,datim)+" PM"
    Else
      result+" AM"
    EndIf  
  CompilerEndIf
ProcedureReturn result
EndProcedure

Procedure scanrec(orec.s)
line.s
field.s
value.s
datim.s
ampm.s
info.s
AddElement(alrec())
If ReadFile(0,orec) 
  Repeat 
    line=ReadString()
    field=Trim(ReplaceString(StringField(line,1,":"),Chr(9)," ",1))
    Select field
    Case "Priority"
      alrec()\priority=Trim(ReplaceString(StringField(line,2,":"),Chr(9)," ",1))
    Case "Message"
      CompilerIf #region=0
        datim=StringField(line,2," ")+" "+Left(StringField(line,3," "),8)
        alrec()\timestamp=ParseDate(datimask,datim)
        alrec()\info=StringField(line,2,"]")
        alrec()\message=StringField(line,12," ") 
        If alrec()\message="the"
          alrec()\message="EICAR.test.virus" 
        EndIf
      CompilerEndIf
      CompilerIf #region=1
        datim=StringField(line,2," ")+" "+Left(StringField(line,3," "),8)
        ampm=Left(StringField(line,4," "),2) 
        alrec()\timestamp=ParseDate(datimask,datim)
        If ampm="PM"
          alrec()\timestamp+#pm
        EndIf  
        alrec()\info=StringField(line,2,"]")
        alrec()\message=StringField(line,14," ") 
        If alrec()\message="the"
          alrec()\message="EICAR.test.virus" 
        EndIf
      CompilerEndIf
    Case "From"
     alrec()\from=StringField(Trim(ReplaceString(StringField(line,2,":"),Chr(9)," ",1)),1,".")
    EndSelect
Until Eof(0)<>0 
  CloseFile(0) 
EndIf 
EndProcedure

This works for me, cause the US version will always run on a US server, the EMEA version on a german server.
scanrec reads logfiles with a fixed format, that I can't change and fills a linked list.
This List us used to create a table and then a HTML file.
MyFormatDate is used to convert the date back to a string.

Michael

P.S:
I didn't post the complete code, cause without the server and the log files and the other html and idc/htx scripts around it, it doesn't make sense.
Since the server is company internal, I can't post a link.
Post Reply