yapc

10
=How to get a date with Purl * Botsnacks * Ponys * Pants =How to get a date with Perl * Date * Time * Calendars =Outline * Built-in date/time functions * Date/Time modules * Module interoperability * How to do common date/time tasks * Reefknot * Ongoing development - how you can help = Built-in date/time functions: time * epoch time * Seconds since Jan 1, 1970 * Perhaps seconds since some other time * Easy to figure out differences in time (subtraction) * Easy to add to dates * Limited to range Jan 1 1970 - some time in 2038 =Built-in date/time functions: localtime() * Takes an epoch time as an argument * Defaults to current time

Upload: paul-duncan

Post on 30-Dec-2015

14 views

Category:

Documents


0 download

DESCRIPTION

- PowerPoint PPT Presentation

TRANSCRIPT

Page 1: yapc

=How to get a date with Purl

* Botsnacks

* Ponys

* Pants

=How to get a date with Perl

* Date

* Time

* Calendars

=Outline

* Built-in date/time functions

* Date/Time modules

* Module interoperability

* How to do common date/time tasks

* Reefknot

* Ongoing development - how you can help

= Built-in date/time functions: time

* epoch time

* Seconds since Jan 1, 1970

* Perhaps seconds since some other time

* Easy to figure out differences in time (subtraction)

* Easy to add to dates

* Limited to range Jan 1 1970 - some time in 2038

=Built-in date/time functions: localtime()

* Takes an epoch time as an argument

* Defaults to current time

* Returns either a string representation of the time

* Or an array of time elements

=Built-in date/time functions: localtime()

( $sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst ) = localtime( time );

* Slices

( $day, $mon, $year ) = ( localtime(time) )[ 3, 4, 5 ];

=Built-in date/time functions: gmtime()

Page 2: yapc

* Just like localtime, only different

* GMT rather than local time

=Standard date/time functions: Time::Local

* Get epoch time back from time elements

$time = timelocal($sec,$min,$hours,$mday,$mon,$year); $time = timegm($sec,$min,$hours,$mday,$mon,$year);

=Date:: and Time:: modules

* There are dozens of date and time modules on CPAN

* None of them talk to each other

* They need to

* Proposal: All Date:: modules use Date::ICal as a internal format

* Detailed proposal on http://dates.rcbowen.com/, when the power comesback on.

=Date:: and Time:: modules

* But, having said that, there are a lot of really useful Date:: andTime:: modules

* Date::Manip - Docs give you various reasons not to use it.

* Date::Calc - Pretty cool, but still very very very large.

* Lots of Date:: modules to do various calendars

=Date::Manip

* Does all sorts of things

* Does them really well, but rather slow

* Is huge and bloated

* The docs tell you that, most of the time, you really should not useit.

* All in Perl

=Date::Calc

* Largely in C

* Still pretty big

* Because it does everything

* Use this rather than Date::Manip, if those are your only two options

* They are probably not your only two options

=Time::HiRes

* time() gives you 1-second granularity

Page 3: yapc

* Time::HiRes gives much higher resolution

=time-modules

* Time::CTime

* Time::DaysInMonth

* Time::JulianDay

* Time::ParseDate

* Time::Timezone

=time-modules: Time::CTime

* Format times

print strftime("%b %o, %Y", localtime(time));

Jun 13th, 2001

=time-modules: Time::DaysInMonth

* Calculates how many days in a given month

* Not very exciting

* Also has a is_leap function.

=time-modules Time::JulianDay

* Julian days express the date in terms of how many days have passedsince some fixed time in the past.

* In particular, Jan 1, 4713 BC

* Gets around all "date window" problems

* Has the disadvantage that it is difficult to calculate datedifferences in anything but elapsed days. That is, converting that toyears, months, etc, is difficult.

* Very useful as a storage mechanism for date information, whichcan then be converted back to something more useful.

=Time::Object

* Gives an object interface to time()

use Time::Object; $t = localtime; $year = $t->year; $month = $t->month;

* Good if you can't remember all the positions of the various fields.

* Methods for various different spellings of all the differentattributes.

=Alternate calendars

* Encapsulate, to an extent, a culture

Page 4: yapc

* Understanding the culture reuquires a understanding of the calendar,and vice versa, for many calendars.

* Calendars are mathematically interesting, for all but the mosttrivial calendars. When are particular events? How long is it until[insert favorite holiday here]?

=Real calendars

* Real calendars are, of course, the most interesting, since theyhave had cenuries to develop complexity.

* They are usually also the most complex

* Seemingly random rules

=Example: Id al-fitr

* Id al-fitr (Shawwal) is the end of Ramadan

* Please forgive my gross ignorance of Islamic tradition ...

* New moon must be seen in Mecca

* What if it's overcast?

=Example: Orthodox church

* High holy days cause a saint's day to get shifted

* Can cause a particular saint day to get shifted 2 or 3 weeks

* Domino effect

=Real calendars

* Hebrew

* 19th Century English gentry

