How do you find the days before a date with time() (RE-VISIT)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raubana
    New Member
    • Feb 2008
    • 55

    How do you find the days before a date with time() (RE-VISIT)

    I tried making a program that tells me the time before a specifide date, but it's really hard! I tried one of the ways that a person sent me, but it doesn't work...
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    You really need to read the posting guidelines (particularly 'How to Ask a Question'), as you consistently violate them.

    What "doesn't work"? What did you try? You need to provide some semblance of information if you expect us to help you.

    Comment

    • kaarthikeyapreyan
      New Member
      • Apr 2007
      • 106

      #3
      Hey try this if it suits your requirement
      This will compute the number of days that has passed if the number of seconds or the date in dd/mm/yyyy format is given

      Code:
      import time
      import string ,sys
      
      class findtime:
          
          def __init__(self):
      
              self.secperday=86400
              self.uinput=raw_input("Enter the number of seconds or the format dd/mm/yyyy: ")
              
          def checktime(self):
              """
              Computations with epoch time working with dates after 1970
              """
              self.presentsec = time.time()
              self.pform= time.localtime()
              
              if '/' in self.uinput :
                  frdate=string.split(self.uinput,'/')
                  consdate = tuple([int(frdate[2]),int(frdate[1]),int(frdate[0]),self.pform[3],self.pform[4],self.pform[5],self.pform[6],self.pform[7],self.pform[8]])
                  diff =   self.presentsec - (time.mktime(consdate))
                  print "Number of seconds Passed %d" %diff
                  print "Number of days passed %d"  % (diff/self.secperday)
      
              else :
                  self.uinput =float(self.uinput)
                  print ("Computation for %s seconds from present date" %self.uinput)
                  self.expectedsec = self.presentsec - self.uinput
                  self.expectedday=time.localtime(self.expectedsec)
                  print ("Expected %d/%d/%d"%(self.expectedday[2],self.expectedday[1],self.expectedday[0]))
                  diffdays =  int(round(self.uinput / self.secperday))
                  print "Number of days passed %d"  % (diffdays)
                  
      if __name__=='__main__':
          
          obj = findtime()
          obj.checktime()

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        It seems like I posted this before. It uses time and datetime.
        [code=Python]import datetime, time

        def convert_seconds (seconds):
        hours = int(seconds/3600)
        mins = int((seconds-hours*3600)/60)
        secs = seconds-hours*3600-mins*60
        return hours, mins, secs

        present = datetime.dateti me.now()

        print '''Calculate the difference between the present time and another
        date, either past or future (present - other date).'''

        dateStr = raw_input('Ente r the date (mm/dd/yyyy)')
        timeObj = time.strptime(d ateStr, '%m/%d/%Y')
        datetimeObj = datetime.dateti me(*timeObj[:6])
        delta = present-datetimeObj
        hours, mins, secs = convert_seconds (delta.seconds)

        print 'The difference is %d day%s, %d hour%s, %d minute%s, and %d second%s.' % \
        (delta.days, ['s', ''][abs(delta.days) ==1 or 0],\
        hours, ['s', ''][abs(hours)==1 or 0],\
        mins, ['s', ''][abs(mins)==1 or 0],\
        secs, ['s', ''][abs(secs)==1 or 0])[/code]
        [code=Python]>>> Calculate the difference between the present time and another
        date, either past or future (present - other date).
        The difference is -29 days, 8 hours, 35 minutes, and 19 seconds.
        [/code]

        Comment

        • jlm699
          Contributor
          • Jul 2007
          • 314

          #5
          Originally posted by raubana
          I tried one of the ways that a person sent me, but it doesn't work...
          @bvdet: Yes you did provide him with this previously... He failed to elaborate why it didn't work however.

          Comment

          • raubana
            New Member
            • Feb 2008
            • 55

            #6
            Yeah, I didn't explain why it didn't work before because I'm not sure why it didn't work... I do know, however, that the '('s, ')'s, '[' and the ']' may have been missing in some places.

            Anyway, thanks for your help, guys! I'll try it out as soon as I can.

            Comment

            Working...