DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Date-to-epoch Conversion

03.07.2006
| 6002 views |
  • submit to reddit
        
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] 
]