Show the epoch
You are encouraged to solve this task according to the task description, using any language you may know.
See also: Date format
[edit] Ada
In Ada, time is a private type and is implementation defined, for instance, on 64 bit GNAT, time is represented internally as nanoseconds relative to Jan 1, 2150.
However, conversion from unix epoch seconds is also supported and shown below.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Calendar.Conversions; use Ada.Calendar.Conversions;
procedure ShowEpoch is
etime : Time := To_Ada_Time (0);
begin
Put_Line (Image (Date => etime));
end ShowEpoch;
- Output:
1970-01-01 00:00:00
[edit] AWK
# syntax: GAWK -f SHOW_THE_EPOCH.AWK
# requires GNU Awk 4.0.1 or later
BEGIN {
print(strftime("%Y-%m-%d %H:%M:%S",0,1))
exit(0)
}
output:
1970-01-01 00:00:00
[edit] BBC BASIC
INSTALL @lib$+"DATELIB"
PRINT FN_date$(0, "dd-MMM-yyyy")
Output:
17-Nov-1858
[edit] C
#include <time.h>
#include <stdio.h>
int main() {
time_t t = 0;
printf("%s", asctime(gmtime(&t)));
return 0;
}
- Output:
Thu Jan 1 00:00:00 1970
[edit] Windows
FileTime, from the Win32 API, uses a different epoch.
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
int
main()
{
FILETIME ft = {dwLowDateTime: 0, dwHighDateTime: 0}; /* Epoch */
SYSTEMTIME st;
wchar_t date[80], time[80];
/*
* Convert FILETIME (which counts 100-nanosecond intervals since
* the epoch) to SYSTEMTIME (which has year, month, and so on).
*
* The time is in UTC, because we never call
* SystemTimeToTzSpecificLocalTime() to convert it to local time.
*/
FileTimeToSystemTime(&ft, &st);
/*
* Format SYSTEMTIME as a string.
*/
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
time, sizeof time / sizeof time[0]) == 0) {
fwprintf(stderr, L"Error!\n");
return 1;
}
wprintf(L"FileTime epoch is %ls, at %ls (UTC).\n", date, time);
return 0;
}
- Output:
FileTime epoch is Monday, January 01, 1601, at 12:00:00 AM (UTC).
[edit] C#
using System;
class Program
{
static void Main()
{
Console.WriteLine(new DateTime());
}
}
- Output:
1-1-0001 0:00:00
[edit] C++
Doesn't work with MSVC 10 SP1
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
std::chrono::system_clock::time_point epoch;
std::time_t t = std::chrono::system_clock::to_time_t(epoch);
std::cout << std::asctime(std::gmtime(&t)) << '\n';
return 0;
}
- Output:
Thu Jan 1 00:00:00 1970
#include <iostream>
#include <boost/date_time.hpp>
int main()
{
std::cout << boost::posix_time::ptime( boost::posix_time::min_date_time ) << '\n';
return 0;
}
- Output:
1400-Jan-01 00:00:00
[edit] Common Lisp
(multiple-value-bind (second minute hour day month year) (decode-universal-time 0 0)
(format t "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D" year month day hour minute second))
- Output:
1900-01-01 00:00:00
[edit] D
The Date struct of the standard library module "std.datetime" represents a date in the Proleptic Gregorian Calendar ranging from 32,768 B.C. to 32,767 A.D.
[edit] Dart
main() {
print(new Date.fromEpoch(0,new TimeZone.utc()));
}
- Output:
1970-01-01 00:00:00.000Z
[edit] Delphi
program ShowEpoch;
{$APPTYPE CONSOLE}
uses SysUtils;
begin
Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', 0));
end.
- Output:
1899-12-30 00:00:00.000
[edit] Factor
IN: USE: calendar calendar.format
IN: 0 micros>timestamp timestamp>ymdhms .
"1970-01-01 00:00:00"
[edit] Forth
include lib/longjday.4th
0 posix>jday .longjday cr
- Output:
Thursday, January 1, 1970
[edit] Go
package main
import ("fmt"; "time")
func main() {
fmt.Println(time.Time{})
}
- Output:
This is UNIX format. The 1 on the end is the full year, not two or four digit year.
Mon Jan 1 00:00:00 +0000 UTC 1
[edit] Groovy
Groovy uses the UNIX epoch.
def date = new Date(0)
def format = new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ')
format.timeZone = TimeZone.getTimeZone('UTC')
println (format.format(date))
- Output:
1970-01-01T00:00:00.000+0000
[edit] Haskell
[edit] Old time library
The ClockTime type is abstract in Haskell 98, but is defined in GHC.
import System.Time
main = putStrLn $ calendarTimeToString $ toUTCTime $ TOD 0 0
- Output:
Thu Jan 1 00:00:00 UTC 1970
[edit] New time library
import Data.Time
main = print $ UTCTime (ModifiedJulianDay 0) 0
- Output:
1858-11-17 00:00:00 UTC
[edit] Icon and Unicon
Date and Time can be accessed via a number of keywords and functions
- The following are available in both Icon and Unicon
- &clock, &date, &dateline, and &time deal with current times and dates
- The following are specific to Unicon
- &now provides the number of seconds since the epoch, Jan 1, 1970 00:00:00
- ctime(integer) takes the number of seconds since the epoch and returns the date and time as a string in the local timezone
- gtime(integer) takes the number of seconds since the epoch and returns the date and time as a string in UTC
- gettimeofday() returns a record with the current time since the epoch in seconds and microseconds
- datetime routines use a global variable 'DateBaseYear' which defaults to Jan 1, 1970 00:00:00 but can be set if desired.
- The example below uses only a couple of the datetime procedures
link printf,datetime
procedure main()
# Unicon
now := gettimeofday().sec
if now = &now then printf("&now and gettimeofday().sec are equal\n")
printf("Now (UTC) %s, (local) %s\n",gtime(now),ctime(now))
printf("Epoch %s\n",gtime(0))
# Icon and Unicon
now := DateToSec(&date) + ClockToSec(&clock)
printf("Now is also %s and %s\n",SecToDate(now),SecToDateLine(now))
end
- Sample Output:
&now and gettimeofday().sec are equal Now (UTC) Tue Aug 09 10:43:23 2011, (local) Tue Aug 09 06:43:23 2011 Epoch Thu Jan 01 00:00:00 1970 Now is also 2011/08/09 and Tuesday, August 9, 2011 6:43 am
[edit] J
J does not have an epoch. J's native representation of date and time is a six element list: year, month, day, hour, minute, second. For example:
6!:0''
2011 8 8 20 25 44.725
(August 8, 2011, 8:25:44 pm)
That said, the 'dates' library does have an epoch:
require'dates'
todate 0
1800 1 1
[edit] Java
DateFormat is needed to set the timezone. Printing date alone would show this date in the timezone/locale of the machine that the program is running on. The epoch used in java.util.Date (as well as java.sql.Date, which can be subbed into this example) is actually in GMT, but there isn't a significant difference between that and UTC for lots of applications (documentation for java.util.Date).
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateTest{
public static void main(String[] args) {
Date date = new Date(0);
DateFormat format = DateFormat.getDateTimeInstance();
format.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(format.format(date));
}
}
- Output:
Jan 1, 1970 12:00:00 AM
On my PC I see
01.01.1970 00:00:00
[edit] JavaScript
document.write(new Date(0).toUTCString());
- Output:
Thu, 01 Jan 1970 00:00:00 GMT
[edit] Mathematica
DateString[0]
->Mon 1 Jan 1900 00:00:00
[edit] MATLAB / Octave
Matlab and Octave store date/time number in a floating point number counting the days.
d = [0,1,2,3.5,-3.5,1000*365,1000*366,now+[-1,0,1]];
for k=1:length(d)
printf('day %f\t%s\n',d(k),datestr(d(k),0))
disp(datevec(d(k)))
end;
- Output:
day 0.000000 31-Dec--001 00:00:00
-1 12 31 0 0 0
day 1.000000 01-Jan-0000 00:00:00
0 1 1 0 0 0
day 2.000000 02-Jan-0000 00:00:00
0 1 2 0 0 0
day 3.500000 03-Jan-0000 12:00:00
0 1 3 12 0 0
day -3.500000 27-Dec--001 12:00:00
-1 12 27 12 0 0
day 365000.000000 02-May-0999 00:00:00
999 5 2 0 0 0
day 366000.000000 27-Jan-1002 00:00:00
1002 1 27 0 0 0
day 734908.972013 09-Feb-2012 23:19:41
2012.0000 2.0000 9.0000 23.0000 19.0000 41.9633
day 734909.972013 10-Feb-2012 23:19:41
2012.0000 2.0000 10.0000 23.0000 19.0000 41.9633
day 734910.972013 11-Feb-2012 23:19:41
2012.0000 2.0000 11.0000 23.0000 19.0000 41.9633
[edit] Maxima
timedate(0);
"1900-01-01 10:00:00+10:00"
[edit] NetRexx
/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.text.DateFormat
edate = Date(0)
zulu = DateFormat.getDateTimeInstance()
zulu.setTimeZone(TimeZone.getTimeZone('UTC'))
say zulu.format(edate)
return
Output:
Jan 1, 1970 12:00:00 AM
[edit] NewLISP
(date 0)
->"Thu Jan 01 01:00:00 1970"
[edit] Objective-C
#import <Foundation/Foundation.h>
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *t = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZ"];
NSLog(@"%@", [dateFormatter stringFromDate:t]);
[pool release];
return 0;
}
- Log:
2001-01-01 00:00:00 +0000
[edit] OCaml
open Unix
let months = [| "January"; "February"; "March"; "April"; "May"; "June";
"July"; "August"; "September"; "October"; "November"; "December" |]
let () =
let t = Unix.gmtime 0.0 in
Printf.printf "%s %d, %d\n" months.(t.tm_mon) t.tm_mday (1900 + t.tm_year)
- Execution:
$ ocaml unix.cma epoch.ml January 1, 1970
[edit] Pascal
This works with Free Pascal:
Program ShowEpoch;
uses
SysUtils;
begin
Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now));
Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', 0));
end.
- Output:
:> ./SelfDescribingNumber 2011-12-13 00:57:41.378 1899-12-30 00:00:00.000
[edit] Perl
print scalar gmtime 0, "\n";
- Output:
Thu Jan 1 00:00:00 1970
[edit] Perl 6
say DateTime.new(0)
- Output:
1970-01-01T00:00:00Z
[edit] PHP
<?php
echo gmdate('r', 0), "\n";
?>
- Output:
Thu, 01 Jan 1970 00:00:00 +0000
[edit] PicoLisp
The 'date' function in PicoLisp returns a day number, starting first of March of the year zero. Calculated according to the gregorian calendar (despite that that calendar wasn't used in 0 AD yet).
: (date 1)
-> (0 3 1) # Year zero, March 1st
[edit] PL/I
show_epoch: procedure options (main); /* 14 Sept. 2012 */
put (datetime('DDMmmYYYY'));
put (time());
end show_epoch;
Result:
14Sep2012 233312540
[edit] PowerShell
PowerShell uses .NET's DateTime structure and an integer can simply be casted appropriately:
[datetime] 0
- Output:
Monday, January 01, 0001 12:00:00 AM
[edit] PureBasic
If OpenConsole()
PrintN(FormatDate("Y = %yyyy M = %mm D = %dd, %hh:%ii:%ss", 0))
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
- Output:
Y = 1970 M = 01 D = 01, 00:00:00
[edit] Python
>>> import time
>>> time.asctime(time.gmtime(0))
'Thu Jan 1 00:00:00 1970'
>>>
[edit] R
> epoch <- 0
> class(epoch) <- class(Sys.time())
> format(epoch, "%Y-%m-%d %H:%M:%S %Z")
[1] "1970-01-01 00:00:00 UTC"
[edit] Racket
#lang racket
(require racket/date)
(date->string (seconds->date 0 #f))
Output:
"Thursday, January 1st, 1970"
[edit] REXX
The epoch for the REXX language built-in function DATE is January 1st, year 1.
/*REXX program shows the # of days since the epoch for the DATE function*/
say ' today is' date() /*today's is format: mm MON YYYY */
days=date('Basedate') /*only 1st char of option is used*/
say right(days,35) "days since the REXX base date of January 1st, year 1"
say 'and today is:' date(,days,'B') /*this should be today (still). */
/*──────── The above statement is only valid for the newer REXXes,*/
/*──────── older versions don't support the 2nd and 3rd arguments.*/
output
today is 3 Aug 2012
734717 days since the REXX base date of January 1st, year 1
and today is: 3 Aug 2012
[edit] Ruby
irb(main):001:0> Time.at(0).utc
=> 1970-01-01 00:00:00 UTC
[edit] Run BASIC
eDate$ = date$("01/01/0001")
cDate$ = date$(0) ' 01/01/1901
sDate$ = date$("01/01/1970")
[edit] Scala
import java.util.{Date, TimeZone, Locale}
import java.text.DateFormat
val df=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ENGLISH)
df.setTimeZone(TimeZone.getTimeZone("UTC"))
println(df.format(new Date(0)))
- Output:
January 1, 1970 12:00:00 AM UTC
[edit] Seed7
The Seed7 library time.s7i defines the type time, which describes times and dates. For dates the proleptic Gregorian calendar is used (which assumes that the Gregorian calendar was even in effect at dates preceding its official introduction). This convention is used according to ISO 8601, which also defines that positive and negative years exist and that the year preceding 1 is 0. Therefore the epoch is the beginning of the year 0.
$ include "seed7_05.s7i";
include "time.s7i";
const proc: main is func
begin
writeln(time.value);
end func;
- Output:
0000-01-01 00:00:00 UTC
[edit] Standard ML
- Date.toString (Date.fromTimeUniv Time.zeroTime);
val it = "Thu Jan 1 00:00:00 1970" : string
[edit] Tcl
% clock format 0 -gmt 1
Thu Jan 01 00:00:00 GMT 1970
[edit] TUSCRIPT
$$ MODE TUSCRIPT
- epoch
number=1
dayofweeknr=DATE (date,day,month,year,number)
epoch=JOIN(year,"-",month,day)
PRINT "epoch: ", epoch," (daynumber ",number,")"
- today's daynumber
dayofweeknr=DATE (today,day,month,year,number)
date=JOIN (year,"-",month,day)
PRINT "today's date: ", date," (daynumber ", number,")"
- Output:
epoch: 1-1-1 (daynumber 1) today's date: 2011-12-14 (daynumber 734487)
[edit] UNIX Shell
The nonstandard option date -r takes seconds from the epoch, and prints date and time. See date(1) manual.
$ date -ur 0
Thu Jan 1 00:00:00 UTC 1970
On systems with GNU date, you can do
$ TZ=UTC date --date "$(date +%s) seconds ago"
Thu Jan 1 00:00:00 UTC 1970
[edit] Visual Basic
Sub Main()
Debug.Print Format(0, "dd mmm yyyy hh:mm")
End Sub
- Output (in debug window):
30 Dec 1899 00:00
- Programming Tasks
- Solutions by Programming Task
- Ada
- AWK
- BBC BASIC
- C
- Win32
- C sharp
- C++
- Boost
- Common Lisp
- D
- Dart
- Delphi
- Factor
- Forth
- Go
- Groovy
- Haskell
- Icon
- Unicon
- Icon Programming Library
- J
- Java
- JavaScript
- Mathematica
- MATLAB
- Octave
- Maxima
- NetRexx
- NewLISP
- Objective-C
- OCaml
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- PowerShell
- PureBasic
- Python
- R
- Racket
- REXX
- Ruby
- Run BASIC
- Scala
- Seed7
- Standard ML
- Tcl
- TUSCRIPT
- UNIX Shell
- Visual Basic
- AutoHotkey/Omit
- GUISS/Omit
- Locomotive Basic/Omit
- ZX Spectrum Basic/Omit