Python Forum
[SOLVED] Alternative to regex to extract date from whole timestamp?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Alternative to regex to extract date from whole timestamp?
#1
Hello,

Python being such a rich language, I was wondering if it provides a ready-to-use function to extract juste the date from a whole timestamp instead of using a regular expression:

#Thu, 10 Nov 2022 18:15:41 +0000
pattern_pubDate = re.compile('(\d{2}) (\w+?) (\d{4})')
…
m = pattern_pubDate.search(link.pubDate.string)
if m:
	pubDate = m.group(0)
I'm only interested in the date.

Thank you.
Reply
#2
I put this together, for my own reference, but if it's of help, then you're welcome to it.

import time

hr = '{}{}{}'.format('\n'*2,'-'*50,'\n') #horizontal rule

print('''time.gmtime(0): returns system’s epoch setting:
The starting point against which you can measure the
passage of time.
''')
system_epoch = time.gmtime()
print(system_epoch,hr)

print('''time.time() returns the number of seconds
that have passed since the epoch; return value is a
floating point number to account for fractional seconds:
''')
tt = time.time()
print(tt,hr)

print('''time.ctime() will return a string object
representation of the time and date based on the
number of seconds since epoch, or the current time
and date, if nothing is passed to the function.

The string representation of time, also known as a
timestamp, returned by ctime() is formatted with the
following structure:

    1: Day of the week
    2: Month of the year
    3: Day of the month
    4: Hours, minutes, and seconds using the 24-hour
       clock notation
    5: Year
''')
tc = time.ctime(1660867200)

print('''Note: time.ctime(0) may not return the same timestamp
on every computer system
''')

print('''For details about time zones, see:
https://realpython.com/python-time-module/
''')
print(tc,hr)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Thank you.
Reply
#4
You can parse it as a string into a datetime object using strptime() than you can manipulate it as you want:

import datetime as dt

string_date = "Thu, 10 Nov 2022 18:15:41 +0000"
date_obj = dt.datetime.strptime(string_date, "%a, %d %b %Y %H:%M:%S %z")

print(date_obj.date())
print(date_obj.day)
print(date_obj.hour)
print(date_obj.weekday())
Output:
date: 2022-11-10 day 10 hour 18 weekday: 3
Source
snippsat likes this post
Reply
#5
Brilliant!
Reply
#6
A exmaple with Pendulum which can parse the date direclty.
One of the strong part is that special care has been taken to ensure timezones are handled correctly.
>>> import pendulum
>>> 
>>> date = "Thu, 10 Nov 2022 18:15:41 +0000"
>>> date = pendulum.parse(date, strict=False)
>>> date
DateTime(2022, 11, 10, 18, 15, 41, tzinfo=Timezone('UTC'))
>>> print(date)
2022-11-10T18:15:41+00:00

>>> # How long ago for fun
>>> pendulum.now().subtract(days=date.day).diff_for_humans()
'1 week ago'
carecavoador and Winfried like this post
Reply
#7
(Nov-16-2022, 01:48 PM)snippsat Wrote: A exmaple with Pendulum which can parse the date direclty.
One of the strong part is that special care has been taken to ensure timezones are handled correctly.

Wow, this is a nice package I didn't know about. Very nice!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 249 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Twilio alternative jmair 3 3,923 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Python date format changes to date & time 1418 4 623 Jan-20-2024, 04:45 AM
Last Post: 1418
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 773 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Pillow alternative? kucingkembar 4 885 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  Help with a regex? (solved) wrybread 3 837 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,074 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Epoch timestamp without milliseconds? Winfried 5 3,041 Jan-27-2023, 04:35 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 3 3,240 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,013 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin

Forum Jump:

User Panel Messages

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