By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

date format question

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. system('clear');
  4.  
  5. my %days = (
  6.         Sun => 'Sunday',
  7.         Mon => 'Monday',
  8.         Tue => 'Tuesday',
  9.         Wed => 'Wednesday',
  10.         Thu => 'Thursday',
  11.         Fri => 'Friday',
  12.         Sat => 'Saturday',
  13.         );
  14.  
  15. my %months = (
  16.         '01' => 'January',
  17.         '02' => 'February',
  18.         '03' => 'March',
  19.         '04' => 'April',
  20.         '05' => 'May',
  21.         '06' => 'June',
  22.         '07' => 'July',
  23.         '08' => 'August',
  24.         '09' => 'September',
  25.         '10' => 'October',
  26.         '11' => 'November',
  27.         '12' => 'December',
  28.         );
  29.  
  30. print "Enter a date dd/mm/yyyy\n";
  31. chomp (my $ans = <>);
  32.  
  33. my @tmp = split(/\//,$ans);
  34.  
  35. print "day - $tmp[0], month = $tmp[1] year = $tmp[2]\n";
  36.  
  37. print "month is $months{$tmp[1]} \n";
  38.  
  39. my @caldata = `cal -m $tmp[1] $tmp[2]`;
  40.  
  41. foreach my $line (@caldata){
  42.         if ($line =~ /[a-zA-Z]+/){
  43.                 $line = "";
  44.                 next;
  45.         }
  46.         else{
  47.                 chomp($line);
  48.                 $line =~ s/ (\d[^\d]| \d$)/0$1/g;
  49.                 #$line =~ s/ (\d)[^\d]/0$1 /g;
  50.                 my @line = split(/ /,$line);
  51.                 print "$line\t element count - ", scalar @line, "\n";
  52.         }
  53. }
  54.  
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
Apr 14 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Apr 14 '08 #2

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1.                 $line =~ s/ (\d[^\d])/0$1/g;
  2.                 $line =~ s/ (\d)$/0$1/g;
  3.                 $line =~ s/\s{2} /XX /g;
  4.  
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
Apr 14 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Apr 14 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
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
Apr 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.