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?

Recommended Answers

All 5 Replies

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'

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

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

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_

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.