> > > I've just ran into a brick wall.
> > >
> > > Where is the epoc time support in Rebol?
> > >
> >
> > It's easy enough to do, Deryk... Here's to epoch time:
>
> Ralph,
>
> Danke, you're the man for the day! :)
>
> (I still think it should be a normal function in rebol ;))
>

Sie sind willkommen, Deryk. Ich arbeite schwer, um gute REBOL Spitzen
bereitzustellen.

Now, you say you wish epoch time was a function in REBOL? No problem. Here's
how you can achieve it (this technique, by the way, is in my forthcoming
book, REBOL/CORE FOR DUMMIES).

1) create a REBOL script file in your REBOL directory called %myuser.r and
add the line 'do %myuser.r' in the 'rebol.r' file. You now have a home for
all sorts of handy enhancements to REBOL/Core which get activated every time
REBOL starts.

2) Turn the to epoch script into a function, as so:

epoch: func ["Returns epoch date of now"][

        date: now

        seconds: ((date - 1-1-1970) * 86400) + (date/time/hour * 3600) +
(date/time/minute * 60) + date/time/second

        zone: now/zone

        zone: zone/hour

        zone: zone * 3600

        seconds: seconds - zone ; minus a minus gives plus

        ]

3) Paste it into your %myuser.r file. Restart REBOL. Now you can use it as
any function function, for example:

>> help epoch
USAGE:
    EPOCH

DESCRIPTION:
     Returns epoch date of now
     EPOCH is a function value.
>> print epoch
960993613
>> print epoch + 100000
961093622

4) Okay, now the from epoch conversion function... put it in your %myuser.r
file also:

epoch-to-date: func [{Convert from epoch to date,
                example: epoch-to-date 960994210 returns
                Epoch date 960994210 is 14-Jun-2000 14:50:10 GMT
                or 10:50:10 Local } epoch][

        days: divide epoch 86400

        days2: make integer! days

        time: (days - days2) * 24
        hours: make integer! time
        minutes: (time - hours) * 60
        minutes2: make integer! minutes
        seconds: make integer! (minutes - minutes2) * 60
        time2: make time! ((((hours * 60) + minutes2) * 60) + seconds)
        prin ["Epoch date" epoch "is" 1-Jan-1970 + days2 time2]
        print [" GMT or" time2 + now/zone "Local"]
        ]

5) Reload %myuser.r. Now you can also use the function epoch-to-date. Do it
like this:

>> help epoch-to-date
USAGE:
    EPOCH-TO-DATE epoch

DESCRIPTION:
     Convert from epoch to date,
        example: epoch-to-date 960994210 returns
        Epoch date 960994210 is 14-Jun-2000 14:50:10 GMT
        or 10:50:10 Local
     EPOCH-TO-DATE is a function value.

ARGUMENTS:
     epoch -- (Type: any)
>> epoch
== 960994724
>> epoch-to-date 960994724
Epoch date 960994724 is 14-Jun-2000 14:58:43 GMT or 10:58:43 Local
>> epoch-to-date (epoch + 100000)
Epoch date 961094764 is 15-Jun-2000 18:46:04 GMT or 14:46:04 Local
>> epoch-to-date (divide epoch 100)
Epoch date 9609947.89 is 22-Apr-1970 5:25:47 GMT or 1:25:47 Local

6) Enjoy and support the /Core Corps, shock troops of the great REBOLution.

--Ralph Roberts


Reply via email to