Home: Perl Programming Help: Intermediate:
Little problem



MouldyGoat
stranger

Mar 2, 2002, 4:39 PM


Views: 474
Little problem

I've been trying to make a Perl equivalent of the PHP date() function, (see http://www.php.net/manual/en/function.date.php), and it's almost working except for a couple of minor things I think I can fix and this one problem which has got me stumped.

One of the characters you can use in the format string is 'y' - the year in two letters, i.e. '02' for 2002.

In the function I do this to get the two-letter year:



Code
my ($p, $ts) = ($_[0], $_[1]); #$p stands for pattern, $ts for timestamp 
my (@bits);

@bits = localtime($ts);
$dt_y = sprintf("%02d", $bits[5] % 100


If I print out the variable's value right there it works fine. The problem comes after I interpolate the value into the pattern variable and return it like this:


Code
$p =~ s/(?<!\\)([aAdDFgGhHiIjlLmMnOrsStUwYyzZ])/${"dt_".$1}/eeg; 
return $p;


Unfortunately the 2-letter years don't seem to be zero-padded.. has anyone got any idea why? Any suggestions at all very welcome.


mhx
Enthusiast / Moderator

Mar 3, 2002, 1:08 AM


Views: 472
Re: [MouldyGoat] Little problem


In Reply To

Code
$p =~ s/(?<!\\)([aAdDFgGhHiIjlLmMnOrsStUwYyzZ])/${"dt_".$1}/eeg; 
return $p;



I'm surprised that the above code worked at all. Wink
At least, it didn't work when I tried it. It generated lots of warnings, isn't strict safe, and yielded an empty string. The problem is the replace part of the search/replace operation, which should instead be something like:


Code
$p =~ s/([xyz])/"\${dt_$1}"/eeg;


(I've stipped down the search part for my test...)
The above code is strict safe and works the way you would expect it to work. However, using a hash would be easier and faster:


Code
$dt{y} = sprintf "%02d", $bits[5] % 100; 
$p =~ s/([xyz])/$dt{$1}/g;


Finally, you could use the [url=http://www.opengroup.org/onlinepubs/7908799/xsh/strftime.html]strftime function from the [url=http://www.perldoc.com/perl5.6.1/lib/POSIX.html]POSIX standard module. It seems to have just about the same functionality than PHP's date:

[perl]
#!/usr/bin/perl -w
use strict;
use POSIX;

my $date = POSIX::strftime( "%y", localtime );
print "The date is '$date'...\n";
[/perl]

The format string may, of course, be more complex:

[perl]
my $date = POSIX::strftime( "%A, %B %d, %Y", localtime );
[/perl]

will put the following into $date:


Code
Sunday, March 03, 2002


Hope this helps,

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



MouldyGoat
stranger

Mar 3, 2002, 3:53 AM


Views: 469
Re: [mhx] Little problem

Thanks a lot Smile I was thinking whilst I was writing it that it didn't look very beautiful...


freddo
User

Mar 3, 2002, 4:32 AM


Views: 463
Re: [mhx] Little problem (off subject)

Hello,

Now this is just my opinion, but why the hell people write month/day/year or day/month/year? I mean is there an historical reason?

i ALWAYS wrote the date year/month/day since i was 7 or 8 (at the displeasure of my teachers, i'm damn limited when i want to) with 0 to make it fit, it's far easier to classify some papers... like we're the 20020303... who cant understand this?

It's like the accent on letters in french or swedish, i dont care about them, i know i'm loosing culture while i doesnt use them, but anyway, i have a US keyboard, so dont expect me to type them....

Can someone enlighten me on dates? Personally i dont see the point of using it in one form or another... for me 161913321020610 (print localtime) would do fine (well, no, because before 1970.... but one could easily come with another way, and that would be fine)...

A lot of country (all?) have accepted to use that 24h/day standard why is this useful? I mean, you need to take in the GMT difference... 1234 would be a reference as usefull... SWATCH tried to make a new "internet" time (it's @558 right now) but it's far from precise... (and i would prefer an hexadecimal thing)

anybody have ideas about how to count time?

Freddo
;---


mhx
Enthusiast / Moderator

Mar 3, 2002, 5:23 AM


Views: 461
Re: [freddo] Little problem (off subject)


In Reply To
Now this is just my opinion, but why the hell people write month/day/year or day/month/year? I mean is there an historical reason?


I think so. I also don't like all those different time formats, but of all of them, I find the english one most confusing.

There's an ISO standard (ISO 8601) available since 1988 that suggests a consistent way to write dates and times, and I really, really like it. The format they propose and the one I usually use is YYYY-MM-DD HH:MM:SS. They recently updated the standard and there's a [url=http://www.pvv.ntnu.no/~nsaa/8601v2000.pdf]final draft of the document available as PDF. In my above post, I only copied the format string directly from a manpage. Wink

You should have a look at ISO 8601 and keep it in mind. It's often a lot easier to convince someone of the way you are writing dates if you can tell them "Hey, this is an ISO standard.". Cool

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



freddo
User

Mar 3, 2002, 1:30 PM


Views: 456
Re: [mhx] Little problem (off subject)

Hi Mhx,


In Reply To
You should have a look at ISO 8601 and keep it in mind. It's often a lot easier to convince someone of the way you are writing dates if you can tell them "Hey, this is an ISO standard." Cool


Yeah, it keep that one fire just for THAT... Thanks a lot....

Freddo
;---