Solved

Perl using time() fails

Posted on 2013-01-04
6
434 Views
Last Modified: 2013-01-24
I am getting these erros... Trying simple code to work with time

I don't know what is wrong... I need to do simple date math code

./dateExample.pl: line 6: syntax error near unexpected token `('
./dateExample.pl: line 6: `my $time = time();'


Code line 6  is
my $time = time();


my code
===============


#/app/cawily/support/perl/bin/perl -w



my $time = time();


my $future_time = $time + (45 * 24 * 60 *60);
my ($second, $minute, $hour, $day, $month, $year, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime($time);
my ($fsecond, $fminute, $fhour, $fday, $fmonth, $fyear, $fdayOfWeek, $fdayOfYear, $fdaylightSavings) = localtime($future_time);

$year += 1900;
$fyear += 1900;

print "$month/$day/$year\n";
print "$fmonth/$fday/$year\n";
0
Comment
Question by:eluka68
6 Comments
 

Author Comment

by:eluka68
Comment Utility
I busted
THe first line should have been
#!/app/cawily/support/perl/bin/perl


But let me modify the Question.... I need help getting to know what day of the week it is (MON, Tuesday and so forth)
0
 
LVL 10

Accepted Solution

by:
tdlewis earned 250 total points
Comment Utility
Your $dayOfWeek variable tells you what day of the week it is. Here is a slightly modified version of your code that adjusts for zero-based months.
#!/app/cawily/support/perl/bin/perl -w

my $time = time();

my $future_time = $time + (45 * 24 * 60 *60);
my ($second, $minute, $hour, $day, $month, $year, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime($time);
my ($fsecond, $fminute, $fhour, $fday, $fmonth, $fyear, $fdayOfWeek, $fdayOfYear, $fdaylightSavings) = localtime($future_time);

$year += 1900;
$fyear += 1900;

$month++;
$fmonth++;

my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

print "$days[$dayOfWeek] $month/$day/$year\n";
print "$days[$fdayOfWeek] $fmonth/$fday/$year\n";

Open in new window

0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
Comment Utility
Much easier to do

#!/app/cawily/support/perl/bin/perl -w
use POSIX 'strftime';
my $day = strftime '%a',localtime;
print "day is $day\n";

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 76

Expert Comment

by:arnold
Comment Utility
Sunday is 0.
You can define an array ahead of time and the
@dayofweek=("Sun","Mon",);
Etc.
Doing the same for months will reduce the overhead of including/referencing other functions.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
@d=(qw(So Mo Tu We Th Fr Sa));@t=localtime;print $d[$t[6]];
# to be improved in many ways
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
print (qw(Su Mo Tu We Th Fr Sa)[(localtime)[6]]);
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now