Announcement

Collapse
No announcement yet.

4D-CD-00041 Designer or ViSi Routine for Parsing a Date String

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 4D-CD-00041 Designer or ViSi Routine for Parsing a Date String




    This codebase example shows one way of parsing a date string to find the month, date, and year information and convert them to their equivalent integer values. To accomplish this, the 4DGL string class functions are used. The program expects the input date string to be of the format "month dd, yyyy". If you wish to use a different format, you must modify the source code.

    Example input date string: "July 2, 2015"

    The program will write the results to three global variables:

    globalMonth = 7, globalDay = 2, globalYear = 2015

    Example 2:
    Input date string: "September 21, 1923"
    globalMonth = 9, globalDay = 21, globalYear = 1923

    Example 3:
    Input date string: "Sep 99, 2015"
    globalMonth = 9, globalDay = 99, globalYear = 2015

    Example 4:
    Input date string: "Septe 21, 9999"
    globalMonth = 9, globalDay = 21, globalYear = 9999

    If the program does not understand the input date string, it prints out an error message. In summary, the program does the following:

    1. Find the locations of the first space and comma characters. The program prints out an error message if no space or comma character is found.

    2. Use the data in step 1 to locate the month, day, and year substrings and copy them to separate arrays.

    3. Compare the month substring to a list of stored strings of correct and expected month name strings. Note that the month substring must be at least three characters long and that the matching process is case insensitive. If the program finds a match for the month substring, it stores the corresponding month number to the global variable "globalMonth". Otherwise it prints the message "no month extracted.".

    4. Extract any decimal number in the day substring and convert it to its equivalent integer value, which is then stored in the global variable "globalDay". The day substring must be one- or two-characters wide. If the program is not able to find a decimal number in the day substring, it prints the message "No day extracted.".

    5. Extract any decimal number in the year substring and convert it to its equivalent integer value, which is then stored in the global variable "globalYear". The year substring must be between one- to four-characters wide. If the program is not able to find a decimal number in the year substring, it prints the message "No year extracted.".


    Note that the program only parses a given string to find the month, day, and year information; it does not check if the date is correct. For example, "Sep 99, 9999" and "February 29, 2015" are valid inputs since they contain month, day, and year information. Further date validity test can be implemented by the user by using the code snippet in 4D-CD-00023 Designer or ViSi Time and Date Validation.

    To see an actual example of a more complete date string check, see the codebase example 4D-CD-00043 Designer or ViSi Parse and Check a Date String. Designer or ViSi Parse and Check a Date String combines this example (4D-CD-00041 Designer or ViSi Routine for Parsing a Date String) and 4D-CD-00023 Designer or ViSi Time and Date Validation to perform both parsing and checking the validity of a date string. Furthermore, Designer or ViSi Parse and Check a Date String shows how the source code in this example(4D-CD-00041 Designer or ViSi Routine for Parsing a Date String) is converted to an include file that can be used in a larger project.


    At the start of the code is a list of strings for the month names. The program will compare the input month substring against this list. The English month names can be replaced with foreign month names, as long as the characters are in ASCII. For instance, "DECEMBER\0" can be changed to "DISYEMBRE\0".

    All variables and arrays used in the program are located inside the array "PDClass[26]".

    var PDClass[26];


    This array contains 26 words, some of which are used for storing strings, and some are used as variables. A list of constants is then used for indexing into the array. The list is partially shown below.

    #CONST
    PDssMonth // 5 words to store month substring
    PDssDay 5 // 2 words to store day substring
    PDssYear 7 // 3 words to store year substring
    PDptrSsMonth 10 // 1 word to be used as a byte-aligned pointer to month substring
    PDptrSsDay // 1 word to be used as a byte-aligned pointer to day substring
    PDptrSsYear
    ...
    PDcounter // 1 word to be used as a multi-purpose counter variable
    PDflag // 1 word to be used as a multi-purpose flag variable
    #END

    The constants have the implicit values shown below:

    #CONST
    PDssMonth 0 // implicit value
    PDssDay 5 // explicit value
    PDssYear 7 // explicit value
    PDptrSsMonth 10 // explicit value
    PDptrSsDay 11 // implicit value
    PDptrSsYear 12 // implicit value
    ...
    PDcounter 24 // implicit value
    PDflag 25 // implicit value
    #END

    Thus, we can use the 25th element of the array as a general-purpose counter variable:

    PDClass[PDcounter] := 0;
    for(; PDClass[PDcounter] < 12; PDClass[PDcounter]++)
    ...
    next

    Also, we can use the seventh element of the array as a pointer to another variable:

    PDClass[PDptrGMonth] := &globalMonth;

    To access the content of the global variable "globalMonth", we can dereference its pointer:

    var x := *PDClass[PDptrGMonth];

    To assign a value to the global variable "globalMonth" thru its pointer we write,

    *PDClass[PDptrGMonth] := 12;


    Also, we can input a sequence of bytes (e.g. the input month name substring) starting at the address of PDClass[PDssMonth]:

    to(&PDClass[PDssMonth]); print("July");

    We arbitrarily limit the length of the input byte sequence to 10 including the null terminator, so it will not overlap with the bytes located starting at &PDClass[PDssDay].

    We can use an element of the big array "PDClass[26]" as a byte-aligned pointer to another section of the array:

    PDClass[PDptrSsMonth] := str_Ptr(&PDClass[PDssMonth]);


    Putting all variables and "sub-arrays" inside a large array and using constants in an organized list as indices into the array allow for the writing of the definitions (as comments) of all the variables and arrays in one place in the source code.



    Instructions:

    1. In the attached Designer code, modify the input date string on line 75. Compile the project and upload it to a uLCD-32PTU (or your target display).

    2. The program should now run on the display module. See the video for more information.
    Attached Files
    Doff
Working...
X