View Category

Parse a date and time from a string

Given the string "2008-05-06 13:29", parse it as a date representing 6th March, 2008 1:29:00pm in the local time zone.
perl
#! /usr/bin/perl
# -*- Mode: CPerl -*-

use strict;
use POSIX;

# Given the string "2008-05-06 13:29", parse it as a date
# representing 6th March, 2008 1:29:00pm in the local time zone.

my $ds = "2008-05-06 13:29";

my $y;
my $m;
my $d;
my $hr;
my $mn;

print "Original: ",$ds,"\n";

if ( $ds =~ /(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2})/ ){
$y = $1 - 1900;
$m = $2;
$d = $3;
$hr = $4;
$mn = $5;

printf "Nominal: %s\n",
strftime("%e %B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);

my $eth = "";
if ( $d == 1 ){
$eth = "st";
} elsif ( $d == 2 ){
$eth = "nd";
} elsif ( $d == 3 ){
$eth = "rd";
} else {
$eth = "th";
}

printf "As required: %d%s %s\n",$d,$eth,
strftime("%B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);
}

#eos
# Shurely you mean 6th MAY? If not, oh well
use Time::Piece;

my $dt_str = '2008-05-06 13:29';
my $tp = Time::Piece->strptime( $dt_str, '%Y-%m-%d %H:%M');
print $tp,"\n";

Display information about a date

Display the day of month, day of year, month name and day name of the day 8 days from now.
perl
#! /usr/bin/perl
# -*- Mode: CPerl -*-

use strict;
use Date::Calc qw(:all);

my $days_in_future = $ARGV[0];
$days_in_future = 8 unless $days_in_future;

my ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = Localtime();

my ($fyear,$fmonth,$fday) = Add_Delta_Days($year,$month,$day,$days_in_future);

printf "Now: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$year,$month,$day,$hour,$min,$sec;

printf "Then: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$fyear,$fmonth,$fday,$hour,$min,$sec;

printf "Then: day of month: %d\n",$fday;
printf "Then: day of year: %d\n",Day_of_Year($fyear,$fmonth,$fday);
printf "Then: day of name: %s\n",
Day_of_Week_to_Text(Day_of_Week($fyear,$fmonth,$fday));
printf "Then: month name: %s\n",Month_to_Text($fmonth);

#eos
use Time::Piece;
use Time::Seconds;

my $t = localtime;
my $t_8 = $t + (ONE_DAY * 8);

printf "Now: %d, %d, %s, %s\n",
$t->day_of_month, $t->day_of_year, $t->fullmonth, $t->fullday;

printf "Then: %d, %d, %s, %s\n",
$t_8->day_of_month, $t_8->day_of_year, $t_8->fullmonth, $t_8->fullday;

Display a date in different locales

Display a language/locale friendly version of New Year's Day for 2009 for several languages/locales. E.g. for languages English, French, German, Italian, Dutch the output might be something like:

Thursday, January 1, 2009
jeudi 1 janvier 2009
giovedì 1 gennaio 2009
Donnerstag, 1. Januar 2009
donderdag 1 januari 2009

(Indicate in comments where possible if any language specific or operating system configuration needs to be in place.)
perl
#!/usr/bin/perl
use warnings;
use strict;

use locale;
use POSIX qw(strftime);
use Time::Local;

my $date=timegm(0,0,0, 1,0,101); #00:00:00 01/01/2001

my $str_time = strftime "%c", gmtime;

print "Date: $str_time\n";

Display the current date and time

Create a Date object representing the current date and time. Print it out.
If you can also do this without creating a Date object you can show that too.
perl
use Class::Date;
my $date = Class::Date->now();
print $date->string()."\n";

print localtime()."\n";
use Time::Piece ();

# Date object
my $date = Time::Piece::localtime;
print "$date\n";
# no object
print scalar(localtime),"\n";