Software, your way.
burger menu icon
WillMaster

WillMasterBlog > Perl

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Seeing the Future with Perl

Adjusting time forwards (and backwards) is easy with Perl.

But before we start playing around with the future, let's determine exactly what time it is now — to the second (according to the server).

my ($second,
    $minute,
    $hour,
    $month_day,
    $month,
    $year,
    $week_day,
    $year_day,
    $is_DST) = localtime;

That snippet retrieves a lot of information about this moment. The same information can be retrieved for any future or past moment.

The $second and $minute variables contain a number 0-59, the $hour variable 0-23.

The $month_day variable contains the calendar day of the month (the number, not the day itself :).

The $month variable contains a number 0-11. January is 0 and December is 11.

The $year variable contains the current 4-digit year minus 1900. Thus, year 2005 would be represented as "105".

The $week_day variable contains a number 0-6. Sunday is 0, Monday is 1, and Saturday is 6.

The $year_day variable contains a count of the number of days elapsed since January 1 of the current year. January 1 is 0, January 2 is 1, January 31 is 30, February 1 is 31, February 2 is 32, and so forth.

The $is_DST variable is either empty or it contains a non-zero number (usually the digit 1). If it contains a non-zero number, it is Daylight Savings Time. If empty, it is not Daylight Savings Time.

Notice that the localtime function retrieves 9 items of information. If you only need only one item, the information can be retrieved independently.

The 9 items the localtime function retrieves can be numbered 0 through 8. Thus, the day of the week number would be number 6.

This retrieves the day of the week number:

my $WeekDay = (localtime)[6];

Now, let's see the future.

A future moment is determined by adding seconds to the current system time.

The current system time is retrieved with the time function.

This snippet adds a years worth of seconds to the current system time, then prints the day of the week for that future.

my $then = time;
$then += 60 * 60 * 24 * 365;
my $ThenWeekDay = (localtime($then))[6];
print $ThenWeekDay;

The value of the $now variable, which contains a system time number of a time in the future, is passed to the localtime function, which then returns the day of the week for that future time.

See how easy it is to see the future with Perl :)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Extracting Dates From the Unix Timestamp

The UNIX timestamp can be used to determine the represented time for any time zone on Earth.

Submit Form Data Without Bothering User

To silently and effectively submit a form on a web page, Ajax with FormData() can be used.

CSS Dots

CSS dots can be used for interactions between web page and website user.

Email Testing

One thing to test when email doesn't get sent or doesn't arrive is whether the server actuall sends email.

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC