0
 import time;

 localtime = time.asctime( time.localtime(time.time()) )
 print "Local current time :", localtime

I'm trying to figure out how to add write a command like this: "Next day." That will then print out the next day in the calendar and so forth. I've managed to get the calendar in, but I'm in need of help on how to connect commands to it. Thanks in advance!

davide
  • 1,918
  • 2
  • 20
  • 30
  • unrelated: your code is equivalent to `print "Local current time:", time.ctime()` call in Python. – jfs Feb 24 '15 at 03:02

1 Answers1

1

Writing a program that will handle a limited set of expressions entered by the user of the form "Next day" isn't that hard, but if you want to handle arbitrary date query expressions, things get a little complicated. :)

But if you just want to know how to manipulate dates (and times) in Python you will have to read the documentation for the datetime and calendar modules. The datetime module is rather large and a little bit messy, so don't expect to master it immediately. But if you read through the docs and write lots of little test programs you will soon learn how to use it.

To get you started, here's a small example which shows how to add or subtract an arbitrary number of days from a given date. To display the dates this program uses the strftime method, which you've probably already seen in the time module docs.

#!/usr/bin/env python

import datetime

def date_string(date):
    return date.strftime('%A %d %B %Y')

oneday = datetime.timedelta(days=1)    

today = datetime.date.today()
print today

print 'Today is', date_string(today)
print 'Tomorrow is', date_string(today + oneday)
print 'Yesterday was', date_string(today - oneday)
print 'In one week it will be', date_string(today + oneday * 7)

output

2015-02-24
Today is Tuesday 24 February 2015
Tomorrow is Wednesday 25 February 2015
Yesterday was Monday 23 February 2015
In one week it will be Tuesday 03 March 2015
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • `next_day = date.today() + timedelta(days=1)` does not look too complicated to me. In general, it may be different from [now+24h](http://stackoverflow.com/q/26313520/4279). Also, note: `datetime` and `calendar` implement "proleptic Gregorian" calendar. People might use other calendars e.g., for religious purposes: PyICU package might be useful in this case. – jfs Feb 24 '15 at 03:16
  • @J.F.Sebastian: Good point; using local time can do Bad Things around Daylight Saving Time transitions. But I already told [my favourite anecdote](http://stackoverflow.com/a/26234817/4014959) about that last time we were discussing time stuff. – PM 2Ring Feb 24 '15 at 03:23
  • note: DST is not the only reason the utc offset may change. Forget wrong hour, incorrect timezone handling may lead to a wrong date! I remember reading an anecdote where pilots died because somebody hasn't handle The International Date Line properly in software -- the source is not credible but I can easily imagine how such error can be made. Here's [another anecdote (everybody is alive)](http://it.slashdot.org/story/07/02/25/2038217/software-bug-halts-f-22-flight) – jfs Feb 24 '15 at 04:01