Solved

Perl using time() fails

Posted on 2013-01-04
6
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:eluka68
ID: 38745743
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
ID: 38745835
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
ID: 38745842
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 78

Expert Comment

by:arnold
ID: 38747002
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
ID: 38766121
@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
ID: 38769533
print (qw(Su Mo Tu We Th Fr Sa)[(localtime)[6]]);
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Nee dhelp identifying the OS and how to access 7 93
Krita v3 Linux Mint/Ubuntu 16.04 9 76
how to write and save a unix script 12 39
powershell script error 2 36
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

751 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