Times a current time "12/DEC/2001:12:23:12" (apache Time) and parses then diplays it as epoch then converts back -JoeCool
#!/usr/bin/perl # # Epoch.pl # # Convert current time to epoch and back # # Joe Henderson # # 1.0 -> 1Dec01 # Born # # 1.1 -> 9Dec01 # Returned Epoch # # 1.2 -> 10Dec01 # Added Subroutines # system("clear"); #Clear Screen use Time::Local; #Using Time Mod $date = "17/Nov/2002:12:11:37"; #Manual Date $etime = &epoch($date); #Getting Epoch Number ($eday, $emon, $eyear, $ehr, $emin, $esec) = &etime($etime); #Convert +Back Time print "Date Entered: $date\n"; print "Epoch Date: $etime\n"; print "Date: $eday $emon$eyear $ehr:$emin:$esec\n"; exit; ##Subroutines sub etime ##Change from Epoch Time to DMYHMS { my $eptime = @_[0]; my %months ('1',Jan,'2',Feb,'3',Mar,'4',Apr,'5',May, '6',Jun,'7',Jul,'8',Aug,'9',Sep,'10',Oct,'11',Nov, '12',Dec); ($esec, $emin, $ehr, $eday, $emon, $eyear) = localtime($eptime); $eyear += 1900; $emon += 1; #Time Zone Change (Zulu) $emon = $emonths{$emon}; return ($eday, $emon, $eyear, $ehr, $emin, $esec); } sub epoch ##Change from DMYHMS to Epoch Time { my $date = @_[0]; %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); $date =~ tr/\[//d; @dates = split(/\:/, $date); @mons = split(/\//, $dates[0]); $year = $mons[2]; $mon = $mons[1]; $day = $mons[0]; $hr = $dates[1]; $min = $dates[2]; $sec = $dates[3]; my $mon = $months{$mon}; if(!($mon < 1) || ($mon > 12)) ##Bad Dates Chk { $time = timelocal($sec, $min, $hr, $day, $mon-1, $year-1900); #converts to Epoch chomp($time); } return $time; chomp($time); return $time; }

In reply to Epoch Conversion by JoeCool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.