SlideShare a Scribd company logo
1 of 50
Download to read offline
Dates and Times
                             The Silent Killers
Saturday, August 11, 12
Dates and Times Are Easy!




Saturday, August 11, 12
Dates and Times Are Easy!


          • 60 seconds in a minute, 60 minutes in an hour.




Saturday, August 11, 12
Dates and Times Are Easy!


          • 60 seconds in a minute, 60 minutes in an hour.
          • 24 hours in a day, 365 days in a year, unless the year
                is a multiple of 4.




Saturday, August 11, 12
Dates and Times Are Easy!


          • 60 seconds in a minute, 60 minutes in an hour.
          • 24 hours in a day, 365 days in a year, unless the year
                is a multiple of 4.

          • Simple, right?


Saturday, August 11, 12
WRONG
Saturday, August 11, 12
A reality check




Saturday, August 11, 12
A reality check


          • There are not always 60 seconds in a minute,
                sometimes more (leap seconds, joy!)




Saturday, August 11, 12
A reality check


          • There are not always 60 seconds in a minute,
                sometimes more (leap seconds, joy!)

          •      Not always 24 hours in a day: sometimes more,
                sometimes less—consider DST




Saturday, August 11, 12
Pop Quiz!
Saturday, August 11, 12
Saturday, August 11, 12
• How many days were there in February 1900?




Saturday, August 11, 12
• How many days were there in February 1900?
          • How about February 2000?




Saturday, August 11, 12
• How many days were there in February 1900?
          • How about February 2000?
          • Remember what you know about leap years.




Saturday, August 11, 12
Saturday, August 11, 12
• There were... 28 days in February 1900




Saturday, August 11, 12
• There were... 28 days in February 1900
          • ... and 29 in February 2000




Saturday, August 11, 12
• There were... 28 days in February 1900
          • ... and 29 in February 2000
          • Why? Years that are divisible by 100 are not leap
                years, whereas years divisible by 400 are




Saturday, August 11, 12
Why? How does that affect me? How do
                        I do it right?




Saturday, August 11, 12
Why? How does that affect me? How do
                        I do it right?

          • Variances in Earth’s rotation and axial tilt cause our
                rotation and revolution to be inconsistent




Saturday, August 11, 12
Why? How does that affect me? How do
                        I do it right?

          • Variances in Earth’s rotation and axial tilt cause our
                rotation and revolution to be inconsistent

          • It means that there’s a lot of particulars and edge
                cases for doing date/time math




Saturday, August 11, 12
Why? How does that affect me? How do
                        I do it right?

          • Variances in Earth’s rotation and axial tilt cause our
                rotation and revolution to be inconsistent

          • It means that there’s a lot of particulars and edge
                cases for doing date/time math

          • Currently, NYC observes DST. But the government
                could change that. Certain parts of the US (Indiana,
                Arizona) don't observe


Saturday, August 11, 12
If that wasn’t enough...


          • Consider what you’d do if you were working for
                Indiana Jones and he wanted you to convert dates
                between the Mayan calendar and the Gregorian
                calendar. Or, more likely, the Julian calendar (run cal
                9 1752 and prepare to be surprised) and the
                Gregorian calendar.




Saturday, August 11, 12
How do we handle this?




Saturday, August 11, 12
use DateTime;




Saturday, August 11, 12
use DateTime;


          • DateTime knows about all these edge cases and
                vagaries and handles them for you, intelligently




Saturday, August 11, 12
use DateTime;


          • DateTime knows about all these edge cases and
                vagaries and handles them for you, intelligently

          • It throws exceptions for invalid data, so you can’t
                construct something that shouldn’t exist




Saturday, August 11, 12
A Word about Timezones




Saturday, August 11, 12
A Word about Timezones
          • By default, DateTime objects use the “floating”
                timezone




Saturday, August 11, 12
A Word about Timezones
          • By default, DateTime objects use the “floating”
                timezone

          • This means that it doesn’t take into account any local
                timezone issues like leap years




