Perl using time() fails

Posted on 2013-01-04
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

./ line 6: syntax error near unexpected token `('
./ 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";
Question by:eluka68

Author Comment

ID: 38745743
I busted
THe first line should have been

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

Accepted Solution

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;


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

LVL 48

Assisted Solution

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 77

Expert Comment

ID: 38747002
Sunday is 0.
You can define an array ahead of time and the
Doing the same for months will reduce the overhead of including/referencing other functions.
LVL 51

Expert Comment

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

Expert Comment

ID: 38769533
print (qw(Su Mo Tu We Th Fr Sa)[(localtime)[6]]);

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Backup & Restore 3 54
Set robocopy to exclude "System Volume Information" folder 3 53
Run DOS2UNIX and then execute the command 21 70
awk sed 8 40
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

785 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