date format question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • g0uki
    New Member
    • Feb 2008
    • 5

    date format question

    Hi all,

    Hope someone can help, i get the date in the format of 12/12/2008

    i 'd like to find out what day this is on, either a weekday/weekend. so output would be:
    12/12/2008

    midweek: fri 12 december

    I'm also trying to do it the hard way, without the use of any modules .. i feel this will help my perl skills.

    after the user has entered a date, i use the back tick operator to ask the shell a cal command and then put it all into an array.

    I then try to see if i can change all single digits to double digits,i.e.

    1 -> 01, 2 ->02...

    but can't do it for the very last number for any month... whereas the other ones i can :(

    Here is my attempt:

    [CODE=perl]
    #!/usr/bin/perl -w
    use strict;
    system('clear') ;

    my %days = (
    Sun => 'Sunday',
    Mon => 'Monday',
    Tue => 'Tuesday',
    Wed => 'Wednesday',
    Thu => 'Thursday',
    Fri => 'Friday',
    Sat => 'Saturday',
    );

    my %months = (
    '01' => 'January',
    '02' => 'February',
    '03' => 'March',
    '04' => 'April',
    '05' => 'May',
    '06' => 'June',
    '07' => 'July',
    '08' => 'August',
    '09' => 'September',
    '10' => 'October',
    '11' => 'November',
    '12' => 'December',
    );

    print "Enter a date dd/mm/yyyy\n";
    chomp (my $ans = <>);

    my @tmp = split(/\//,$ans);

    print "day - $tmp[0], month = $tmp[1] year = $tmp[2]\n";

    print "month is $months{$tmp[1]} \n";

    my @caldata = `cal -m $tmp[1] $tmp[2]`;

    foreach my $line (@caldata){
    if ($line =~ /[a-zA-Z]+/){
    $line = "";
    next;
    }
    else{
    chomp($line);
    $line =~ s/ (\d[^\d]| \d$)/0$1/g;
    #$line =~ s/ (\d)[^\d]/0$1 /g;
    my @line = split(/ /,$line);
    print "$line\t element count - ", scalar @line, "\n";
    }
    }
    [/CODE]

    the very last date on any line is giving me probs, the single digit remains single, like:

    01 02 03 04 05 06 7

    Any help would b very much appreciated.

    thanks
    g0uki
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    I haven't extensively read the page (due to not having much time at the moment), but you may want to check out the DateTime module from CPAN. It may do what you are looking for.

    Regards,

    Jeff

    Comment

    • g0uki
      New Member
      • Feb 2008
      • 5

      #3
      Thanks Jeff,

      I got it working in the end :))

      the server this will live on doesn't have DateTime.pm nstalled and i'm not sure if i'll be allowed to install it. Server guys for ya!

      i modded the following at line 48:

      [CODE=perl]
      $line =~ s/ (\d[^\d])/0$1/g;
      $line =~ s/ (\d)$/0$1/g;
      $line =~ s/\s{2} /XX /g;
      [/CODE]

      and now it works like a dream.. only i'm not too happy about doing a regex 3 times in three lines.. but hey.. as i learn more i feel i'll pick up perl's short hand approach.

      btw, at one point i tried alternation approach..

      $line =~ s/ (\d[^\d])| (\d)$/0$1/g

      which i think only works for the first remebered element?
      because at one point both will be true, is there a way to harness this?

      thanks
      g0uki

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Glad that you got it working. Its always a good feeling when you get something working.

        Don't forget though, even though you cannot install modules, it doesn't mean that you cannot dissect them and pull out of them the code that does what you want. I have had to do that before a few times. Sometimes its the only way around such "server guys" road blocks.

        (when are those server guys gonna stop us from doing our job?)

        Regards,

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          There is no need to shell out to the 'cal' application. Use Time::Local. see this snippet I wrote on another forum:

          http://www.daniweb.com/code/snippet570.html

          Comment

          Working...