How to Convert date time stamp to epoch time?

December 10, 2010 at 05:10:54
Specs: Windows XP
i need the command for this.


Thanks in advance


See More: How to Convert date time stamp to epoch time?

Report •

#1
December 10, 2010 at 09:10:35
In the bash shell, the command to get the epoch time stamp is "date +%s" (no quotes), but that returns the current epoch time.

You could build a shell function (script) to calculate the equivalent epoch time using the following information:

The current epoch time consists of the number of seconds since midnight, January 1, 1970.

1 minute = 60 seconds
1 hour = 3600 seconds
1 day = 86400 seconds
1 week = 604800 seconds
1 month (30.44 days) = 2629743 seconds
1 year (365.24 days) = 31556926 seconds

This information (and much more) comes from Epoch Converter.

Ernie Registered Linux User 247790


Report •

#2
December 12, 2010 at 23:29:13
This bash script uses perl's timelocal function to determine the seconds since the epoch of a file called nowfile.txt. This will only work with the GNU version of the ls command.

Let me know if you have any questions:

# YYYY MM DD HH MM SS
# perl autosplits the string and uses timelocal to return
# the number of seconds from the Epoch.
# No error checking!
function seconds_from_epoch {
echo $*| perl -MTime::Local -ane '
my $epochseconds = timelocal($F[5], $F[4], $F[3], $F[2], $F[1] - 1, $F[0]);
print "$epochseconds\n"; '
}

# parse file nowfile.txt date and time
set - $(ls -l nowfile.txt)
fdate=$6
ftime=$7

# parse the year, month, and day
set - $(IFS="-"; echo $fdate)
fyear=$1
fmonth=$2
fday=$3

# parse the hours and minutes
set - $(IFS=":"; echo $ftime)
fhour=$1
fmin=$2

totsecs=$(seconds_from_epoch "$fyear $fmonth $fday $fhour $fmin 0")
echo $totsecs


Report •
Related Solutions


Ask Question