Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parse date/time
#1
i need to parse date/time stamps of this format:
Oct 4 13:27:52 2003
i also need to handle leap years.

what can i use for this (besides regexp which seems complicated to write)?
Reply
#2
import time
import datetime

dt = 'Oct 4 13:27:52 2003'
dtobj = datetime.datetime.strptime(dt, '%b %d %H:%M:%S %Y')
print(dtobj)
timestamp = int(time.mktime(dtobj.timetuple()))
print(timestamp)
results:
Output:
datetime.datetime(2003, 10, 4, 13, 27, 52) 1065288472
Reply
#3
what if i have something else too on the line (like, 4 5 6 <date>)?
Reply
#4
you will have to figure out where the date starts and parse from there.

if you knew that the dates all begin with a valid month
This is a hack, but works:
>>> import calendar
>>> import time
>>> import datetime
>>> monlst = []
>>> for mon in range(1, 13):
...     monlst.append(calendar.month_abbr[mon])
...
>>> dt = '4 5 6 Oct 4 13:27:52 2003'
>>> sdt = dt.strip().split()
>>> for word in sdt:
...     if word in monlst:
...         idx = dt.index(word)
...         dt = dt[idx:]
...
>>> dtobj = datetime.datetime.strptime(dt, '%b %d %H:%M:%S %Y')
>>> print(dtobj)
2003-10-04 13:27:52
>>> timestamp = int(time.mktime(dtobj.timetuple()))
>>> print(timestamp)
1065288472
>>>
Reply
#5
i also need the first 3 numbers
Reply
#6
Put in a bit of effort.
How do you think this can be done?
Reply
#7
i was thinking of first getting the numbers, then doing the date/time
Reply
#8
In the code that I showed you,
the input is split into a list.
guess what the first three cells of the list contain.
Reply
#9
ignore this, it's okay

when i have something like this: "4 5 6 Oct 4 13:27:00 2003", notice the second is 0, strptime() produces (2003, 10, 4, 13, 27) and not (2003, 10, 4, 13, 27, 0)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 220 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 354 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 590 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,017 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,255 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,424 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Date format and past date check function Turtle 5 4,236 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,233 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 2,971 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 2,026 Jan-08-2021, 06:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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