Saturday, August 11, 12
A Word about Timezones
          • By default, DateTime objects use the “floating”
                timezone

          • This means that it doesn’t take into account any local
                timezone issues like leap years

          • It’s best to specify the timezone when creating a DT
                object, convert to UTC for working with time/dates,
                and then convert to the user’s local timezone for
                display


Saturday, August 11, 12
my $dt = DateTime->now(time_zone =>
             'America/New_York');

             $dt->set_time_zone('UTC');
             $dt->add(months => 3) # leap year!
             # the next day...
             $dt->set_time_zone('Asia/Taipei');




Saturday, August 11, 12
Getting Dat[ae] into DateTime




Saturday, August 11, 12
Getting Dat[ae] into DateTime

          • Several constructors: new, now, from_epoch




Saturday, August 11, 12
Getting Dat[ae] into DateTime

          • Several constructors: new, now, from_epoch
                • The new constructor takes parameters like hour,
                     minute, second, day, month, year




Saturday, August 11, 12
Getting Dat[ae] into DateTime

          • Several constructors: new, now, from_epoch
                • The new constructor takes parameters like hour,
                     minute, second, day, month, year

                • from_epoch takes a named epoch argument



Saturday, August 11, 12
Getting Dat[ae] into DateTime

          • Several constructors: new, now, from_epoch
                • The new constructor takes parameters like hour,
                     minute, second, day, month, year

                • from_epoch takes a named epoch argument
                • All constructors take a time_zone named
                     argument


Saturday, August 11, 12
my $dt = DateTime->now(
     time_zone => 'EST5EDT'
    );
    my $dt = DateTime->from_epoch(
       epoch => 1234567890,
       time_zone => 'America/New_York',
    );
    my $dt = DateTime->new(
       minute => 24,
       hour => 20,
       year => 2012,
       month => 3,
       day => 1
    );

Saturday, August 11, 12
Manipulating Dates




Saturday, August 11, 12
Manipulating Dates
          • Arithmetic operations are overloaded




Saturday, August 11, 12
Manipulating Dates
          • Arithmetic operations are overloaded
          • Subtracting two DateTime objects yields a
                DateTime::Duration (the delta)




Saturday, August 11, 12
Manipulating Dates
          • Arithmetic operations are overloaded
          • Subtracting two DateTime objects yields a
                DateTime::Duration (the delta)

          • Adding two objects doesn't make sense; add
                a ::Duration to get a DateTime object




Saturday, August 11, 12
Manipulating Dates
          • Arithmetic operations are overloaded
          • Subtracting two DateTime objects yields a
                DateTime::Duration (the delta)

          • Adding two objects doesn't make sense; add
                a ::Duration to get a DateTime object

          • See also add, subtract methods (take same args as
                new)


Saturday, August 11, 12
my $later = $dt->clone->add(days => 2);
    my $duration = $later - $dt;
    my $later_also = $dt + $duration;




Saturday, August 11, 12
Getting Dat[ae] Out




Saturday, August 11, 12
Getting Dat[ae] Out

          • DateTime overloads the " operator; this produces an
                ISO8601-formatted string. There are lots of other
                formatters on CPAN, including one for MySQL types.




Saturday, August 11, 12
Getting Dat[ae] Out

          • DateTime overloads the " operator; this produces an
                ISO8601-formatted string. There are lots of other
                formatters on CPAN, including one for MySQL types.

          • There's a strftime method for very flexible
                formatting




Saturday, August 11, 12
Getting Dat[ae] Out

          • DateTime overloads the " operator; this produces an
                ISO8601-formatted string. There are lots of other
                formatters on CPAN, including one for MySQL types.

          • There's a strftime method for very flexible
                formatting

          • Other simpler methods, like ymd, as well as varied
                accessors


