Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sh command time_to_second

Status
Not open for further replies.

pho01

Programmer
Mar 17, 2003
218
0
0
US
how to convert a date into time_to_second format in a plain hpux shell script?

for example:
04132006 09:19:19 = 1144941559

thanks!
 
I'am not sure what you mean by 'time_to_second' format...

As Perl is installed on HPUX, do something like (the script gives 1144912759 on my machine and not 1144941559, perhaps I do something wrong...):

#!/bin/perl

use Time::Local;
$_ = "04132006 09:19:19";
($mon,$mday,$year,$hours,$min,$sec) = /(\d\d)(\d\d)(\d\d\d\d) (\d\d):(\d\d):(\d\d)/ ;
$mon -= 1 ;
$year-=1900 ;

$thisday = timelocal ($sec,$min,$hours,$mday,$mon,$year);
print "\tthis date: $mday/$mon/$year $hours:$min:$sec - from Epoch (): $thisday\n";


 
Found this script that may help

Source code in perl and shell script to run it.

Code:
The code is:

  a) vastly documented, for your ease of understanding.

  b) thoroughly tested in Solaris 5.7, should run on any unix m/c

How to Info:
~~~~~~~~~~~~
   How to run the scripts:
       
         There are two attachments:  
                                RunScript.sh 
                                & 
                                DateToTimestamp.pl 
  
         KEEP BOTH OF THEM IN THE SAME DIRECTORY

         The first attachment "RunScript.sh" is a shell script.
         It executes the "date" command 
         (which has your date format)
         and 
         passes its output to DateToTimestamp.pl.   
         (which generates the Unix timestamp and prints it)

         change its mode to executable 
           (by the command):
                             $ chmod 755 RunScript.sh

         Then just run this script
           (by the command):
                             $ RunScript.sh

And you get what you want.

REFERENCES
~~~~~~~~~~
  1. Manuals
    $ man  date  (for the manual on unix date format)
    $ man  mktime (for the manual on mktime method I used in .pl)

  2. The POSIX library.
 
##############################################

DateToTimestamp.pl 



#!/usr/local/bin/perl

#
# author:  nabarun of [URL unfurl="true"]www.arzoo.com[/URL]
#

use POSIX;

my (
    $wday,
    $year,
    $month,
    $date,
    $time,
    $zone,
    @wdays,
    @months,
);

#
# Days in a week, as appears in the output of unix shell command "date"
#
@wdays = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

#
# Months in a year, as appears in the output of unix shell command "date"
#
@months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");


#
# the date commands output is passed as the input here (by the RunScript.sh)
#
$inputDate = <STDIN>;

#
# storing the different words in the output
#
@words = split (/\s+/, $inputDate);


$wday  = $words[0];
$month = $words[1];
$date  = $words[2];
$time  = $words[3];
$zone  = $words[4];
$year  = $words[5];

print "\ndate commands output:\n"
      . "~~~~~~~~~~~~~~~~~~~~~\n\t\t$inputDate\n";


for($i = 0; $i <= $#wdays; $i++) {
  if ("$wday" eq "$wdays[$i]") {
    $wday = $i;
#   print "$wday = $wdays[$i]\n";
    last;
  }
}

for($i = 0; $i <= $#months; $i++) {
  if ("$month" eq "$months[$i]") {
    $month = $i;
#   print "$month = $months[$i]\n";
    last;
  }
}

@time_components = split (/\:/, $time);
$hour  = $time_components[0];
$min  = $time_components[1];
$sec  = $time_components[2];
$year = $year - 1900;

print "arguments to the mktime() method:\n"
  .   "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
print "sec = $sec, min = $min, hour = $hour,\ndate = $date, month = $month, "
  . "year (this yr. - 1900) = $year, day = $wday \n\n";


$timestamp = mktime($sec, $min, $hour, $date, $month, $year, $wday, 0, -1);

print "********************************\n";
print "CALCULATED TIMESTAMP = ";
print "$timestamp \n";
print "********************************\n";

print "PRESENT TIMESTAMP    = ";
print (time());
print "\n";
print "********************************\n\n";


##################################

RunScript.sh 


#!/bin/sh
clear;
#
# run the unix command date and pass its input to the output of 
# the perl script  DateToTimeStamp.pl.
#
date | perl DateToTimestamp.pl;

Hope it helps

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top