Date procedure? - Page 2

•  Subject
• Author
• Posted on

Re: Date procedure?

I suggest 'localtime' instead of 'gmtime' - unless you need
everyone around the world to understand exactly the precise
time you mean, in which case I'd suffix GMT and want hours,
minutes and seconds.

Justin.

--
Justin C, by the sea.

Re: Date procedure?

On 8/12/2014 16:00, Tuxedo wrote:

use strict;
use warnings;
use feature qw/say/;;

my \$orig_date = '2014-12-08';

say ReformatDay('\$DD \$MM \$Y',               \$orig_date);
say ReformatDay('\$D (\$DDDD) \$M (\$MMMM) \$Y', \$orig_date);
say ReformatDay('\$MMM/\$DDD \$Y',             \$orig_date);

#   \$M    = 2
#   \$MM   = 02
#   \$MMM  = Feb
#   \$MMMM = February
#   \$D    = 9
#   \$DD   = 09
#   \$DDD  = Sun
#   \$DDDD = Sunday
#   \$Y    = 2014
#
sub ReformatDay
{
my (\$Y,\$M,\$D) = \$_[1] =~/^(\d)-0?(\d)-0?(\d)\$/ or return undef;
my \$MM   = sprintf "%02d", \$M;
my \$MMMM = {qw/1 January 2 February 3 March 4 April 5 May 6 June 7 July
8 August 9 September 10 October 11 November 12 December/}->;
my \$MMM  = substr \$MMMM, 0, 3;
my \$DD   = sprintf "%02d", \$D;
my \$DDDD = sub {
my (\$d,\$m,\$y)=@_;
\$m < 3 && \$y--;
->{(\$d
+ ((qw/0 3 2 5 0 3 5 1 4 6 2 4/)[\$m-1])
+\$y+(int(\$y/4))-(int(\$y/100))+(int(\$y/400)))%7}
}->(\$D,\$M,\$Y);
my \$DDD = substr \$DDDD, 0, 3;
eval "\"\$_[0]\""
}

Re: Date procedure?

George Mpouras wrote:

[...]

->{(\$d

Many thanks for sharing this example. It appears to work perfectly!

Tuxedo

Re: Date procedure?

[more of this]

People who are less keen on baffling others with funny characters and
pointless byzantinisms might feel inclined to use something like this

-----------
use POSIX;

my \$d = '2014-12-08';

sub reformat
{
my (\$y, \$m, \$d) = split(/-/, \$_[0]);
return (strftime("%e %B %Y", 0, 0, 0, \$d, \$m - 1, \$y - 1900) =~ / ?(.*)/)[0];
}

print(reformat(\$d), "\n");
-----------

If your system has a strftime man page, you can get the full set of
available format specifiers from there.

Re: Date procedure?

Rainer Weikusat wrote:

[...]

Perhaps a little less modular in returning localized month strings than the
script posted previously by George Mpourasbut, but the above is much
shorter and easier for me to see through.

I do indeed have a strftime man page and will look into it.

Thanks for the pointer and for a most usable date reformatting procedure.

Tuxedo

Re: Date procedure?

[...]

If you don't want localized names, you can just use an array of month
names and index that by the month number - 1 (since array indices start
with zero):

-----------
my \$d = '2014-12-08';
my @months = qw(January February March April May June July August September October November December);

sub reformat
{
my (\$y, \$m, \$d) = split(/-/, \$_[0]);
return sprintf("%d %s %d", \$d, \$months[\$m - 1], \$y);
}

print(reformat(\$d), "\n");

Re: Date procedure?

Rainer Weikusat wrote:

[...]

The above procedure is now both short and modular, quite perfect!

Many thanks,
Tuxedo

Re: Date procedure?

[...]

Well, but it looks as if the author had be exposed to an overdose of PHP
while being in a less-than-resistant mental state. Here's one making use
of facilities typically associated with Perl (Yes, this is sort-of
silly. But it makes a nice, simple example):

-----------
my @months = qw(January February March April May June July August September October November December);

sub reformat
{
\$_[0] =~ /(\d+)-(\d+)-0?(\d+)/ and "\$3 \$months[\$2 - 1] \$1";
}

print(reformat('2014-05-08'), "\n");
-----------

Re: Date procedure?

Rainer Weikusat wrote:

Thanks for this mentally enhanced solution under the influence of perl :-)

Tuxedo

Re: Date procedure?

12/09/2014
said:

People who are more concerned with a dialog than with baseless charges
of elitismr might note that the characters used by the OP are bog
standard and that you code is equally hard to read, in a different
way. Why not simply suggest that he use existing routines without
inventing ludicrous motives for the code he wrote?

--
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not