Dates, Numbers, and Strings


Dates, Numbers , and Strings

OOo Basic recognizes dates in two different string formats. The obvious format is set by the locale. A less obvious format is the ISO 8601 date format. String formats are always assumed to be in a locale-specific format except for routines specific to the ISO 8601 format. Locale-specific formats and the ISO 8601 format are discussed at length later. Arguments passed to the date and time functions are converted to an appropriate type if possible. As a result, most of the functions in Table 3 accept string, numeric, and date arguments.

Table 3: Date and string conversion functions

Function

Description

CDate

Convert a number or string to a date.

DateValue

The OOo help incorrectly states that DateValue converts to a Long and incorrectly places restrictions on the range of supported dates. Convert a formatted string from December 1, 1582 through December 31, 9999 to a Date value that contains no time.

CDateFromIso

Convert to a date from an ISO 8601 date representation.

CDateToIso

Convert a date to an ISO 8601 date representation.

IsDate

Is this string a properly formatted date?

Use the IsDate function to test if a string contains a valid date. The argument is always converted to a string before it is used, so a numeric argument will return False. The IsDate function tests more than just syntax-it checks to see if the string contains a valid date. For example, "02/29/2003" fails because February 2003 contains only 28 days. The same validity check is not performed on the time component of the string (see Listing 1 and Listing 2 ).

Listing 1: IsDate verifies that a string contains a valid date.
start example
 Print IsDate("December 1, 1582 2:13:42") 'True Print IsDate("2:13:42")                  'True Print IsDate("12/1/1582")                'True Print IsDate(Now)                        'True Print IsDate("26:61:112")                'True: 112 seconds and 61 minutes!!! Print IsDate(True)                       'False: Converts to string first Print IsDate(32686.22332)                'False: Converts to string first Print IsDate("02/29/2003")               'False: Only 28 days in February 03 
end example
 
Listing 2: ExampleTimeConversions is found in the Date module in this chapter's source code files as SC05.sxw.
start example
 Sub ExampleTimeConversions   Dim Dates()   Dim i As Integer   Dim s As String   Dates() = Array("1/1/1 00:00:00  ", "1/1/1 22:40:00 ", "1/1/1 30:40:00  ",_                   "1/1/1 30:100:00 ", "1/1/1 30:100:100")   For i = LBound(Dates()) To UBound(Dates())     s = s & CStr(i) & " " & Dates(i) & " => " & CDate(Dates(i)) & CHR$(10)   Next   MsgBox s, 0, "Strange Time Values" End Sub 
end example
 

The apparent inconsistency with the IsDate function is that "02/29/2003" is an invalid date but "26:61:112" is valid. With time values, if a section of the time is too large, it is simply added to the next section. For example, 61 minutes is one hour and one minute. Again, 112 seconds adds one minute and 52 seconds to the final computed time value. This is demonstrated in Listing 2 and shown in Figure 1 . Notice that, in line two, 30 hours becomes six hours and the day is incremented by one. Negative numbers, however, cause a run-time error.

click to expand
Figure 1: What appear to be invalid times are valid.



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net