1 #!/usr/bin/perl -w
2 # File: date-time.pl
3 # Author: Tom Large - tlarge@thelug.org
4 # Location: http://www.thelug.org/docs/
5 # Description: Simple script to demonstrate different
6 # ways to acquire the date and time, and manipulate it.
7
8 # Acquire individual variables for the entries in localtime from EPOCH.
9 my ($sec, $min, $hour, $day, $month, $year, $wday, $yday) = (localtime)[0,1,2,3,4,5,6,7];
10 $month = $month+1; # Month needs to have 1 added to it.
11 $year = $year+1900; # Year needs to have 1900 added to it.
12
13 # An example of how to combine into one variable.
14 $today = "$month\/$day\/$year";
15 print "$today\n";
16
17 # How to convert the decimal response for day of week into days.
18 if ($wday eq 0){
19 ($wkday = "Sunday");}
20 if ($wday eq 1){
21 ($wkday = "Monday");}
22 if ($wday eq 2){
23 ($wkday = "Tuesday");}
24 if ($wday eq 3){
25 ($wkday = "Wednesday");}
26 if ($wday eq 4){
27 ($wkday = "Thursday");}
28 if ($wday eq 5){
29 ($wkday = "Friday");}
30 if ($wday eq 6){
31 ($wkday = "Saturday");}
32
33 print "$wday\n";
34
35 # How to format year as two digits.
36 if ($year =~ /\d\d(\d\d)/)
37 {
38 $twoyear = $1;
39 }
40
41 # How to print to different methods.
42 print "It is currently $hour:$min:$sec on $wkday\, $month-$day-$year\n";
43 print "$month-$day-$twoyear\n";
syntax highlighted by Code2HTML, v. 0.9.1