date_Construct8601 (was numberformat)

Sarah Reichelt sarah.reichelt at gmail.com
Tue Aug 26 20:16:34 EDT 2008


Hi David,

Here is my re-working of your function:

function date_Construct8601 someDate, someTime, extended, isUTC
    if someTime is empty then
        -- returning date only
        convert someDate to dateItems
        put item 1 of someDate into y
        put item 2 of someDate into mons
        put item 3 of someDate into d

        convert someDate to internet date
        if extended is true then
            -- 1981-04-05
            put "%4d-%02d-%02d" into baseString
        else
            -- 19810405
            put "%4d%02d%02d" into baseString
        end if

        put format(baseString, y, mons, d) into fDate
        return fDate

    else
        -- returning date & time as either local or UTC
        convert (someDate && someTime) to internet date
        put it into someDate

        put word -1 of someDate into utcOffset

        convert someDate to dateItems
        put item 1 of someDate into y
        put item 2 of someDate into mons
        put item 3 of someDate into d

        put item 4 of someDate into h
        put item 5 of someDate into mins
        put item 6 of someDate into s

        if isUTC is true then
            -- UTC specific code
            put char 2 to 3 of utcOffset into extraHours
            put char 4 to 5 of utcOffset into extraMin

            if char 1 of utcOffset is "-" then
                add extraHours to h
                add extraMin to m
            else
                subtract extraHours from h
                subtract extraMin from m
            end if

            if extended is true then
                -- 1981-04-05T14:30:30-05:00
                put "%4d-%02d-%02dT%02d:%02d:%02dZ" into baseString
            else
                -- 19810405T14:30:30-05 00
                put "%4d%02d%02dT%02d:%02d:%02dZ" into baseString
            end if
            put format(baseString, y, mons, d, h, mins, s) into fDate
            return fDate

        else
            -- Local specific code
            if extended is true then
                -- 1981-04-05T14:30:30-05:00
                put "%4d-%02d-%02dT%02d:%02d:%02d %04s" into baseString
            else
                -- 19810405T14:30:30-05 00
                put "%4d%02d%02dT%02d:%02d:%02d%04s" into baseString
            end if

            put format(baseString, y, mons, d, h, mins, s, utcOffset) into fDate
            return fDate
        end if
    end if
end date_Construct8601


- instead of using your padding function and numberFormat, I just used
format to supply the leading zeroes as required.
- your UTC calculation was going the wrong way. I am in +1000 time
zone, and to get to UTC, I have to subtract 10 from the local hours,
not add.
- there was some code duplication between the UTC and local date/time
sections, so I re-organised the structure of the if-else-end if"s to
avoid this.

Thanks for providing an interesting challenge :-)

Cheers,
Sarah



More information about the use-livecode mailing list