944,083 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3646
  • Python RSS
Oct 25th, 2005
0

File with date + time

Expand Post »
I want to creat file with each line having date + time + data so I can append more data as need be and know when it was added. What date+time function would be best?
Similar Threads
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 25th, 2005
0

Re: File with date + time

I just was messing around with the time module tonight. are you asking for something simple like
Python Syntax (Toggle Plain Text)
  1. >>> import time
  2. >>> time.ctime()
  3. 'Mon Oct 24 23:30:47 2005'
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Oct 25th, 2005
0

Re: File with date + time

That would do it - but maybe a little shorter is even better.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 25th, 2005
0

Re: File with date + time

how about this
Python Syntax (Toggle Plain Text)
  1. import time
  2. # time.localtime() splits the time into elements of a tuple
  3. tuple_time = time.localtime()
  4. # don't europeans do day/month/year ?
  5. time_stamp = "%s:%s %s/%s/%s" %(tuple_time[3], tuple_time[4], tuple_time[2], tuple_time[1], tuple_time[0])
  6. print time_stamp

edit added later//

I noticed a time like one minute after 12 looks like this
Python Syntax (Toggle Plain Text)
  1. 0:1 25/10/2005

that could be fixed with a little more code
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Oct 25th, 2005
0

Re: File with date + time

this code fixes that issue
Python Syntax (Toggle Plain Text)
  1. def time_stamp():
  2. import time
  3. tuple_time = time.localtime()
  4. hours = tuple_time[3]
  5. if int(hours) in range(10):
  6. hours = "0%s" %hours
  7. minutes = tuple_time[4]
  8. if int(minutes) in range(10):
  9. minutes = "0%s" %minutes
  10. day = tuple_time[2]
  11. month = tuple_time[1]
  12. year = tuple_time[0]
  13. # don't europeans do day/month/year ?
  14. time_ = "%s:%s %s/%s/%s" %(hours, minutes, day, month, year)
  15. return time_
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Oct 26th, 2005
0

Re: File with date + time

That looks good. Many countries in Europe have day/month/year format, but that doesn't make much sense for what I may want to do. I am thinking about a date and time stamp starting each data line where the line could be sorted. It could have to look something like year/month/day hour:minute:second to do this rightly.

The tuple_time almost has this sorting format.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wxPython slider help
Next Thread in Python Forum Timeline: Doubling the size of an image





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC