Perl help LocalTime in New Format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl help LocalTime in New Format
# 1  
Old 10-18-2011
Tools Perl help LocalTime in New Format

Hi,

I'm new to perl scripting and am trying it out.

I have a file written in the following format:

myfile-MMDDYY where MM is the number of the Month; DD the Day and YY the last two of the year... (Apologies for dumbing this down; I'm trying to be clear).

There is a new file put onto my server everyday by a vendor and I would like to log the fact that the file is present.

So when I grab today's date in Perl I get:
Code:
perl -e 'localtime()'

which presents the date in this format:
Code:
Tue Oct 18 15:25:47 2011


How would I go about grabbing the date in the other format from this line?

Thanks,

Last edited by Franklin52; 10-19-2011 at 03:52 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-18-2011
perl -e 'localtime()' prints nothing, and I wouldn't have expected it to. It doesn't print anything, just returns a list or scalar.

perldoc -f localtime has these suggestions for getting dates as you want:

Code:
                   use POSIX qw(strftime);
                   $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
                   # or for GMT formatted appropriately for your locale:
                   $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;

And suggests man strftime for an explanation of the strings strftime takes.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-18-2011
Thanks, that helps.

---------- Post updated at 07:18 PM ---------- Previous update was at 06:54 PM ----------

Code:
perl -MPOSIX -e 'print POSIX::strftime("%m%d%y",localtime),"\n"'


Last edited by Franklin52; 10-19-2011 at 03:52 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

TZ, localtime and strftime problem on AIX and Solaris

Hello all, I have the following code that seems to be misbehaving depending on the timezone setting (TZ Environment variable). It gives the correct value when TZ is in POSIX format and the wrong value when in OLSON format. #include <stdio.h> #include <stdlib.h> #include <time.h> #include... (6 Replies)
Discussion started by: biju64
6 Replies

2. Shell Programming and Scripting

How to use a variable in perl localtime()?

Hi all, a=$1 ## b=`echo "86400 * $a"|bc` `perl -e 'use POSIX qw(strftime);$now_string = strftime "%d/%m/%Y", localtime(time-$b); print $now_string,"\n";' > date_file` but im always getting current date; can any one suggest me any the improvement the above works fine if i use some thing... (2 Replies)
Discussion started by: zozoo
2 Replies

3. Shell Programming and Scripting

perl module to convert xlsx format to xls format

Hi Folks, I have written a perl script that reads data from excel sheet(.xls) using Spreadsheet::ParseExcel module. But the problem is this module doesn't work for excel sheets with extension .xlsx. I have gone through Spreadsheet::XLSX module with which we can read from .xlsx file directly.... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

Pack and unpack localtime in perl script

Hi I have a code like this: sub WriteEbcdicHeader { my $Htimestamp=localtime();#i need to pack and unpack this my $eheaderline = $Htimestamp; #packing has to be done here #unpacking has to be done after packing print $EOUTFILE return $eheaderline; } sub WriteEbcdicTrailer { ... (5 Replies)
Discussion started by: rbathena
5 Replies

5. Shell Programming and Scripting

what is the default return type of localtime() in perl?

Hi, I have given like this to get the time of the sub routine. my $start = localtime(); print "\n start time: $start \n"; Output start time: Fri Apr 29 01:01:31 2011 I want to know what is the format of the time. I am not able to follow is is HH:MM:SS or MM:HH:SS os... (2 Replies)
Discussion started by: vanitham
2 Replies

6. Shell Programming and Scripting

date with perl localtime

Hi Experts, I know how to handle normal date changes in perl. Most of my requirement are full filled with following: $date1 = strftime "%Y%m%d",localtime; $date2 = strftime "%Y%m%d",localtime(time -24 * 60 * 60); $date3 = strftime "%Y%m%d",localtime(time +24 * 60 * 60); $date4 = strftime... (4 Replies)
Discussion started by: mtomar
4 Replies

7. Shell Programming and Scripting

PERL localtime Warning

Hello All, I am facing a warning "Argument "" isn't numeric in localtime at" what i m using is below my $timestamp = Timestamp(time); go_log("###############$timestamp###############"); can some one please suggest the way to avoid this message :confused: (6 Replies)
Discussion started by: NIMISH AGARWAL
6 Replies

8. HP-UX

sleep & localtime query

Hi, I work on HPUX application and i had a query regarding the sleep & localtime system call in HPUX. Here is the code that we have : const uint32_t WAKE_INTERVAL_SEC(30*60); while (true) { // Find out what time it is time(&currentTime);... (3 Replies)
Discussion started by: nsvora
3 Replies

9. UNIX for Advanced & Expert Users

converting localtime to unixtime

hi, how to convert the localtime to unixtime? i have date from the date command in unix i want to convert it into unixtime thnx (2 Replies)
Discussion started by: AshishK
2 Replies

10. Shell Programming and Scripting

Perl + localtime()

ok here is a perl date question not asked befor. i know i am feeling small for not knowing. BUT!!!! $ENV{TZ}="US/Central"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(); how can i do the addition to year so i can get the current year w/o going $ntime=$year+1900;... (3 Replies)
Discussion started by: Optimus_P
3 Replies
Login or Register to Ask a Question