View Full Version : localtime help
ediggity
01-01-2007, 05:15 PM
Hi everyone,
I have a calendar script, "calendar.pl" that parses a template and a data file "calendar_events.txt", which contains the events to output a calendar.
my code so far is:
# Get last modified time of the file
my $mtime = (stat("calendar_events.txt"))[9];
print localtime($mtime);
but I want to be able to control the format and subtract 3 hours from the time to account for my local time relative to my server time. The format I am looking for is: HH:mm AM Mon dd, yyyy or something close to it if it is not possible.
I don't have the Time::Format module installed on my host server by the way.
Any help would be greatly appreciated as I haven't worked on perl in a long time and so have stopped learning it. Thanks in advance
FishMonger
01-01-2007, 06:51 PM
The easiest method would be to use the strftime function from the POSIX module, which is a core module.
use POSIX qw/strftime/;
my $mtime = (stat("calendar_events.txt"))[9];
print strftime("%H:%M %p %b, %Y", localtime($mtime));
ediggity
01-01-2007, 08:10 PM
The easiest method would be to use the strftime function from the POSIX module, which is a core module.
use POSIX qw/strftime/;
my $mtime = (stat("calendar_events.txt"))[9];
print strftime("%H:%M %p %b, %Y", localtime($mtime));
If I wanted to correct the server time to account for my local time (minus 3 hours) would I do this:
use POSIX qw/strftime/;
my $mtime = (stat("calendar_events.txt"))[9];
print strftime("%H-3:%M %p %b, %Y", localtime($mtime));
or
use POSIX qw/strftime/;
my $mtime = (stat("calendar_events.txt"))[9];
print strftime("%H:%M %p %b, %Y", localtime($mtime)-3*24*60*60);
FishMonger
01-01-2007, 09:19 PM
That's almost correct, your math is a little off and I forgot to show how put in the day of the month.
print strftime("%H:%M %p %b %d, %Y", localtime($mtime - 3*60*60));
KevinADC
01-02-2007, 12:36 AM
same question answered on another forum.
FishMonger
01-02-2007, 05:20 AM
same question answered on another forum.
Kevin, I know that you have a problem with people posting questions in multiple forums, but I must disagree. If it takes multiple forums to get the correct answer, then so be it. However, it would be courteous for the poster to inform us that he/she has posted in other forums. In the future, can you cite the url's where you found the other postings?
Here's a case in point. I had a recent problem with the Net::SSH::Perl module and posted a question at EE, which has people that are far more knowledgeable in Perl than either you or I or anyone else in this forum. I also posted the exact same question on the module's mailing list. I received several responses from a couple of the top Perl experts at EE which boiled down to questions on my server config. The module's mailing list provided a quick and accurate answer; the problem I was experiencing was well known and was due to the fact that the module's CPAN distribution was missing several key dependencies. Once I installed those dependencies, the module worked as expected.
KevinADC
01-02-2007, 08:35 AM
That's cool, I respect your opinion concerning cross posting. I don't post links to the other forums out of respect to this forum.
ozo is the man over there, and you're not doing so bad yourself. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.