SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date format in Perl

    Hello,

    I'm currently rewritting an old Perl script of mine. In this script the date and time is printed with the following code:

    Code:
    print scalar(localtime);
    But this gives the following output "Sun Apr 15 07:32:12 2007".

    First of all I want the output to have the following format:

    "Sunday, April 15, 2007 at 07:32:12"

    Furthermore I have a variable $timezone. This variable is the number of hours ahead or behind on the system time. So if $timezone=6, then the output should be:

    "Sunday, April 15, 2007 at 13:32:12"

    Any one know how I can print the date and time in the right format and use $timezone to correct the time?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for formatting the date/time:

    http://perldoc.perl.org/POSIX.html

    look in the FUNCTIONS section and find the strftime function

    as far as the offset you would appy that to the time function, if 6 is negative 6 hours

    time-3600*6

    or positive offset:

    time+3600*6

    where 3600 is the number of seconds in one hour (60*60)

    there might be other ways to handle the timezone offest too but thats how I generally do it.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code, if you want.
    Code:
    #!/usr/bin/perl
    @months = qw(January February March April May June July August September October Novmber December);
    @weekDays = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
    
    $current = time;
    $timezone=6;
    $current = $current + ($timezone * 3600);
    ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime($current);
    $year = 1900 + $yearOffset;
    
    print "$weekDays[$dayOfWeek], $months[$month] $dayOfMonth, $year at $hour:$minute:$second \n";
    SparkSupport
    Redifining Support Integrating Ideas
    http://sparksupport.com
    Articles


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •