Python Forum
Convert from time.struct_time to a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert from time.struct_time to a string
#1
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.
Reply
#2
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)
Reply
#3
Thank you very much for your help ODIS, I can see the problem are now is solved! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Convert UTC now() to local time to compare to a strptime() Calab 2 153 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,343 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  convert string to float in list jacklee26 6 1,952 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,378 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,445 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,583 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,480 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,519 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,500 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,379 Jun-16-2021, 02:11 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020