Display Date & Time, Review Script.

Posted on 2003-03-07
Medium Priority
Last Modified: 2010-03-05
print "Content-type: text/html \n\n";




  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  @pairs = split(/&/, $buffer);
  foreach $pair (@pairs) {
       ($name, $value) = split(/=/, $pair);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       $value =~ s/"/'/g;
       $FORM{$name} = $value;


 my $offset = -3;
 my $zone = PST;
 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
 $year += 1900;
 $hour=$hour + $offset;
 if ($hour = 0) {$hour = $hour+12;}
 if ($hour >= 24) {
 $hour = $hour - 24;
 $mday = $mday + 1;
 if ($hour >= 12) {
     if ($hour > 12){
           $hour = $hour - 12;
     $ampm = "PM";
 else { $ampm = "AM"; }

 if ($sec < 10) { $sec = "0$sec"; }
 if ($min < 10) { $min = "0$min"; }
 if ($hour == 0) { $hour = "12"; }
 if ($mon < 10) { $mon = "0$mon"; }
 if ($mday < 10) { $mday = "0$mday"; }
 $month = ($mon + 1);
 @months = ("January","February","March","April","May","June","July","August","September","October","November","December");
 my $long_date = "$months[$mon] $mday, $year";
 my $short_date = "$month/$mday/$year";
 my $arch_year = "$year";
 chop($date) if ($date =~ /\n$/);
 my $time = "$hour:$min $ampm $zone";


sub date
  print qq~


This is how I'm displaying time & date on my perl scripts.  I wanted someone to review it and if I couldn't do it in a more simplified way, please let me know.  Recently, the time hasn't been displaying correctly and I'm not exactly sure as to whats wrong.

As of now this is worth 25 points but as always, I will raise points if I feel the answer given is all gravy.

Thanks in advance.
Question by:KenHeckert
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8092568
If your problem is that you are not getting anything then the problem is that you are calling the &date routine before the variables are even set.  If the problem is with the output, please provide the output you are getting and what you expect.

Author Comment

ID: 8092593
I just switched the $date; routine below the variables so it's displaying now.  The problem I'm having is that it's displaying $time as always being 12:## AM/PM.  The other numbers change but 12 stays the same.
LVL 26

Accepted Solution

wilcoxon earned 200 total points
ID: 8092624
This line is your problem.

if ($hour = 0) {$hour = $hour+12;}

It should be:

if ($hour == 0) {$hour = $hour+12;}

The way you have it, you are always setting $hour to 0.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8092643
Thanks wilcoxon.  Something so simple, at one time it was working correctly, I wonder when I messed it up.  I increased the points to 50.

sykkn, look for a thread giving you points as well.  Although 15 isn't much, it's something.

Appreciate the help.
LVL 84

Expert Comment

ID: 8093356
my $offset = -3;
my $zone = PST;
my @date = localtime(time+60*60*$offset);

use POSIX;

my $long_date = strftime("%B %d %Y",@date);
my $short_date = strftime("%m/%d/%Y",@date);
my $arch_year = $date[5]+1900;
my $time = strftime("%r $zone",@date);

Author Comment

ID: 8093398
Thanks ozo, I'll drop some points your way.  Can you do me a favor and drop me a list of what each %? means, incase I need to customize the time/date in the future.

Much appreciated.

Author Comment

ID: 8093461
Also, what is: use POSIX; refer to?
LVL 84

Expert Comment

ID: 8096118
see `perldoc POSIX`
                         Convert  date  and  time  information  to  string.
                         Returns the string.
                                 strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)

                         The  month  (mon),  weekday  (wday),  and  yearday
                         (yday)  begin  at zero.  I.e. January is 0, not 1;
                         Sunday is 0, not 1; January 1st is 0, not 1.   The
                         year  (year)  is given in years since 1900.  I.e.,
                         the year  1995  is  95;  the  year  2001  is  101.
                         Consult   your  system's  strftime()  manpage  for
                         details about these and the other  arguments.   If
                         you  want  your  code  to be portable, your format
                         (fmt) argument  should  use  only  the  conversion
                         specifiers  defined by the ANSI C standard.  These
                         are aAbBcdHIjmMpSUwWxXyYZ%.  The  given  arguments
                         are  made consistent as though by calling mktime()
                         before calling your system's strftime()  function,
                         except that the isdst value is not affected.
                         The string for Tuesday, December 12, 1995.
                                 $str = POSIX::strftime( "%A, %B %d, %Y", 0, 0, 0, 12, 11, 95, 2 );
                                 print "$str\n";

     %%        same as %
     %a        locale's abbreviated weekday name
     %A        locale's full weekday name
     %b        locale's abbreviated month name
     %B        locale's full month name
     %c        locale's appropriate date and time representation
     %C        century number (the year divided by 100 and truncated to an
               integer) as a decimal number [00-99]
     %d        day of month ( 01 - 31 )
     %D        date as %m/%d/%y
     %e        day of month (1-31; single digits are preceded by a blank)
     %h        locale's abbreviated month name.
     %H        hour ( 00 - 23 )
     %I        hour ( 01 - 12 )
     %j        day number of year ( 001 - 366 )
     %KC       locale's appropriate date and time representation
     %m        month number ( 01 - 12 )
     %M        minute ( 00 - 59 )
     %n        same as new-line
     %p        locale's equivalent of either AM or PM
     %r        locale's 12-hour time representation, default %I:%M:%S [AM|PM]
     %R        time as %H:%M
     %S        seconds ( 00 - 61 ), allows for leap seconds
     %t        same as a tab
     %T        time as %H:%M:%S
     %U        week number of year ( 00 - 53 ), Sunday is the first day of
               week 1
     %w        weekday number ( 0 - 6 ), Sunday = 0
     %W        week number of year ( 00 - 53 ), Monday is the first day of
               week 1
     %x        locale's appropriate date representation
     %X        locale's appropriate time representation
     %y        year within century ( 00 - 99 )
     %Y        year as ccyy ( e.g. 1986)
     %Z        time zone name or no characters if no time zone exists

Expert Comment

ID: 8233884
Points have been allocated for you at: http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20543153.html

If you do not claim them in two weeks, I will honor the request to delete the question.

Community Support Moderator @Experts Exchange

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question