Show the epoch: Difference between revisions

Added uBasic/4tH version
(Added a Scheme implementation.)
(Added uBasic/4tH version)
Line 1,076:
</pre>
 
=={{header|uBasic/4tH}}==
uBasic/4tH provides a '''TIME()''' function, which returns the common epoch, but doesn't provide a builtin function to display it - other than it's numerical value. This program shows the epoch in high level code.
<lang>Print Show(FUNC(_DateStr(0))), Show(FUNC(_TimeStr(0)))
End
 
_DateStr ' convert epoch to date string
Param (1)
Local (6)
 
a@ = a@ / 86400 ' just get the number of days since epoch
b@ = 1970+(a@/365) ' ball parking year, will not be accurate!
 
d@ = 0
For c@ = 1972 To b@ - 1 Step 4
If (((c@%4) = 0) * ((c@%100) # 0)) + ((c@%400) = 0) Then d@ = d@+1
Next
 
b@ = 1970+((a@ - d@)/365) ' calculating accurate current year by (x - extra leap days)
e@ = ((a@ - d@)%365)+1 ' if current year is leap, set indicator to 1
f@ = (((b@%4) = 0) * ((b@%100) # 0)) + ((b@%400) = 0)
 
g@ = 0 ' calculating current month
For c@ = 0 To 11 Until e@ < (g@+1)
g@ = g@ + FUNC(_Monthdays (c@, f@))
Next
' calculating current date
g@ = g@ - FUNC(_Monthdays (c@-1, f@))
' Print a@, d@, e@, f@
Return (Join (Str(b@), FUNC(_Format (c@, Dup("-"))), FUNC(_Format (e@ - g@, Dup("-")))))
 
_TimeStr ' convert epoch to time string
Param (1)
Return (Join(Str((a@%86400)/3600), FUNC(_Format ((a@%3600)/60, Dup(":"))), FUNC(_Format (a@%60, Dup(":")))))
 
_Format Param (2) : Return (Join (Iif (a@<10, Join(b@, "0"), b@), Str (a@)))
_Monthdays Param (2) : Return (((a@ + (a@<7)) % 2) + 30 - ((2 - b@) * (a@=1)))</lang>
{{Out}}
<pre>
1970-01-01 0:00:00
 
0 OK, 0:58
</pre>
=={{header|UNIX Shell}}==
The nonstandard option <code>date -r</code> takes seconds from the epoch, and prints date and time. See [http://www.openbsd.org/cgi-bin/man.cgi?query=date&apropos=0&sektion=1&manpath=OpenBSD+Current&arch=i386&format=html date(1) manual].
374

edits