#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    11

    add 1 day to date


    i have an app that reads from a text file which has the date in the filename and is constantly written to every second of the day. At various stages my app reads all the new lines in the file and write them to a new file with additional data that is entered into a form.

    However, the system that is writing to these files will create a new file every day at midnight so it is likely that i will need to use data from both files. I am going to put the name of the last file used into a text file and open that file but i also need to add 1 day to the date and check if that file exists. is there a quick and easy way of doing this in strftime

    Code:
    checkdate = time.strftime('%Y_%m_%d')
    2005_05_25
    File Name examples
    Data_Line5_2005_05_25.txt
    Data_Line5_2005_05_26.txt
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    This will work:
    Code:
    # adding one day to the date
    
    import time
    
    timeTuple = time.localtime()
    print timeTuple  # test
    
    # convert tuple to list so you can change an element
    timeList = list(timeTuple)
    print timeList   # test
    
    # day is element at index 2, increment by one day
    timeList[2] += 1
    print timeList   # test
    
    checkdate = time.strftime('%Y_%m_%d',timeList)
    print checkdate  # test

    Comments on this post

    • netytan agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    11
    This wont work at the end of the months, say 28th Feb, 30th or 31st of a month as it will just add a day that doesn't exist.

    I think i can get round it by putting the names of last files used into a list and writing these to a text file. The i can just compare the list of last files used against the list of current files.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Originally Posted by macca1707
    This wont work at the end of the months, say 28th Feb, 30th or 31st of a month as it will just add a day that doesn't exist.
    Then just test with a if statement
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    What about datetime:
    Code:
    import datetime
    
    now = datetime.date(2005,5,26)
    delta = datetime.timedelta(days=1)
    print now
    print now + delta
    Regards, mawe
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    Originally Posted by mawe
    Hi!

    What about datetime:
    Code:
    import datetime
    
    now = datetime.date(2005,5,26)
    delta = datetime.timedelta(days=1)
    print now
    print now + delta
    Regards, mawe
    Good show mawe! This one takes care of end of the month and even leapyears!!!
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    mawe's idea modified:
    Code:
    # add a day, takes care of end of month and leap years
    import datetime
    
    now = datetime.date.today()
    print "now = ", now       # test
    
    delta = datetime.timedelta(days=1)
    print "delta = ", delta   # test
    
    add_a_day = now + delta
    print "now + delta = ", add_a_day
    How is that for team work!

IMN logo majestic logo threadwatch logo seochat tools logo