domingo, setembro 18, 2011

The Unix Time

Don't you know what is Unix Time?
According to Wikipedia: "Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds."

It's a linear representation of time, because we're just working with the amount of seconds that have passed from an event in the past (January, 1, 1970 00:00 - UTC time). Hence, it is widely used in computer systems because it's easy to store (only one "integer" variable is needed), and it makes some time calculations easier. Basically, you will work with dates the same way you work with integer numbers.


Bellow, we list some examples about how to get the Unix Time in different programming languages:

C#:

(DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;



Java:

System.currentTimeMillis()/1000;



JavaScript:

Math.round(new Date().getTime()/1000.0);



MySQL:

SELECT unix_timestamp(now())



Oracle PL/SQL:

SELECT (SYSDATE - TO_DATE('01/01/1970 00:00:00', 'MM-DD-YYYY HH24:MI:SS')) * 24 * 60 * 60 FROM DUAL



Perl:

time



PHP:

time()



PostgreSQL:

SELECT extract(epoch FROM now())



Python:

time.time()



R:

as.numeric(Sys.time())



Ruby:

Time.now



SQL Server:

SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())



Unix or Linux shell:

date +%s