views:

644

answers:

3

I want to find out the time in unix time (ie seconds since the unix epoch) on 9:00 BST on 1st October 2009. How can I do this on the linux command line?

I know you can use date @$UNIXTIME '+%someformat', but the unix time is what I'm trying to figure out

A: 
date +%s

gives seconds since the epoch

Wikipedia (Unix Time) has

To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"

1240596879

I couldn't see your preferred date while I was editing this answer, so I didn't try it out -- but the example I found looks like a similar format.

pavium
I don't want it for now, I want it for a specificed date in the future
Rory
Yes, it took me a few minutes to find the command option for a specified date (--date= or -d=) and Adam Bellaire beat me to it. But this is good - I've been wondering about this for some time (no pun intended) and I'l go back to that Wikipedia link in future.
pavium
+6  A: 

Using date thus:

date --date="Oct 1 09:00:00 BST 2009" +%s

Yields:

1254384000
Adam Bellaire
A: 

I use this website: http://www.epochconverter.com/ to convert unixtime to human readable and vice versa. Even though it's not code, it's useful for validating your code. Here's some code (in Java) to do what you asked. The unixtime it prints out is an hour off according to epochconverter.com (not sure why).

SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy HH:mm z" );

try {
    Date d = sdf.parse("10/01/2009 09:00 BST");
    System.out.println("Unixtime is: " + d.getTime() / 1000);
} catch (ParseException pe) { pe.printStackTrace();
NotWally