Chapter 3. Dates and Times

Introduction

It is inappropriate to require that a time represented as seconds since the Epoch precisely represent the number of seconds between the referenced time and the Epoch.

IEEE Std 1003.1b-1993 (POSIX) Section B.2.2.2

Times and dates are important things to be able to manipulate. “How many users logged in last month?”, “How many seconds should I sleep, if I want to wake up at midday?”, and “Has this user’s password expired yet?” are all common questions whose answers involve surprisingly non-obvious manipulations.

Perl represents points in time as intervals, measuring seconds past a point in time called the Epoch. On Unix and many other systems, the Epoch was 00:00 Jan 1, 1970, Greenwich Mean Time (GMT).[5] On a Mac, all dates and times are expressed in the local time zone. The gmtime function returns the correct GMT time, based on your Mac’s time zone offset. Bear this in mind when considering the recipes in this chapter. The Macintosh’s Epoch seconds value ranges from 00:00 Jan 1, 1904 to 06:28:15 Feb 6, 2040.

When we talk about dates and times, we often interchange two different concepts: points in time (dates and times) and intervals between points in time (weeks, months, days, etc.). Epoch seconds represent intervals and points in the same units, so you can do basic arithmetic on them.

However, people are not used to working with Epoch seconds. We are more used to dealing with individual year, month, day, hour, minute, and second values. ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.