Date Template Functions - Clipper

This is a discussion on Date Template Functions - Clipper ; Hi folks, I thought I might share some of my code so others can use and maybe even improve. A bit of background first. Several of my programs retrieve files from a web server. These files include the date in ...

+ Reply to Thread
Results 1 to 4 of 4

Date Template Functions

  1. Default Date Template Functions

    Hi folks,

    I thought I might share some of my code so others can use and maybe
    even improve. A bit of background first. Several of my programs
    retrieve files from a web server. These files include the date in the
    name in different formats. For some unknown reason the creators of the
    files would occassikonally change the date format which meant the
    program would not find it. Each program tracks the date date of the
    last file it processed and would pick up missing files automatically
    if a newer one did not appear first.

    To accomodate this, without having to rebuild program every time the
    wind changed direction <g>; I modified the program to store a file
    template as well as three functions to work with the template.

    The templates use the Excel format along with an ampersand. The
    FillTemplate function would be the easiest to see all the formats.
    Time values are not used as I did not requrie them but should be easy
    to add if needed.

    FillTemplate
    Takes a date value and a string template. The individual date elements
    are then inserted in to the template and the result is returned. If
    the date passed is empty the template is returned.

    GetDateFromName
    A filename with containing the date value to retrieve and the template
    to use to get the date. Returns the date retrieved from file name.

    MakeTemplateWild
    Makes the template in to a wild card search. This is used on the local
    machine when pikcing up missing files. When looking on the web server
    a loop from the last pocessed date to the current one is used,
    checking for each file.


    I hope these are of use to others. The code for these functions is
    below and I will upload it to support.cavo.com. Any feedback or
    improvements would be welcome.

    Kevin

    FUNCTION FillTemplate( dDate AS DATE, sTemplate AS STRING ) AS STRING
    //
    // Insert date elements into a template
    //
    // Arguements:
    // dDate Date to insert in to template
    // sTemplate Template to fill
    //
    // Returns:
    // sRet Template with date inserted
    //
    LOCAL sM AS STRING // Month as a single digit (e.g. 1 - 12)
    LOCAL sMm AS STRING // Two digit month (e.g. 01 - 12)
    LOCAL sMmm AS STRING // 3 chars for month (e.g. Jan - Dec )
    LOCAL sMmmm AS STRING // Full month name (e.g. January - December)
    LOCAL sMmmmm AS STRING // First letter of month
    LOCAL sD AS STRING // Single digit for day (e.g. 1 - 31)
    LOCAL sDd AS STRING // Two digit day (e.g. 01 - 31)
    LOCAL sDdd AS STRING // 3 chars for day (e.g. Sun - Sat)
    LOCAL sDddd AS STRING // Full day (e.g. Sunday - Saturday)
    LOCAL sYy AS STRING // Two digit year (e.g. 01 - 99)
    LOCAL sYyyy AS STRING // Full year (e.g. 1901 - 1999)
    LOCAL sRet AS STRING // Return value

    // Check we do not have an empty date
    IF (dDate == NULL_DATE)
    // Just return template
    sRet := sTemplate

    ELSE
    // Get parts for template
    sD := NTrim( Day(dDate))
    sDd := PadL( Day(dDate), 2, '0')
    sDddd := CDoW(dDate)
    sDdd := Left( sDddd, 3 )

    sM := NTrim(Month(dDate))
    sMm := PadL( Month(dDate), 2, '0')
    sMmmm := CMonth(dDate)
    sMmm := Left( sMmmm, 3)
    sMmmmm := Left( sMmmm, 1 )

    sYyyy := NTrim( Year(dDate) )
    sYy := Right(sYyyy, 2 )

    // Now fill the template
    sRet := sTemplate

    // Work with longest first - otherwise could upset parts
    sRet := StrTran( sRet, [&yyyy], sYyyy )
    sRet := StrTran( sRet, [&yy], sYy )

    sRet := StrTran( sRet, [&mmmmm], sMmmmm )
    sRet := StrTran( sRet, [&mmmm], sMmmm )
    sRet := StrTran( sRet, [&mmm], sMmm )
    sRet := StrTran( sRet, [&mm], sMm )
    sRet := StrTran( sRet, [&m], sM )

    sRet := StrTran( sRet, [&dddd], sDddd )
    sRet := StrTran( sRet, [&ddd], sDdd )
    sRet := StrTran( sRet, [&dd], sDd )
    sRet := StrTran( sRet, [&d], sD )

    ENDIF

    RETURN sRet
    FUNCTION GetDateFromName( sFileName AS STRING, sTemplate AS STRING) AS
    DATE
    //
    // Get date from a file name using the template
    // Arguments
    // sFileName Name of file to retrieve date from
    // sTemplate Template identifying wher date values are
    //
    // Returns
    // Date retrieved from sFileName
    //
    LOCAL aYear AS ARRAY
    LOCAL aMonth AS ARRAY
    LOCAL aDay AS ARRAY
    LOCAL dDate AS DATE
    LOCAL dwYearPos, dwYearL, dwYear AS DWORD
    LOCAL dwMonthPos, dwMonthL, dwMonth AS DWORD
    LOCAL dwDayPos, dwDayL, dwDay AS DWORD

    aYear := { [&yyyy], [&yy]}
    aMonth := { [&mmmmm], [&mmmm], [&mmm], [&mm], [&m] }
    aDay := {[&dddd], [&ddd], [&dd], [&d] }

    // Scan for date formats
    dwYearPos := AScan( aYear, {|x| Instr(x, sTemplate) })
    dwMonthPos := AScan( aMonth, {|x| Instr(x, sTemplate) })
    dwDayPos := AScan( aDay, {|x| Instr(x, sTemplate) })

    // Look for year in template
    dwYearL := SLen( aYear[ dwYearPos ] ) - 1
    dwYearPos := At( aYear[ dwYearPos ], sTemplate )

    // Look for month in template
    dwMonthL := SLen( aMonth[ dwMonthPos ] ) - 1
    dwMonthPos := At( aMonth[ dwMonthPos ], sTemplate )

    // Look for day in template
    dwDayL := SLen( aDay[ dwDayPos ] ) - 1
    dwDayPos := At( aDay[ dwDayPos ], sTemplate )

    // Adjust for '&' positions
    // Check if year is earliest
    IF (dwYearPos < dwMonthPos) .and. (dwYearPos < dwDayPos )
    IF (dwMonthPos < dwDayPos)
    dwMonthPos -= 1
    dwDayPos -= 2
    ELSE
    dwDayPos -= 1
    dwMonthPos -= 2
    ENDIF

    // check if month is earliest
    ELSEIF (dwMonthPos < dwYearPos) .and. (dwMonthPos < dwDayPos)
    IF (dwYearPos < dwDayPos)
    dwYearPos -= 1
    dwDayPos -= 2
    ELSE
    dwDayPos -= 1
    dwYearPos -= 2
    ENDIF

    // Day is earliest
    ELSE
    IF (dwYearPos < dwMonthPos)
    dwYearPos -= 1
    dwMonthPos -= 2
    ELSE
    dwMonthPos -= 1
    dwYearPos -= 2
    ENDIF
    ENDIF

    // Get parts of date
    dwYear := Val(SubStr( sFileName, dwYearPos, dwYearL ))
    dwMonth := Val(SubStr( sFileName, dwMonthPos, dwMonthL ))
    dwDay := Val(SubStr( sFileName, dwDayPos, dwDayL ))

    // Build date
    dDate := ConDate( dwYear, dwMonth, dwDay )

    RETURN dDate
    FUNCTION MakeTemplateWild( sTemplate AS STRING ) AS STRING
    //
    // Convert a template to wild card search
    //
    // Arguements:
    // sTemplate Template to fill
    //
    // Returns:
    // sRet Template with date inserted
    //
    LOCAL sRet AS STRING // Return value

    // Now fill the template
    sRet := sTemplate

    // Work with longest first - otherwise could upset parts
    sRet := StrTran( sRet, [&yyyy],[*] )
    sRet := StrTran( sRet, [&yy],[*] )

    sRet := StrTran( sRet, [&mmmmm],[*] )
    sRet := StrTran( sRet, [&mmmm],[*] )
    sRet := StrTran( sRet, [&mmm],[*] )
    sRet := StrTran( sRet, [&mm],[*] )
    sRet := StrTran( sRet, [&m],[*] )

    sRet := StrTran( sRet, [&dddd],[*] )
    sRet := StrTran( sRet, [&ddd],[*] )
    sRet := StrTran( sRet, [&dd],[*] )
    sRet := StrTran( sRet, [&d],[*] )

    sRet := StrTran( sRet, [***],[*] )
    sRet := StrTran( sRet, [**],[*] )

    RETURN sRet


  2. Default Re: Date Template Functions

    murphy (I guess it's your name)

    Thanks for sharing

    --

    Carlos Rocha

  3. Default Re: Date Template Functions

    Kevin actually <g>

    "carlos.delete.this@net-disk.com" <carlos.delete.this@net-disk.com>
    wrote in message news:YeudnWROX4huqbXbnZ2dnUVZ8vqdnZ2d@novis.pt:

    > murphy (I guess it's your name)
    >
    > Thanks for sharing
    >
    > --
    >
    > Carlos Rocha



  4. Default Re: Date Template Functions

    Carlos,

    You are welcome. I have got lots of answers to questions here. So I am
    happy to be able to give something back that might be of use.

    Kevin


+ Reply to Thread

Similar Threads

  1. Overload resolution with template and non-template functions
    By Application Development in forum c++
    Replies: 4
    Last Post: 07-26-2007, 09:59 AM
  2. Are there any template trigonometry functions?
    By Application Development in forum c++
    Replies: 2
    Last Post: 07-07-2007, 10:17 AM
  3. template functions intricacies
    By Application Development in forum c++
    Replies: 2
    Last Post: 07-03-2007, 09:02 AM
  4. Vectorization of template functions
    By Application Development in forum c++
    Replies: 2
    Last Post: 06-22-2007, 12:10 PM
  5. Template functions questions
    By Application Development in forum c++
    Replies: 6
    Last Post: 06-19-2007, 08:08 AM