* Mayan

* Chinese

* Japanese

* ... and thousands of others

=Real calendars and Perl

* Date::Maya

* Date::Convert

* Several Hebrew calendar modules, but not on CPAN

* Very little else

* A lot of opportunity for fun modules

* They need to be able to talk to each other

=Invented calendars

Page 5: yapc

* Typically much simpler ... (Discordian, ISO)

* ... but not always (Stardate, French Revolutionary)

* Calendars tend to have much more predicatable, calculatablerules

=Discordian

* 5 seasons (Chaos, Discord, Confusion, Beaurocracy, Aftermath)

* Each season is 73 days

* Leap day (St Tibbs)

* 5 days (Sweetmorn, Boomtime, Pungenday, Prickle Prickle, SettingOrange)

* 5th and 50th of every season is a holyday

* Date::Discordian

=ISO

* "Months" are confusing

* 52 weeks

* Year always starts on a Monday

* Dates are represented as a week number and a day number

* Strange contortions to figure out when the year starts

* Date::ISO

=French Revolutionary

* Sort of "real", but expressly invented rather than evolved

* Presumably this was the supremely logical calendar

* 10 day weeks with 3 days of weekend

* Date::Convert::FrenchRevolutionary

=All the easy ones are taken!

* The easy calendar modules are done

* What's left to do?

* Date::ICal (Or something else.)

=ICalendar

* The ICalendar standard

* Reefknot

* Date::ICal - useful subset

* Common language between date modules

Page 6: yapc

* Go to the Reefknot talk

* Go to the BOF this evening

=Date Cookbook

* Loosely based on the Perl Cookbook, date chapter

* But always takes a different approach

* TIMTOWTDI

=What is today?

* Um ...

* This is a really hard question, depending on what you mean byit. Today is ...

Thursday, June 14, 2001 Setting Orange, Confusion 19, 3167 YOLD Anniversary of the creation of the US Army Commemoration Day of Victims of the Communist Terror (Latvia) Corpus Christi Day of Mourning and Hope (Lithuania) Freedom Day (Malawi) Independence Day (Vietnam) Liberation Day (Falkland Islands) Mothers' Day (Afghanistan)

* How do you calculate that?

* More importantly, are these answers meaningful out of the historicaland/or cultural context?

=OK, so what is today?

print scalar localtime;

use Date::Manip; $listref=&Events_List($date); $listref=&Events_List($date0,$date1);

=Convert a date to epoch time

* User input - don't permit freeform

* But, if you do ...

use Time::ParseDate; $epoch = parsedate( "Monday, June 4th, 1984, 4:32pm" ); $epoch = parsedate( "next tuesday" ); $epoch = parsedate( "4pm tomorrow" );

* Date::Manip and Date::Calc have similar functions

=Pretty formatting of dates

* Posix does this, but it is HUGE

* Time::CTime provides arbitrary formatting of dates using templates

use Time::CTime;

Page 7: yapc

my $now = time; print strftime( '%B %o, %Y', localtime($now) );

* See Time::CTime docs for more details.

=What day of the week?

* During epoch dates, element 6 of localtime array

* Outside of the epoch, there are several algorithms

* Carrol had an algorithm, but I have not tracked it down just yet

* Doomsday

=Doomsday

* As Seen on /. !!!

* Last day of February is Doomsday (This year doomsday is Wednesday)

* N/N is doomsday for even N

* 9-5, 7-11

* Jan 31 (or 32)

* March 0

=Date::Doomsday and Date::DayOfWeek

use Date::DayOfWeek; $dow = dayofweek( 7, 12, 1941 ); # Pearl Harbor bombed on a Sunday $dow = dayofweek( 4, 7, 1776 ); # USA Declaration of Independance signed on Thursday

* Only good after September 1562

* Need something for earlier days, in Julian calendar

* Date::DayOfWeek::Birthday, Date::DayOfWeek::Nails,Date::DayOfWeek::Sneeze

=Storing dates in a convenient format

* Storing dates (particularly outside of the epoch) is cumbersome

* Need common standard format

* ICalendar is potentially a solution

* Date::ICal should be considered beta

* Go to the calendaring BOF if you're more interested

=Time elapsed between two dates

* Date::Calc

* Time::JulianDay

$date1 = julian_day( $year1, $month1, $day1 ); $date2 = julian_day( $year2, $month2, $day2 );

Page 8: yapc

$difference = $date2 - $date1;

* Just days, not months, years, etc.

=More reading

* Calendarical Calculations

* The Oxford Companion to the Year (Blackburn and Holfort-Strevens)Oxford Press

* Numerous web sites, but make sure that they are reasonablyauthoritative, as there is a LOT of bad information out there too.

=Interested?

* Reefknot talk next session

* Reefknot/Date/Time/Calendaring BOF 6pm today

* [email protected] (see http://lists.perl.org for more info)

* http://dates.rcbowen.com/ (This talk, other random stuff)

* http://www.reefknot.org/

=Questions?

* ???