954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File with date + time

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?

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

I just was messing around with the time module tonight. are you asking for something simple like

>>> import time
>>> time.ctime()
'Mon Oct 24 23:30:47 2005'
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

That would do it - but maybe a little shorter is even better.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

how about this

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


edit added later//

I noticed a time like one minute after 12 looks like this

0:1 25/10/2005


that could be fixed with a little more code

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

this code fixes that issue

def time_stamp():  
    import time
    tuple_time = time.localtime()
    hours = tuple_time[3]
    if int(hours) in range(10):
        hours = "0%s" %hours
    minutes = tuple_time[4]
    if int(minutes) in range(10):
        minutes = "0%s" %minutes
    day = tuple_time[2]
    month = tuple_time[1]
    year = tuple_time[0]
    # don't europeans do day/month/year ?
    time_ = "%s:%s %s/%s/%s" %(hours, minutes, day, month, year)
    return time_
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

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.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You