Re: Converting dates with Date:Format
by huck (Prior) on Jan 10, 2017 at 07:17 UTC
|
Could it just be that you assign to $test but print $text?
use strict; use warnings; is your friend!
| [reply] |
|
Ah man, that wouldn't help! :)
It not does something, but I get:
1900-16-20 03:11
So I must be doing something else wrong?
Also, I can't enable strict on this script, as its a very old one, and will break in every way imaginable (they don't use "my" deceleration's, apart from the code I have added in)
Thanks!
Andy
| [reply] [d/l] |
Re: Converting dates with Date:Format
by soonix (Chancellor) on Jan 10, 2017 at 08:08 UTC
|
Which strftime do you use? I get Undefined subroutine &main::strftime called at ...
or, with use POSIX; I getUsage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1
+, yday = -1, isdst = -1) at ...
I doubt 2008 is a valid number of seconds ;) | [reply] [d/l] [select] |
|
You - I misunderstood what was meant to be passed in (I assumed it was the values of the format you defined, rather than a localtime() array)
| [reply] |
Re: Converting dates with Date:Format
by Anonymous Monk on Jan 10, 2017 at 07:31 UTC
|
Strftime takes the return value of localtime function which tmpdate doesnt match | [reply] |
|
Ahhh ok - yeah that makes sense. Is there a better way to change from one format to another? Seems a bit long winded to have to convert one string into a localtime(), then to epoch, and then back again into another format
Thanks!
| [reply] |
|
use Time::Piece;
my $t = Time::Piece->strptime(
'2008-11-03 20:15:44',"%Y-%m-%d %H:%M:%S"
);
print $t->strftime("%a, %d %b %Y %H:%M:%S %Z");
poj | [reply] [d/l] |
|
Re: Converting dates with Date:Format
by 1nickt (Canon) on Jan 10, 2017 at 11:15 UTC
|
For completeness: DateTime:
(Note that your input has no timezone info, so you can't use %Z to print the timezone with accuracy. Also note that you have two spaces in your input, so the solution reflects that.)
use DateTime::Format::Strptime;
my $formatter = DateTime::Format::Strptime->new(pattern => "%F %T");
my $dt_obj = $formatter->parse_datetime("2008-11-03 19:03:44");
my $datetime = $dt_obj->strftime("%a, %d %b %Y %T EST");
Or, more succinctly as a one-liner:
perl -ML -E'say DateTime::Format::Strptime->new(pattern => "%F %T")->
parse_datetime("2008-11-03 19:03:44")->strftime("%a,
+%d %b %Y %T EST");
'
Output:
Mon, 03 Nov 2008 19:03:44 EST
Hope this helps!
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
Hi 1nickt,
my $datetime = $dt_obj->strftime("%a, %d %b %Y %T EST");
I wouldn't hardcode the timezone into the format string like that, since technically the object's time zone is in DateTime's special "floating" zone, and that's what'd be output if you use %Z (which I personally would still do). To assume a time zone for the input data instead use time_zone=>'EST' in the DateTime::Format::Strptime constructor:
use DateTime::Format::Strptime;
my $formatter = DateTime::Format::Strptime->new(pattern => "%F %T",ti
+me_zone=>'EST');
my $dt_obj = $formatter->parse_datetime("2008-11-03 19:03:44");
print $dt_obj->strftime("%a, %d %b %Y %T %Z"), "\n";
# to convert to a different zone:
$dt_obj->set_time_zone('America/Chicago');
print $dt_obj->strftime("%a, %d %b %Y %T %Z"), "\n";
__END__
Mon, 03 Nov 2008 19:03:44 EST
Mon, 03 Nov 2008 18:03:44 CST
Regards, -- Hauke D | [reply] [d/l] [select] |
|
Hi haukex,
Your suggestion is quite correct and a much more complete treatment of the problem (as is your way!).
My choice of using the string was intended to show precisely that since the provided input data lacked any TZ (and %Z returns the literal string "floating" in that case), no meaningful TZ info could be provided by the DateTime object, so you'd have to just add it as a string. Of course I didn't explain any of that :(
If, as is I guess most likely, the OP has datetimes lacking a TZ, and is himself located in Eastern Time Zone, his TZ will shift during the year, so while it may be EST today it could be EDT in six months. I think the best of all in his case may be to add the time_zone param in the constructor as you say, but provide the value "local".
nick@vagrant$ perl -Mstrict -Mwarnings -ML -E'
say DateTime::Format::Strptime->new(pattern => "%F %T", time_zone =>
+"local")->
parse_datetime("2008-11-03 19:03:44")->
strftime("%a, %d %b %Y %T %Z");
'
Mon, 03 Nov 2008 19:03:44 EST
(now you know where that vagrant lives!)
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|