Epoch Time

unix epoch

I’ve spoken about Unix epoch before, so this is a follow up post with the next few things about epoch time that I find particularly interesting.

What is Unix Epoch time?

Unix Epoch time is the number of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970.

It’s an interesting concept, because the point in time is specific time we can easily recognise and process – it’s a specific time on a certain day way back in 1970. But the Epoch time is not a point in time, but a large counter – specifically, a number of seconds elapsed since the beginning on Unix time.

Does Epoch Time differ with timezones?

This is one of the most interesting things about Unix time. Even though it’s based on a specific timezone (UTC), the Unix time itself is not timezone specific. So one server referring to a specific Unix time will be referring to exactly the same point in time as another server in a different timezone – for both of them the number of seconds elapsed since UTC will be the same.

It is a great question though, because Unix Epoch time is commonly converted back to human-friendly time and date stamps, which at that point become timezone specific. So if you decode Epoch time value on two servers in different timezones, you will end up with a different human-friendly timestamp.

Will there be an end of Epoch Time?

Apparently, current representation of Epoch time means some 32-bit implementations will have a problem similar to Y2K, only a bit later: sometime on January 19th, 2038. On that day, the time counter will reach its maximum value for a 32-bit signed number, meaning next valid value will have to be 0 – meaning both the end and the start of the Unix Epoch time.

This 2038 problem sounds like such a fascinating issue that I’ll have a separate post on it soon!

See Aso




Converting date and time to Unix epoch in Perl

Today I was working on a script, and one of the subroutines needed simple seconds-based arithmetics with time. As you probably remember fromĀ  my date and time in Unix scripts article, the easiest way to approach this task is to deal with the raw representation of date and time in Unix – the Unix epoch times. This post will show you how to convert standard dates into Unix epoch times in Perl.

Why would you want to convert timestamps to Unix epoch time?

Unix epoch time is an ever-growing counter which increments every second and shows the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. Such numbers are pretty useless in their raw form for the tasks of confirming current time and date, but they’re perfect for measuring between two points in time.

Current epoch time in Perl

First things first. If you want to confirm the current Unix epoch time in Perl, here’s how you do it: just use the time() subroutine.

#!/bin/perl
print "Current (epoch) time: " . time() . "\n";

Converting regular date and time into epoch time

It’s equally easy to convert time into Unix epoch representation, for this you should use timegm() or localtime() subroutine from the Time::Local module.

timegm() has the following syntax:

$current = timegm($sec,$min,$hours,$day,$month,$year);

Important: While most of the parameters are self-explanatory, it is probably worth reminding that $year should be specified as a number like 109 and not 2009.
Also, the $month is not the number of a month, but a number of months since January. So, 0 will mean January itself, 1 will mean February, and 11 means December.

Here’s a fully working example, showing the countdown in seconds until midnight on the New Year’s Eve 2009:

#!/usr/bin/perl
#
use Time::Local;
#
$sec=59;
$min=59;
$hours=23;
$day=31;
$month=11;
$year=109;
#
$current = time();
print "Current (epoch) time: $current\n";
#
$newyear = timegm($sec,$min,$hours,$day,$month,$year);
print "New Year's Eve 2009: $newyear\n";
#
print "Only " .($newyear-$current) . " seconds to go\n";

When you save this script as epoch.pl and make the file executable, here’s what you’ll get if you run it:

# ./epoch.pl
Current (epoch) time: 1234911622
New Year's Eve 2009: 1262303999
Only 27392377 seconds to go

That’s it for today! Hope this little post will save you time when tacking next scripting challenge with Perl.

See also