Home > Archive > PERL CGI Beginners > August 2007 > Adding a comma to format localtime
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Adding a comma to format localtime
|
|
| Gregg O'Donnell 2007-04-09, 9:55 pm |
| All,
I use this line of code:
my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
To create this result:
Mon 9 Apr 2007 09:15:05
How can I add a comma to this result to get:
Mon, 9 Apr 2007 09:15:05
Best and thanks,
Gregg
---------------------------------
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
| |
| Wiggins d'Anconia 2007-04-09, 9:55 pm |
| Gregg O'Donnell wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
my $localtime = [ split ' ', localtime ];
my $datetime = $localtime->[0] . ', ' . join ' ', @$localtime[2,1,4,3];
TMTOWTDI...
http://danconia.org
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
>
> Best and thanks,
> Gregg
>
| |
| Paul Lalli 2007-04-09, 9:55 pm |
| On Apr 9, 12:41 pm, gpod...@yahoo.com (Gregg O'Donnell) wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
You're better off using modules the way they were designed to be used,
rather than hacks like the above.
use POSIX qw/strftime/;
my $datetime = strftime("%a, %e %b %Y %T", localtime);
See also:
man strftime
perldoc POSIX
Paul Lalli
| |
| Purl Gurl 2007-04-09, 9:55 pm |
| Paul Lalli wrote:
> Gregg O'Donnell wrote:
[color=darkred]
[color=darkred]
[color=darkred]
> You're better off using modules the way they were designed to be used,
> rather than hacks like the above.
> use POSIX qw/strftime/;
> my $datetime = strftime("%a, %e %b %Y %T", localtime);
Better off using a module?
#!perl
$datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
print $datetime;
print "\n\n";
use POSIX qw/strftime/;
$datetime = strftime("%a, %e %b %Y %T", localtime);
print $datetime;
PRINTED RESULTS:
Mon 9 Apr 2007 13:21:25
Mon, Apr 2007
Purl Gurl
| |
| Purl Gurl 2007-04-09, 9:55 pm |
| Gregg O'Donnell wrote:
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
> To create this result:
> Mon 9 Apr 2007 09:15:05
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
#!perl
$datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
substr ($datetime, 3, 0, ",");
print $datetime;
PRINTED RESULTS:
Mon, 9 Apr 2007 13:34:01
Purl Gurl
| |
| John W. Krahn 2007-04-11, 9:55 pm |
| Gregg O'Donnell wrote:
>
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
( my $datetime = localtime ) =~ s{(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)}
{$1, $3 $2 $5 $4};
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Susheel Koushik 2007-04-12, 3:55 am |
| use this:
$str = localtime;
@fields = split/ /,$str;
$str2 = "$fields[0]".","." $fields[2]"." $fields[1]"." $fields[4]"."
$fields[3]";
print ("$str2\n");
On 4/9/07, Gregg O'Donnell <gpod363@yahoo.com> wrote:
> All,
> I use this line of code:
> my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3];
>
> To create this result:
> Mon 9 Apr 2007 09:15:05
>
> How can I add a comma to this result to get:
> Mon, 9 Apr 2007 09:15:05
>
> Best and thanks,
> Gregg
>
>
> ---------------------------------
> Looking for earth-friendly autos?
> Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
| |
| Paul Archer 2007-04-12, 7:55 am |
| A cleaner way to do it is:
printf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
Or, if you want to save it to a variable:
my $dateout = sprintf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
Paul
11:57am, Susheel Koushik wrote:
> use this:
>
> $str = localtime;
> @fields = split/ /,$str;
> $str2 = "$fields[0]".","." $fields[2]"." $fields[1]"." $fields[4]"."
> $fields[3]";
> print ("$str2\n");
>
> On 4/9/07, Gregg O'Donnell <gpod363@yahoo.com> wrote:
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> http://learn.perl.org/
>
>
>
-------------------------------------------------------------------
Perl elegant? Perl is like your grandfather's garage. Sure, he kept
most of it tidy to please your grandmother but there was always one
corner where you could find the most amazing junk. And some days,
when you were particularly lucky, he'd show you how it worked.
----------Shawn Corey <shawn.corey [at] sympatico.ca>--------------
| |
| Peter Scott 2007-04-12, 6:55 pm |
| On Thu, 12 Apr 2007 05:56:14 -0500, Paul Archer wrote:[color=darkred]
> A cleaner way to do it is:
> printf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
> Or, if you want to save it to a variable:
> my $dateout = sprintf "%s, %s %s %s %s\n", (split ' ', localtime)[0,2,1,4,3];
Personally, I find this more maintainable and readable:
use POSIX qw(strftime);
my $datetime = strftime "%a, %d %b %Y %T", localtime;
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
| |
|
|
|
|
|
|
|
|
|
|
|