Dates and Times - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Dates and Times

Description:

... The Epoch (On Unix and many other systems, the Epoch was 00:00 Jan 1, 1970, GMT) ... and seconds, use the localtime() function (use gmtime() if you want GMT) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 27
Provided by: acmCseB
Category:
Tags: dates | gmt | times

less

Transcript and Presenter's Notes

Title: Dates and Times


1
Dates and Times
  • How do we calculate dates and times?

2
Time
  • Perl's time function returns the number of
    seconds that have passed since The Epoch (On Unix
    and many other systems, the Epoch was 0000 Jan
    1, 1970, GMT).

3
Time, in action
  • the_time time
  • print the_time, "\n"
  • prints a huge number (number of seconds
  • since The Epoch)
  • years (((the_time/60)/60)/24)/365
  • print years, "\n"
  • prints number of years since The Epoch

4
More on time
  • Epoch seconds represent intervals and points in
    the same units, so you can do basic arithmetic on
    them.
  • However, people don't deal with epoch seconds we
    deal with weeks, months, etc.
  • Because of the difficulty of performing
    calculations with a variety of formats, in Perl
    we might convert human-supplied dates
    (01/22/2001) to Epoch seconds, calculate, and
    then convert back to dates for output.

5
localtime (and gmtime)
  • We usually want weeks, months, etc, not seconds.
  • To convert epoch seconds into distinct values for
    days, months, years, hours, minutes, and seconds,
    use the localtime() function (use gmtime() if you
    want GMT).

6
localtime() and gmtime()
  • NOTES
  • Months are returned in the range 0-11
  • Year is returned as number of years since 1900
  • localtime() and gmtime() take an argument of
    seconds since the Epoch. The default is the
    current value of time

7
Quick Diversion on Years
  • Perl does not return a 2-digit year value. It
    returns the year minus 1900.
  • Y2K problem?? Only if you create one
  • If you use a construct like
  • full_year "19year"
  • you'll find your scripts will refer to the year
    19101.
  • Instead use
  • year 1900

8
Y2038 problem
  • However, there is (possibly) a Y2038 problem! If
    you have a 32-bit system, a 32-bit integer will
    hold your Epoch seconds. The range of this
    number is from December 1901 to January 2038.
    After that the Epoch seconds overflow 32 bits!

9
Localtime in action
  • localtime() returns a bunch of info in
  • list context
  • my (seconds, minutes, hours, day, month,
    year, weekday, yearday, isdst) localtime
    (time)
  • month why do we do this?
  • year 1900 why do we do this?
  • print "Date is month/day/year\n"

10
Cleaner Version of localtime
  • just grab what you need using a slice
  • (day, month, year) (localtime)3,4,5
  • month
  • year 1900
  • printf comes in handy to make dates
  • look clean
  • printf "Date is 02d/02d/04d\n",
  • month, day, year

11
Backwards
  • Use TimeLocal to convert from day/month/year to
    an epoch time
  • use TimeLocal
  • time timelocal(0,0,0,day,
  • mon - 1, year)
  • Use localtime(number) to go from epoch to a
    day/month/year

12
Scalar form of localtime
  • In scalar context, localtime() returns a
    human-readable string.
  • This is often good enough if you don't need a
    particular date format (MM/DD/YYYY, DD-MM-YYYY,
    etc).
  • date localtime
  • print date
  • prints something like
  • Tues Apr 13 122931 1999

13
Modules for Dates
  • There are several good modules on CPAN for
    dealing with dates and times. Here's two
  • DateCalc
  • DateManip

14
DateCalc
  • Many functions in DateCalc. "perldoc
    DateCalc" for everything. Here's an example
  • use strict
  • use DateCalc 'Today_and_Now'
  • my (year,month,day, hour,min,sec)
    Today_and_Now()
  • printf "It is 02d02d02d on
  • 02d/02d/04d\n",
  • hour, min, sec, month, day, year

15
Another DateCalc example
  • use strict
  • use DateCalc 'Delta_Days'
  • my _at_joel (1973, 3, 16) March 16, 1973
  • my _at_sarah (1975, 9, 3) Sept 3, 1975
  • my difference Delta_Days (_at_joel, _at_sarah)
  • print "There were difference days between Joel
    and Sarah\n"

16
DateManip
  • DateManip is the 800-pound gorilla of date
    manipulation modules biggest, most powerful, and
    slowest.
  • Many functions are available. We will have a
    module presentation on this next quarter.
  • It is designed to make any common date/time
    manipulation easy, not necessarily quickly.

17
Catching errors
  • Many modern languages incorporate "exception
    handling"
  • Java and C call them "throw" and "catch"
  • Perls version is called die and eval

18
Catching errors
  • Use eval to trap problems
  • print "enter divisor"
  • my divisor ltSTDINgt
  • my result 100 / divisor
  • catching the divide by zero error
  • my result eval 100 / divisor

19
Catching errors
  • You can look at the actual error caught in _at_
  • sub getFile
  • print "Enter file"
  • my file ltSTDINgt
  • open (FH, file) or die(!)
  • more code
  • eval getFile("filename.txt")
  • if (_at_)
  • print "There was an error opening file.\n"

20
Compiling code at run time
  • Eval on a string (without ) is a completely
    different animal
  • eval variable
  • eval " code "
  • This actually compiles code.
  • Can be a security hole if hackers can run the
    program (like in a CGI).
  • eval is safe.
  • eval string can be dangerous.

21
Example
  • print "Enter filename "
  • my file ltSTDINgt
  • eval
  • open FH, file or die !
  • versus
  • eval "open FH, file or die !"

22
Process Management
  • Usually theres a CPAN module that does what you
    need portably.
  • But sometimes you want a quick-and-dirty script.

23
System
  • system "dir"
  • System starts a child process to run a shell
    command.
  • Perl naps in the meantime
  • STDOUT and STDIN are inherited

24
System (continued)
  • Not portable
  • Can be problems with user input.
  • Whats wrong with this
  • print "Enter filename"
  • my file ltSTDINgt
  • system("cat file")

25
Backticks
  • Backticks run a system command and capture the
    output
  • my help
  • my _at_functions
  • qw! int rand length not exit sqrt!
  • foreach (_at_functions)
  • help_ perldoc f _

26
Backticks (unix shell)
  • the unix date command prints out the date
  • in a variety of formats ("man date" for
  • more info). Use backticks to run a shell
  • command and capture the output, like so
  • chomp(my date date 'm/d/Y')
  • print date, "\n"
  • prints MM/DD/YYYY
Write a Comment
User Comments (0)
About PowerShow.com