I have a string of epoch seconds "1510652305" which when i convert to normal time on unix command line using
`date -d #1510652305`
i get
Tue Nov 14 15:08:25 IST 2017
But when i tried it in perl using something like this
use POSIX qw(strftime);
use Time::Local;
use Time::localtime;
$kickoff_time=1510652305;
$kickoff_time=ctime($kickoff_time);
i get
Thu Jan 1 05:30:00 1970
How can i achieve the result i am getting in linux in perl?
Thanks!!
Don't overthink it!
my $kickoff_time = localtime 1510652305;
say $kickoff_time; # Tue Nov 14 15:08:25 2017
If you absolutely, positively need the timezone in there:
use POSIX qw{strftime};
my $kickoff_time = strftime '%a %b %e %H:%M:%S %Z %Y', localtime 1510652305;
say $kickoff_time; # Tue Nov 14 15:08:25 IST 2017
Note that this is locale-dependent.
I would recommend using Time::Piece for this job - it's core in perl.
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = localtime ( 1510652305 );
print $t;
It'll print default format, or you can use formatted using strftime.
Related
I use mojolicious application. When I do logging all if fine, except when I run application under morbo I see text like:
$app->log->info('тест лога');
[Sat Oct 6 15:22:43 2018] [info] �е�� лога
here is some problem with utf8.
What should I do that messages will be correctly displayed?
My terminal have support for utf8. I run Linux Mint v19.3
Here is how messages looks which come from script:
Test terminal:
Don't use the following when using Mojo::Log to write to STDERR:
binmode STDERR, ':encoding(UTF-8)';
Mojo::Log explicitly encodes everything it logs using UTF-8, even when writing to STDERR.
sub append {
my ($self, $msg) = #_;
return unless my $handle = $self->handle;
flock $handle, LOCK_EX;
$handle->print(encode('UTF-8', $msg)) or croak "Can't write to log: $!";
flock $handle, LOCK_UN;
}
When using STDERR as the log output (the default), this conflicts with the well-established practice of adding an encoding layer to STD*. In such a circumstance, double-encoding occurs.
One must therefore avoid doing
binmode STDERR, ':encoding(UTF-8)';
Note that this is done as part of
use open ':std', ':encoding(UTF-8)';
Try to run following code sample in your system. The test confirmed with correct UTF-8 output in terminal
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
get '/' => sub ($c) {
$c->render(text => 'Hello World!');
};
app->log->info('тест лога');
app->start;
Run as morbo test.pl produces following output
:u99257852:~/work/perl/mojo$ morbo ./test.pl
Web application available at http://127.0.0.1:3000
[2020-10-31 13:33:57.42056] [83940] [info] тест лога
[2020-10-31 13:35:16.72465] [83940] [debug] [hga9Tgyy] GET "/"
[2020-10-31 13:35:16.72528] [83940] [debug] [hga9Tgyy] Routing to a callback
[2020-10-31 13:35:16.72574] [83940] [debug] [hga9Tgyy] 200 OK (0.001078s, 927.644/s)
(uiserver):u99257852:~/work/perl/mojo$
Tested locally with nc localhost 3000
(uiserver):u99257852:~$ nc localhost 3000
GET / HTTP/1.1
HTTP/1.1 200 OK
Date: Sat, 31 Oct 2020 17:35:16 GMT
Server: Mojolicious (Perl)
Content-Type: text/html;charset=UTF-8
Content-Length: 12
Hello World!
Output of uname -a
(uiserver):u99257852:~/work/perl/mojo$ uname -a
Linux infongwp-us19 4.4.223-icpu-044 #2 SMP Sun May 10 11:26:44 UTC 2020 x86_64 GNU/Linux
(uiserver):u99257852:~/work/perl/mojo$
Bash is user's shell configured with ~/.bashrc with following settings to support UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8:
I have problem formatting and dealing with dates before the epoch 1/1/1970 in Perl, dates comes back as negative integer:
my $time=timelocal(0, 0, 0, 1, 1, 1969);
print "$time\n";
$theTime = localtime($time);
print "the time is good: $theTime\n\n";
How to deal with dates before the epoch in Perl, both on unix and windows have the same problem Perl 5.8.8. PHP shows the date normal without problems.
have you tried using DateTime?
If perl print a negative integer, this is the good behaviour since 01/01/1970 is the zero day of this date format. Search the word negative on https://en.wikipedia.org/wiki/Unix_epoch
An example in shell :
$ date -d "1957-10-04T00:00:00Z" +%s
-386380800
This is correct.
I need is to pass this negative number to localtime or some other function to return formated date time array.
Ok, what's stopping you?
# ActivePerl on Windows
>perl -E"say ''.localtime(-386380800)"
Thu Oct 3 20:00:00 1957
# Linux
$ perl -E'say "".localtime(-386380800)'
Thu Oct 3 16:00:00 1957
# Cygwin
$ perl -E'say "".localtime(-386380800)'
Thu Oct 3 16:00:00 1957
I want to log file to display date as %a %b %d %H:%M:%S.%3N.
$ date '+%a %b %d %H:%M:%S.%3N'
Mon Aug 27 12:15:36.954
I already found that because of a collision I needed to set log-date = %%a %%b %%d %%H:%%M:%%S.
But can't get the milliseconds nor nanoseconds to work...
strftime() does not support milliseconds nor nanoseconds.
You have to rely on lower-level systems:
uWSGI LogFormat
Unfortunately this requires uWSGI v1.3+.
My hosting provider has recently upgraded their servers and I am having lots of problems with imagemagick scripts in perl. My script worked perfectly on the old server but fail on the new one so I have gone back to basics to try and sort out what is going wrong.
The server reports the imagemagick as:
Version: ImageMagick 6.7.2-2 2011-10-20 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP
and the perl module Image::Magick is version 6.72
The following script is saved on my server:
#!/usr/bin/perl
use CGI::Carp qw( fatalsToBrowser );
use Image::Magick;
my $image = Image::Magick->new;
$x = $image -> Set(size=>"200x200");
warn "$x" if "$x";
$x = $image -> ReadImage("canvas:black");
warn "$x" if "$x";
$x = $image -> Draw (
stroke => "red",
primitive => "line",
points => "20,20 180,180");
warn "$x" if "$x";
print "Content-type: image/gif\n\n";
binmode STDOUT;
$image -> write ("gif:-");
This fails with the following errors:
[Sun Oct 23 11:02:32 2011] imtest.pl: Exception 420: no decode delegate for this image format `lack' # error/constitute.c/ReadImage/532 at www/11/cgi-bin/imtest.pl line 12.
[Sun Oct 23 11:02:32 2011] imtest.pl: Exception 410: no images defined `Draw' # error/Magick.xs/XS_Image__Magick_Mogrify/7394 at www/11/cgi-bin/imtest.pl line 18.
If I change ReadImage("canvas:black") to ReadImage("xc:black") then the script runs continuously with no output.
My webhost has been great in trying to find a solution but I need to know if I am doing something wrong here, or if there is an installation problem with imagemagick.
Please note I realise the above can be done with other simpler modules but this is just a simple example to determine if the problem is imagemagick or my code!
Thanks for your help.
Regards,
Stu
I received similar errors in winxp command line mode with version 6.3.7 of ImageMagick.
I changed the first few lines to this and it worked:
my $image = Image::Magick->new(size=>"200x200");
die "Image::Magick->new failed" unless $image;
my $x = $image->Read("xc:black");
warn "$x" if "$x";
in Perl
how do I get current London time? GMT has offset with BST
is it possible to get the TZ offset or the time itself for a different timezone
from a NY Red Hat Linux machine?
-Thanks much
use DateTime;
say "UTC:\t\t" . DateTime->now->iso8601;
say "London time:\t" . DateTime->now->set_time_zone("Europe/London")->iso8601;
UTC: 2011-04-24T17:45:16
London time: 2011-04-24T18:45:16
Use the DateTime modules. They have all sorts of date and time calculation tools, including timezone support.
The Time Zones FAQ will get you started on common timezone issues.
In standard Perl:
% env TZ=Europe/London date
Sun Apr 24 19:51:24 BST 2011
% env TZ=Europe/London perl -le 'print scalar localtime'
Sun Apr 24 19:51:37 2011
% env TZ=Europe/London perl -MPOSIX=strftime -le 'print strftime "%x %X %Z (%z)", localtime'
04/24/11 19:51:52 BST (+0100)