LanguagesPerlDates and Times

Dates and Times content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

When I first started putting up JavaScripts, and now this PERL series, one of the big things people want is the ability to post the date and the time. No sweat. This primer is dedicated to just that. I’ll tell you that it’s little rogher than you might think, but stay with me. We’ll get the dates and times up and running.

Display the date and time

See the time: See the Script

(This will open in a new window):
Here’s the code

The Funny Thing About Dates and Times

The funny thing about dates and times, and one of the hardest things to get across to students is that computer don’t see dates and times like you do. Computers see only numbers. You have to force a computer to display text. Furthermore, computers always start their number counts at zero. If the days of the week start with Sunday, then a computer sees Sunday as day zero. That sort off puts computer returns off by one.

Further furthermore, this doesn’t hold true across elements. For instance, it’s true for days of the week, months, and the hour of the day, but not for day of the month, minute or second. Confused?

Take heart. I put together the format I’m about to give you a good six months ago to be able to collect and time stamp some data. i wanted to put together a module I could simply copy and paste into a PERL script and gets and date or time element I wanted…and for that element to be correct.

The script you should now have open in that second window is what I came up with. let’s look at it.

The Script

Every element of time is contained within a the command (function really) “localtime”. What you basically do is set up some code to pluck out just what you want from localtime. In my code I grab these elements:

  • $sec (seconds)
  • $min (minutes)
  • $hour (the hour, 00-24)
  • $mday (day of the month)
  • $mon (month, 0-11)
  • $year (year, 2000 is “100”)
  • $wday (day of the week, 1 – 7)
  • $yday (day of the year, 1-365 (6))

I have assigned each of those elements to localtime(time).

Next I have three arrays, one for the hour, the month, and the day. This is how I force numbers to become text.

The format is simple and cannot be changed:

  • A scalar variable (with the $ sign) is assigned the value of the array.
  • The array elements are within (parentheses) separated by commas, no spaces.
  • Next, square brackets denote where the number return for the array will come from (localtime)
  • Finally, the number in the second set of brackets denotes the item in the first array ($sec,$min,$hour, etc.)

Let me actually explain further by using the third array first. That’s the one that represents “$thisday”.

I have an array of the days of the week starting with Sunday. I know a number, zero through six, will be returned depending on the day of the week. I also know zero is Sunday. That’s why I started with Sunday.

The return will come from (localtime) and I want the localtime array number 6. If you count the items in the localtime array left to right, starting at zero, you get $wday, the day of the week.

The day I wrote this tutorial was Thursday. The return from “$wday” was four. Count over zero, one, teo, three, four in the days array, you get Thursday.

Thursday is returned.

It’s the same story with the hours and the months. The reason I did the hours was that the hours come back in military time. By putting in an array 1 to 12, 1 to 12, I get turn the 24 hour clock into a 12 hour return. get it.

OK, that’s the thing I came up with (just those four lines) that I simply copy and paste into any PERL script if I want any date or time element. With those things in place, just under the PERL path, I can call on what I want.

By the way, the reason I only have those four arrays is that those are the only four elements that need them. Everything else returns correct numbers…except that $year, but we’ll get to that in a minute.

Print It

These three lines print the date and time you saw in the example:

print "Today is $thisday, $thismonth, $mday, 200";
print $year-100;
print ", $thishour:$min:$sec ";

The first line used the array returns “$thisday” and “$thismonth”. The day of the month, “$mday”, returns correctly. It doesn’t need an array.

That year’s a pesky thing though. The variable “$year” returns 100. It’s a Y2K thing believe it or don’t. I tried setting up an array, but that would mean I would need 99 aray items before I got to what I wanted. No thanks. I just did some simple math.

I only want a single number return. That’s why I have “200” waiting there. I’m just going to add the last digit later.

The next line of the code gets that last digit for me. I just took the $year and took away 100. That’s zero. Add that zero to the end of the 200 and you get 2000.

We’ll get into math inthe next primer, but real quick take a look at that line I just described. I do not want text returned from that line. I want the result of that math. To get a result rather than a simple printing, lose the double quotes. That’s all.

Getting back to printing the time, the next line uses the “$thishour” array return and the “$min” and “$sec” round it out with colons between each return.

Ta da! You have the time.

The next little blip of PERL looks at the return from “$hour” . Get that? It uses the return from “$hour”. Not the “$thishour” return.

if($hour > 11 && $hour < 18)
{print " PM. Good Afternoon.";}
elsif($hour > 17 && &hour < 24)
{print "PM. Good Evening.";}
{print "AM. Good Morning.";}

First we check to see if the return from “$hour” is greater than eleven AND (&&) less than 18. If so, print “Good Afternoon”.

If the return is greater than 17 but less than 24, print “Good Evening”. Otherwise, print “Good Morning”.

I wrap it up then by sticking in a couple of returns shoing day of the week and day of the year just to show you the returns, nothing more.

I’ll bet you can get this one. Set up a branch that acts as a reminder. Alter the script so that if it is past 6:30PM, a line will print to the page that it’s dinner time and the user should go and eat.

Primer Eight Assignment

Here’s a Possible Answer
(This will open in a new window)

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories