Example

Unix Date Format Converter

Author: Josť Antonio Meira da Rocha
Return to REBOL Cookbook

This example provides functions to convert between REBOL date format and Unix date format (integer seconds since 1970):


    epoch-to-date: func [
        "Return REBOL date from unix time format"
        epoch [integer!] "Date in unix time format"
    ][
        day: 1-Jan-1970 + (to-integer epoch / 86400) 
        hours:   to-integer epoch // 86400 / 3600
        minutes: to-integer epoch // 86400 // 3600 / 60
        seconds: to-integer epoch // 86400 // 3600 // 60
        return (to-date rejoin [
            day "/" hours ":" minutes ":" seconds now/zone
        ]) + now/zone
    ]

    date-to-epoch: func [
        "Return date in unix time format from a date in REBOL format"
        rebol-date [date!] "Date in REBOL time format"
    ][
        if rebol-date/time = none [ 
            rebol-date: to-date rejoin [rebol-date "/00:00:00"] 
        ]
        rebol-date: rebol-date - now/zone
        return to-integer (rebol-date - 1-Jan-1970 * 86400) + 
        (rebol-date/time/hour * 3600) + 
        (rebol-date/time/minute * 60) + rebol-date/time/second
    ]

    print epoch-to-date date-to-epoch now

2006 REBOL Technologies REBOL.com REBOL.net