Saturday, August 11, 12
my $announcement = "It is now $dt";
             my $proper_date = $dt->strftime(
                "%Y-%m-%d"
             );
             my $identical = $dt->ymd;




Saturday, August 11, 12
Questions?




Saturday, August 11, 12

More Related Content

Viewers also liked (8)

Describing people
Describing peopleDescribing people
Describing people
 
Asking and giving direction
Asking and giving directionAsking and giving direction
Asking and giving direction
 
Asking giving directions
Asking giving directionsAsking giving directions
Asking giving directions
 
Giving directions presentation_oseiasa_modelo
Giving directions presentation_oseiasa_modeloGiving directions presentation_oseiasa_modelo
Giving directions presentation_oseiasa_modelo
 
Adjectives describing appearance and personality
Adjectives   describing appearance and personalityAdjectives   describing appearance and personality
Adjectives describing appearance and personality
 
Street directions
Street directionsStreet directions
Street directions
 
Can We Assess Creativity?
Can We Assess Creativity?Can We Assess Creativity?
Can We Assess Creativity?
 
Guided Reading: Making the Most of It
Guided Reading: Making the Most of ItGuided Reading: Making the Most of It
Guided Reading: Making the Most of It
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Date time

  • 1. Dates and Times The Silent Killers Saturday, August 11, 12
  • 2. Dates and Times Are Easy! Saturday, August 11, 12
  • 3. Dates and Times Are Easy! • 60 seconds in a minute, 60 minutes in an hour. Saturday, August 11, 12
  • 4. Dates and Times Are Easy! • 60 seconds in a minute, 60 minutes in an hour. • 24 hours in a day, 365 days in a year, unless the year is a multiple of 4. Saturday, August 11, 12
  • 5. Dates and Times Are Easy! • 60 seconds in a minute, 60 minutes in an hour. • 24 hours in a day, 365 days in a year, unless the year is a multiple of 4. • Simple, right? Saturday, August 11, 12
  • 8. A reality check • There are not always 60 seconds in a minute, sometimes more (leap seconds, joy!) Saturday, August 11, 12
  • 9. A reality check • There are not always 60 seconds in a minute, sometimes more (leap seconds, joy!) • Not always 24 hours in a day: sometimes more, sometimes less—consider DST Saturday, August 11, 12
  • 12. • How many days were there in February 1900? Saturday, August 11, 12
  • 13. • How many days were there in February 1900? • How about February 2000? Saturday, August 11, 12
  • 14. • How many days were there in February 1900? • How about February 2000? • Remember what you know about leap years. Saturday, August 11, 12
  • 16. • There were... 28 days in February 1900 Saturday, August 11, 12
  • 17. • There were... 28 days in February 1900 • ... and 29 in February 2000 Saturday, August 11, 12
  • 18. • There were... 28 days in February 1900 • ... and 29 in February 2000 • Why? Years that are divisible by 100 are not leap years, whereas years divisible by 400 are Saturday, August 11, 12
  • 19. Why? How does that affect me? How do I do it right? Saturday, August 11, 12
  • 20. Why? How does that affect me? How do I do it right? • Variances in Earth’s rotation and axial tilt cause our rotation and revolution to be inconsistent Saturday, August 11, 12
  • 21. Why? How does that affect me? How do I do it right? • Variances in Earth’s rotation and axial tilt cause our rotation and revolution to be inconsistent • It means that there’s a lot of particulars and edge cases for doing date/time math Saturday, August 11, 12
  • 22. Why? How does that affect me? How do I do it right? • Variances in Earth’s rotation and axial tilt cause our rotation and revolution to be inconsistent • It means that there’s a lot of particulars and edge cases for doing date/time math • Currently, NYC observes DST. But the government could change that. Certain parts of the US (Indiana, Arizona) don't observe Saturday, August 11, 12
  • 23. If that wasn’t enough... • Consider what you’d do if you were working for Indiana Jones and he wanted you to convert dates between the Mayan calendar and the Gregorian calendar. Or, more likely, the Julian calendar (run cal 9 1752 and prepare to be surprised) and the Gregorian calendar. Saturday, August 11, 12
  • 24. How do we handle this? Saturday, August 11, 12
  • 26. use DateTime; • DateTime knows about all these edge cases and vagaries and handles them for you, intelligently Saturday, August 11, 12
  • 27. use DateTime; • DateTime knows about all these edge cases and vagaries and handles them for you, intelligently • It throws exceptions for invalid data, so you can’t construct something that shouldn’t exist Saturday, August 11, 12
  • 28. A Word about Timezones Saturday, August 11, 12
  • 29. A Word about Timezones • By default, DateTime objects use the “floating” timezone Saturday, August 11, 12
  • 30. A Word about Timezones • By default, DateTime objects use the “floating” timezone • This means that it doesn’t take into account any local timezone issues like leap years Saturday, August 11, 12
  • 31. A Word about Timezones • By default, DateTime objects use the “floating” timezone • This means that it doesn’t take into account any local timezone issues like leap years • It’s best to specify the timezone when creating a DT object, convert to UTC for working with time/dates, and then convert to the user’s local timezone for display Saturday, August 11, 12
  • 32. my $dt = DateTime->now(time_zone => 'America/New_York'); $dt->set_time_zone('UTC'); $dt->add(months => 3) # leap year! # the next day... $dt->set_time_zone('Asia/Taipei'); Saturday, August 11, 12
  • 33. Getting Dat[ae] into DateTime Saturday, August 11, 12
  • 34. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epoch Saturday, August 11, 12
  • 35. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epoch • The new constructor takes parameters like hour, minute, second, day, month, year Saturday, August 11, 12
  • 36. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epoch • The new constructor takes parameters like hour, minute, second, day, month, year • from_epoch takes a named epoch argument Saturday, August 11, 12
  • 37. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epoch • The new constructor takes parameters like hour, minute, second, day, month, year • from_epoch takes a named epoch argument • All constructors take a time_zone named argument Saturday, August 11, 12
  • 38. my $dt = DateTime->now( time_zone => 'EST5EDT' ); my $dt = DateTime->from_epoch( epoch => 1234567890, time_zone => 'America/New_York', ); my $dt = DateTime->new( minute => 24, hour => 20, year => 2012, month => 3, day => 1 ); Saturday, August 11, 12
  • 40. Manipulating Dates • Arithmetic operations are overloaded Saturday, August 11, 12
  • 41. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta) Saturday, August 11, 12
  • 42. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta) • Adding two objects doesn't make sense; add a ::Duration to get a DateTime object Saturday, August 11, 12
  • 43. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta) • Adding two objects doesn't make sense; add a ::Duration to get a DateTime object • See also add, subtract methods (take same args as new) Saturday, August 11, 12
  • 44. my $later = $dt->clone->add(days => 2); my $duration = $later - $dt; my $later_also = $dt + $duration; Saturday, August 11, 12
  • 46. Getting Dat[ae] Out • DateTime overloads the " operator; this produces an ISO8601-formatted string. There are lots of other formatters on CPAN, including one for MySQL types. Saturday, August 11, 12
  • 47. Getting Dat[ae] Out • DateTime overloads the " operator; this produces an ISO8601-formatted string. There are lots of other formatters on CPAN, including one for MySQL types. • There's a strftime method for very flexible formatting Saturday, August 11, 12
  • 48. Getting Dat[ae] Out • DateTime overloads the " operator; this produces an ISO8601-formatted string. There are lots of other formatters on CPAN, including one for MySQL types. • There's a strftime method for very flexible formatting • Other simpler methods, like ymd, as well as varied accessors Saturday, August 11, 12
  • 49. my $announcement = "It is now $dt"; my $proper_date = $dt->strftime( "%Y-%m-%d" ); my $identical = $dt->ymd; Saturday, August 11, 12