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)
Related
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.
I try to convert the character "Friday, March 24, 2017 4:39:46 PM" into a date, but it does not work. dat.1 results in NA. Can anyone tell me, how to fix it.
dat <- c("Friday, March 24, 2017 4:39:46 PM")
dat.1 <- strptime(dat, format="%A, %B %d, %Y %I:%M:%S %p")
dat.1
Result
[1] NA
EDIT. K..pradeeep pointed out that the code is running on his pc. On my it does not work. I use a Macbook Air, OS X 10.12.3. Version:
platform x86_64-apple-darwin15.6.0
arch x86_64
os darwin15.6.0
system x86_64, darwin15.6.0
status
major 3
minor 4.0
year 2017
month 04
day 21
svn rev 72570
language R
version.string R version 3.4.0 (2017-04-21)
nickname You Stupid Darkness
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+.
I recently stumbled upon a cool feature in CVS where you can name revisions by date, e.g.:
# List changes made between the latest revision 24 hours ago and now
cvs diff -D "1 day ago"
Do any other repository systems (e.g. Git, SVN, Bazaar, Mercurial, etc.) have an option like this?
Subversion has a similar feature. For example:
svn diff -r {2010-07-31}
The syntax is explained in http://svnbook.red-bean.com/en/1.5/svn.tour.revs.specifiers.html#svn.tour.revs.dates
Mercurial has a wide range of date formats: http://www.selenic.com/mercurial/hg.1.html#date-formats, though maybe not "1 day ago".
This subversion bug report indicates that Subversion can't do it natively, but does offer a tip on using date to do it:
(2) Whilst Subversion doesn't understand -r "{3 days ago}", date can
help out there too: -r "{date -Is -d '3 days ago'}".
(answering my own question)
git log supports dates for filtering before or after given times. Example:
git log --after='july 17 2010' --before='july 31 2010'
Here's a shell script that makes it a little easier to list ranges of commits, but it also uses a terser format than git log's default:
#!/bin/sh
# git-changes
FORMAT='%cd%x09%h%n%x09%s%n'
CMD="git log --format=format:$FORMAT"
case $# in
0 )
$CMD ;;
1 )
$CMD "--after=`date -d "$1"`" ;;
2 )
$CMD "--after=`date -d "$1"`" --before="`date -d "$2"`";;
esac
Note: I wrapped the date arguments with the date command, since git treats 'July 17' as a few hours off from 'July 17 2010' for some reason.
Usage:
git-changes # Same as git log, but more terse
git-changes 'yesterday' # List all commits from 24 hours ago to now
git-changes 'jul 17' 'aug 1' # List all commits after July 17 at midnight
# and before August 1 at midnight.
Sample output of git-changes 'jul 17' 'aug 1':
Sat Jul 31 23:43:47 2010 -0400 86a6727
* Moved libcurl into project directory as static lib.
Sat Jul 31 20:04:24 2010 -0400 3a4eb10
* Added configuration file support.
Sat Jul 31 17:44:53 2010 -0400 aa2046b
* Fixed truncation bug in bit parser.
Sat Jul 17 00:10:57 2010 -0400 99e8124
* Added support for more bits.
Now, to see all changes introduced by commit 99e8124, type git show 99e8124. To see all changes since revision 99e8124 (not including that commit itself), type git diff 99e8124.