Python Forum

Full Version: Convert from time.struct_time to a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I need your help with my code. I want to convert from time.struct_time to a string.

Example:

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=23, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=54, tm_isdst=-1)
To this:

22/02/2018 23:08PM
If I want to convert from time.struct_time to a string, I would have to use something like this:

program_start_date = str(row[0])
program_start_hours = int(program_start_date.hour)
program_start_minutes = str(start_time.minute)
program_start_days = int(start_time.day)
program_start_months = int(start_time.month)
program_start_year = str(start_time.year)
#program_start_hours = str(program_start_hours)

if program_start_minutes == "0":
   program_start_minutes = "00"
elif program_start_minutes == "5":
     program_start_minutes = "05"


if program_start_hours >= 0 and program_start_hours <= 9:
   program_start_hours = str(program_start_hours)
   program_start_hours = str("0" + program_start_hours)


if program_start_days >= 0 and program_start_days <= 9:
   program_start_days = str(program_start_days)
   program_start_days = str("0" + program_start_days)


if program_start_months >= 0 and program_start_months <= 9:
   program_start_months = str(program_start_months)
   program_start_months = str("0" + program_start_months)



program_start_hours = int(program_start_hours)
program_start_days = str(program_start_days)
program_start_months = str(program_start_months)

program_finished = str(program_stop_minutes)


if program_start_hours >= 00 and program_start_hours <= 12:
    program_AM_PM = 'AM'
else:
    program_AM_PM = 'PM'
                               
                                 
program_start_times = str(program_start_hours + ':' + program_start_minutes + program_AM_PM)
program_start_time1 = str(program_start_days + "/" + program_start_months + "/" + program_start_year + " " + program_start_times)        
program_start_time = time.strptime(program_start_time1, '%d/%m/%Y %H:%M%p')
Here is the code:

cur.execute('SELECT start_date, stop_date, title, program_id FROM programs WHERE channel=? and program_id != "" LIMIT 10', [channel])
programs = cur.fetchall()

for ind, row in enumerate(programs):
    program_start_date = str(row[0])
    start_time = time.strptime(program_start_date, '%Y%m%d%H%M%S')
    #program_start_time = time.strptime(start_time, '%d/%m/%Y %H:%M%p')
Output for program_start_date:

20180222230800
I'd find that the code I wrote which it is too long and it is not necessary. If you can show me an example how I could do this with my current code, that would be great.
You almost had it:

cur.execute('SELECT start_date, stop_date, title, program_id FROM programs WHERE channel=? and program_id != "" LIMIT 10', [channel])
programs = cur.fetchall()
 
for ind, row in enumerate(programs):
    program_start_date = str(row[0])
    # parsing time with time.strptime()
    start_time = time.strptime(program_start_date, '%Y%m%d%H%M%S')
    # formatting time.struct_time with time.strftime()
    program_start_time = time.strftime('%d/%m/%Y %H:%M%p', start_time)
Thank you very much for your help ODIS, I can see the problem are now is solved! :)