manxing
manxing

Reputation: 3305

calculate dates in python

def main(filename, from_str, to_str):
    date_from =time.strptime(from_str, "%Y-%m-%d %H:%M")
    date_to = time.strptime(to_str, "%Y-%m-%d %H:%M")
    print date_from, date_to
    days = (date_from - date_to).days
    print days

if __name__ == "__main__":
    if len(sys.argv) < 1:
        print "Usage: %s DATE [e.g. 2011-09-08 2011-10-08]"
        sys.exit(1)
    main("servanet-" + sys.argv[1] + sys.argv[2]+ ".txt", sys.argv[1] + " 00:00", sys.argv[2] + " 23:59")

This is part of my code, I want to calculate the days from the input, (I don't need to calculate minutes and seconds,just days in this case, but I will use the minute and the second information later in the code, so I need to keep them like this) ,but it seems, (date_from - date_to).days cannot work with minutes and seconds after it, how can I solve this problem?

Many thanks!

========comments: I think I cannot simply use day2-day1. since if they are from different month, the result will be wrong, like from 2011-08-01 to 2011-09-02

Upvotes: 0

Views: 1112

Answers (3)

unutbu
unutbu

Reputation: 879321

Use datetime.datetime.strptime instead of time.strptime: time.striptime returns a time.struct_time object which does not support subtraction. In contrast, datetime.datetime.strptime returns a datetime.datetime object, which does support date arithmetic.

import datetime as dt

def main(filename, from_str, to_str):
    date_from = dt.datetime.strptime(from_str, "%Y-%m-%d %H:%M")
    date_to = dt.datetime.strptime(to_str, "%Y-%m-%d %H:%M")
    print date_from, date_to
    days = (date_from - date_to).days
    print days

yields

% test.py '2011-09-08' '2011-10-08'
2011-09-08 00:00:00 2011-10-08 23:59:00
-31

By the way, sys.argv is always at least of length 1. The first item is the name of the calling program. So I think you need

if __name__ == "__main__":
    if len(sys.argv) <= 2:
        print "Usage: %s DATE [e.g. 2011-09-08 2011-10-08]"

instead of if len(sys.argv) < 1.

Upvotes: 3

Johnlcf
Johnlcf

Reputation: 582

I am not sure what you mean "cannot with minutes and seconds" after it. But I modified your function a little bit and it should be fine:

def main(filename, from_str, to_str):
    date_from = datetime.strptime(from_str, "%Y-%m-%d %H:%M")
    date_to = datetime.strptime(to_str, "%Y-%m-%d %H:%M")
    print date_from, date_to
    days = (date_to - date_from).days
    print days

main("", "2011-09-08 00:00", "2011-10-09 00:00")
main("", "2011-09-08 00:00", "2011-10-08 23:59")
main("", "2011-09-08 00:00", "2011-10-08 00:00")

>>> 2011-09-08 00:00:00 2011-10-09 00:00:00
31
2011-09-08 00:00:00 2011-10-08 23:59:00
30
2011-09-08 00:00:00 2011-10-08 00:00:00
30

Upvotes: 0

pts
pts

Reputation: 87221

import datetime
import time

def parse_date(date_str):
  if ' ' in date_str:
    return time.strptime(date_str, "%Y-%m-%d %H:%M")
  else:
    return time.strptime(date_str, "%Y-%m-%d")

def main(filename, from_str, to_str):
  date_from = parse_date(from_str)
  date_to = parse_date(to_str)
  print date_from, date_to
  days = (datetime.date(*date_to[:3]) - datetime.date(*date_from[:3])).days
  print days

Upvotes: 0

Related Questions