Can anyone help me to print UNIX epoch time to days,hours,min,sec ?

I have unix epoch time 1441678454803, Can you please help me to print this time in below format ?

DAY,HOUR,MIN,SEC

Appreciate your help!!!

Thanks,
Prince

#!/usr/bin/perl

# convert_epoch.pl

use strict;
use warnings;

my $epoch = shift or die;
my ($sec, $min, $hour, $day) = (localtime($epoch))[0,1,2,3];
print "$day,$hour,$min,$sec\n";

Run as perl convert_epoch.pl 1441678454803

1 Like

As a note - with Linux (GNU date) try:

 date --date='@1441678454803' +'%a %H %m %S'

Bad number? It is for the year 47654
Plus the number you gave will not work as a valid UNIX epoch second on a lot of older systems because it is for the year far in the future -- %Y prints the full year 4 or more digits. I ran this on a 64 bit OS so it would correctly output:

 date --date='@1441678454803' +'%a %H %m %S %Y'
Sun 22 12 43 47654

So I would question this number as a valid date

1 Like

Do you know any commands in UNIX without perl script? For example if its 25 hours, then our result should be.

1day,1hour,0min,0sec.

Appreciate your help!
Thanks

---------- Post updated at 09:11 AM ---------- Previous update was at 09:04 AM ----------

Can you calculate with 1443929685, For example if its 25 hours, then our result should be.

1day,1hour,0min,0sec.

Appreciate your help!
Thanks

It appears you are confusing what epoch time is. That 1443929685 is the representation in seconds of a specific time; in this case Sat Oct 3 21:34:45 2015 in my timezone
There is not way of getting 1day,1hour,0min,0sec unless you are computing the difference against another time.
Please, mention what shell you are using if you do not want to use Perl.

Hello,

Am using kernel shell. Aww computing help me.

Thanks,

You probably mean the korn shell. If you have the korn shell 93 on your system, you can use

$ printf "%(%Y-%m-%d %T)T\n" '#1443929685'
2015-10-04 05:34:45

This requires the printf builtin function, which is activated by default in ksh93, not /usr/bin/printf . By changing the format string in the printf statement you can easily change the output format.

Maybe you mean something more like:

#!/bin/ksh
time="${1:-1441678454803}"
sec=$((time % 60))
time=$((time / 60))
min=$((time % 60))
time=$((time / 60))
hrs=$((time % 24))
days=$((time / 24))
[ $days -eq 1 ] && daysp="" || daysp=s
[ $hrs -eq 1 ] && hrsp="" || hrsp=s
[ $min -eq 1 ] && minp="" || minp=s
[ $sec -eq 1 ] && secp="" || secp=s
printf '%d day%s, %d hour%s, %d minute%s, %d second%s since the Epoch\n' \
    $days "$daysp" $hrs "$hrsp" $min "$minp" $sec "$secp"

which, for your sample value of 1441678454803 seconds since the Epoch produces the output:

16686093 days, 5 hours, 26 minutes, 43 seconds since the Epoch

and, if it is invoked with the operand 90061 , produces the output:

1 day, 1 hour, 1 minute, 1 second since the Epoch
1 Like