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+.
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 just encountered some strange behavior with Perl 5.16.3 on FreeBSD 9.3-RELEASE-p3. We've got a cron job which runs every five minutes and generates some text status files. I just happened to list the contents of the output directory and saw that the timestamps for some of the files were in the future! The files are created like this:
if (open(OUT, "> $status_file_path")) {
print OUT "$status_info\n";
close OUT;
}
Now, the file handle OUT is used in several places, however it is opened and closed within the same block as shown above. And like I said, out of ten files, only a few had future dates when displayed using ls.
For example, files with the current date had timestamps like 04/02/2015 20:29:46, files with future timestamps were out in November, e.g. 11/10/2015 09:38:41.
What might be going on here?
EDIT
I've got two tests running:
1) a perl script running a loop of 1000 iterations, sleeping a random time up to 10 seconds between iterations, using the open/print/close logic to create an output file and abort the script if the file's modification time is in the future.
2) a cron entry to touch a test file every minute, e.g. touch /home/test/test_file_date_with_cron.txt
TEST RESULTS
Neither of the tests generated output files with a timestamp in the future.
This is scary.
EDIT 2
Here is the filesystem info, the files are written in the /usr directory.
# df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/gpt/gprootfs 2G 133M 1.7G 7% /
devfs 1.0k 1.0k 0B 100% /dev
/dev/gpt/gpusrfs 431G 3.8G 392G 1% /usr
procfs 4.0k 4.0k 0B 100% /proc
EDIT 3
Running the script outside of cron for several hundred iterations didn't duplicate the problem. HOWEVER, I just found some other files, which are created by a CGI script which have the future dates:
-rw-r--r-- 1 test test 5783 Nov 10 2015 Config.xml_20150210_104151
-rw-r--r-- 1 test test 34548 Nov 10 2015 Config2.xml_20150210_104151
-rw-r--r-- 1 test test 6105 Nov 10 2015 Config.xml_20151109_232210
-rw-r--r-- 1 test test 34554 Nov 10 2015 Config2.xml_20151109_232210
-rw-rw-r-- 1 root test 2075 Nov 9 2015 Config.xml_20151109_231055
-rw-rw-r-- 1 root test 1232 Nov 9 2015 Config2.xml_20151109_231055
These are archive files, which get moved and renamed with the file's mtime timestamp. Note that BOTH ls and Perl's stat() function report the future date -- stat() is used to generate the file's timestamp portion of the name.
Looking at the first entry, ls reports "Nov 10 2015", whereas when the CGI script processed it, Perl's stat() reported "20150210_104151", i.e. "Feb 02 2015" which is most likely correct.
Further down, we see ls showing "Nov 10 2015" and stat() reported "20151109_232210", i.e. "Nov 09 2015".
Finding those additional archived config files helped me track down the cause, which was as others have suggested, that the system date and timezone changed.
From: 1447147328 and America/Adak
To: 1426637771 and America/New_York
What was throwing me off, was that I thought the cron script wrote ALL of the output files each time it executes, but that's not the case. The files have different "refresh intervals".
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
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)
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.