- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- convert human readable to epoch secs
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-14-2010 06:02 PM
тАО01-14-2010 06:02 PM
There are numerous postings on converting human date to epoch, but I am looking for a perl or shell one-liner.
I am doing some automation and bumped into a date validation and time elapsed since a particular date, the output that I get from a query for particular timestamp column is in this format:
14-JAN-2010 17:21:38
I need to pass this value and use the built-in perl localtime/date functions to output this format to epoch secs.
From going through these forums, I can convert current date to epoch and the output of that to the format that I want, but how do I convert the same format back to epoch?
##Convert current time to epoch secs
# perl -e "print time"
## Convert epoch to a timestamp format that I want
# perl -MPOSIX -e 'print strftime "%Y-%m-%d %H:%M:%S\n",localtime(1263520672)'
## Convert the same timestamp (%Y-%m-%d %H:%M:%S) format to epoch?
???
Thanks,
Solved! Go to Solution.
- Tags:
- date arithmetic
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-14-2010 08:42 PM
тАО01-14-2010 08:42 PM
Re: convert human readable to epoch secs
check this site, there is a pelr script :
http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/
mikap
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-14-2010 09:04 PM
тАО01-14-2010 09:04 PM
Re: convert human readable to epoch secs
That helps a little bit, but I am still playing around with it, doesn't give me the correct info yet ..
Timestamp is 14-JAN-2010 20:33:38
#perl -MTime::Local -le '$epoch_secs = timegm(38,33,20,14,01,110); print "$epoch_secs";'
1266179618
#perl -e "print scalar(localtime(1266179618))"
Sun Feb 14 12:33:38 2010
Returns incorrect info?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-14-2010 09:22 PM
тАО01-14-2010 09:22 PM
Re: convert human readable to epoch secs
Other solutions are welcome too ...
I'll leave this thread Open ...
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2010 04:59 AM
тАО01-15-2010 04:59 AM
Re: convert human readable to epoch secs
Here's a small script that lets you pass a date and time argument. It converts this to epoch seconds for output. Leading zeros in the input fields are unnecessary, and the month name can be either lower or uppercase letters or mixed.
# cat ./mytime
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my %months =
( 'JAN'=>1, 'FEB'=>2, 'MAR'=>3, 'APR'=>4, 'MAY'=>5, 'JUN'=>6,
'JUL'=>7, 'AUG'=>8, 'SEP'=>9, 'OCT'=>10, 'NOV'=>11, 'DEC'=>12
);
my $date = shift or die "DD-MMM-YYYY expected\n";
my $time = shift or die "HH:MM:SS expected\n";
my ( $mday, $mon, $year ) = split /-/, $date;
my ( $hours, $min, $sec ) = split /:/, $time;
print timelocal($sec,$min,$hours,$mday,$months{uc($mon)}-1,$year-1900),"\n";
1;
...run as:
# ./mytime 14-JAN-2010 17:21:38
1263507698
# perl -le 'print scalar localtime(1263507698)'
Thu Jan 14 17:21:38 2010
# ./mytime 8-Apr-2010 8:17:23
1270729043
# perl -wle 'print scalar localtime(1270729043)'
Thu Apr 8 08:17:23 2010
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2010 05:26 AM
тАО01-15-2010 05:26 AM
SolutionI assume that your first perl statement sums a number of values and "01" in (38,33,20,14,01,110) will include the full first month - which it should not as long as we are still in January.
You need to convert JAN to 00, FEB to 01, etc., before feeding it to your first perl command,e.g.:
#!/usr/bin/sh
TIMESTAMP="14-JAN-2010 17:21:38"
export DA=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $1}')
export MO=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $2}')
export YE=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $3}')
export HO=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $4}')
export MI=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $5}')
export SE=$(echo "$TIMESTAMP"| awk -F"(-|:|\ )" '{print $6}')
case $MO in
JAN) MO=00;;
FEB) MO=01;;
MAR) MO=02;;
# fill in your month representation....
esac;
SECONDS=$(perl -MTime::Local -le '$epoch_secs = timegm($ENV{'SE'},$ENV{'MI'},$ENV{'HO'},$ENV{'DA'},$ENV{'MO'},$ENV{'YE'}); print "$epoch_secs";')
echo $SECONDS
echo "0d${SECONDS}=y"| adb
# end of example
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2010 10:42 AM
тАО01-15-2010 10:42 AM
Re: convert human readable to epoch secs
You need to use timegm() and gmtime() together, or timelocal() and localtime() together, in order to get the same thing out as what you put in.
The timelocal() and timegm() functions use the same array format as localtime() and gmtime(), as you discovered, with month = 0..11
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2010 11:08 AM
тАО01-15-2010 11:08 AM
Re: convert human readable to epoch secs
Here is how I got this done ... part of the script ...
......
JOB_ID="$(echo ${LINE} | cut -d '|' -f1)"
START_TIME="$(echo ${LINE} | cut -d '|' -f2)"
STATUS="$(echo ${LINE} | awk '{print $NF}')"
export SS="$(echo ${START_TIME} | cut -d_ -f1)"
export MM="$(echo ${START_TIME} | cut -d_ -f2)"
export HH="$(echo ${START_TIME} | cut -d_ -f3)"
export DD="$(echo ${START_TIME} | cut -d_ -f4)"
export MM="$(echo ${START_TIME} | cut -d_ -f5)"
export YY="$(echo ${START_TIME} | cut -d_ -f6)"
START_TIME_IN_EPOCH_SECS="$(perl -MTime::Local -le '$epoch_secs = timelocal('$SS','$MM','$HH','$DD','$MM','$YY'); print "$epoch_secs";')"
CURR_TIME_IN_EPOCH_SECS="$(perl -le "print time")"
ELAPSED_TIME_IN_EPOCH_SECS="$(expr ${CURR_TIME_IN_EPOCH_SECS} - ${START_TIME_IN_EPOCH_SECS})"
ELPASED_TIME_IN_MINS="$(echo ${ELAPSED_TIME_IN_EPOCH_SECS}/60 | bc)"
......
Kind of the same way that John suggested.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2010 01:19 PM
тАО01-19-2010 01:19 PM
Re: convert human readable to epoch secs
This is what I was asking...
Reopening ...
I am running to a weird condition ...
the Output that I am parsing is this:
16295|38_08_12_19_JAN_2010|||STARTED
# perl -MTime::Local -le '$epoch_secs = timelocal('${SS}','${MM}','${HH}','${DD}','${MON}','${YY}'); print "$epoch_secs";'
Illegal octal digit '8' at -e line 1, at end of line
Execution of -e aborted due to compilation errors.
12:08 pm is the time that this job started.
Anyone know why its complaining about '08' Minutes - ${MM} variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2010 01:31 PM
тАО01-19-2010 01:31 PM
Re: convert human readable to epoch secs
> Anyone know why its complaining about '08' Minutes - ${MM} variable?
Yes, this is your *shell* interpreting the leading zero as signifying an *octal* value. If would need to drop the leading zero.
Of course if you use my slightly larger script, you can pass leading zeros or not :-)
Regards!
...JRF...