Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

date-to-epoch conversion

date-to-epoch: func [
    "Returns a unix time (epoch) format from a date."
    date [date!]
    /local res
][
    ; If no time is given, negate our zone to give us 0:00 UTC
	if none? date/time [date/time: negate now/zone]
	; This uses the epoch base in UTC, so we assume that either
	; the date is also in UTC, or has a zone offset included.
	; DIFFERENCE fails for huge time differences, so we subtract
	; them instead, giving us a difference in days, and multiply
	; by the number of seconds in a day.
	either res: attempt [to integer! difference date 1-Jan-1970/0:0:0] 
		[res] 
		[date - 1-Jan-1970/0:0:0 * 86400.0] 
]

epoch-to-date conversion

epoch-to-date: func [
    "Returns a date from unix time (epoch) format."
    value [integer!] "Number of seconds since 1-Jan-1970"
    /local date time
][
    value: to time! value
    date: 1-Jan-1970 + round/down value / 24:0:0
    time: value // 24:0:0
    add now/zone to date! rejoin [date "/" time now/zone]
]
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS