iNTERFACEWARE Products Manual > Installing and Using Chameleon > Using Python Scripting > Python Scripting Examples > Date and Time Manipulation > Validating a Date and Time |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
In Iguana or Chameleon, dates and times are normally in one of the formats shown below:
In these date-time formats:
In addition, any of these time formats can have a time zone correction appended. This consists of + or - and a four-digit number representing the hours and minutes to add or subtract from the time. (In this manual page, this time zone correction is represented as +ZZZZ or -ZZZZ.) Occasionally, you may need to check whether a date and time are complete and valid. For example:
The following Python function, parse_time(), validates dates and times provided in any of the date-time formats shown above. It calls the days_in_month() function to obtain the number of days in the specified month. days_in_month() is defined in the calendar.py module, which is included with Iguana and Chameleon.
parse_time() expects one parameter, which is the timestamp that needs to be validated. If it detects an invalid timestamp component, it raises a ValueError exception; otherwise, it returns the components of the timestamp. Here is some sample code that calls parse_time():
This function returns the components of a valid timestamp, which can also be interpreted as true. It returns None (false) if the timestamp is invalid.
|