# Recipe 3.3 Converting Epoch Seconds to DMYHMS

#### 3.3.1 Problem

You have a date and time in Epoch seconds, and you want to calculate individual DMYHMS values from it.

#### 3.3.2 Solution

Use the localtime or gmtime functions, depending on whether you want the date and time in UTC or your local time zone.

```(\$seconds, \$minutes, \$hours, \$day_of_month, \$month, \$year,
\$wday, \$yday, \$isdst) = localtime(\$time);```

The standard Time::timelocal and Time::gmtime modules override the localtime and gmtime functions to provide named access to the individual values.

```use Time::localtime;        # or Time::gmtime
\$tm = localtime(\$TIME);     # or gmtime(\$TIME)
\$seconds = \$tm->sec;
# ...```

#### 3.3.3 Discussion

The localtime and gmtime functions return strange year and month values; the year has 1900 subtracted from it, and 0 is the month value for January. Be sure to correct the base values for year and month, as this example does:

```(\$seconds, \$minutes, \$hours, \$day_of_month, \$month, \$year,
\$wday, \$yday, \$isdst) = localtime(\$time);
printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n",
\$hours, \$minutes, \$seconds, \$year+1900, \$month+1,
\$day_of_month);```

We could have used the Time::localtime module to avoid the temporary variables:

```use Time::localtime;
\$tm = localtime(\$time);
printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n",
\$tm->hour, \$tm->min, \$tm->sec, \$tm->year+1900,
\$tm->mon+1, \$tm->mday);```

The localtime function in perlfunc(1) and Chapter 29 of Programming Perl; the documentation for the standard Time::localtime and Time::gmtime modules; convert in the other direction using Recipe 3.3

 Chapter 1. Strings
 Chapter 2. Numbers
 Chapter 4. Arrays
 Chapter 5. Hashes
 Chapter 6. Pattern Matching
 Chapter 7. File Access
 Chapter 8. File Contents
 Chapter 9. Directories
 Chapter 10. Subroutines
 Chapter 11. References and Records
 Chapter 12. Packages, Libraries, and Modules
 Chapter 13. Classes, Objects, and Ties
 Chapter 14. Database Access
 Chapter 15. Interactivity
 Chapter 16. Process Management and Communication
 Chapter 17. Sockets
 Chapter 18. Internet Services
 Chapter 19. CGI Programming
 Chapter 20. Web Automation
 Chapter 21. mod_perl
 Chapter 22. XML