Python Forum
[SOLVED] Alternative to regex to extract date from whole timestamp? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] Alternative to regex to extract date from whole timestamp? (/thread-38721.html)



[SOLVED] Alternative to regex to extract date from whole timestamp? - Winfried - Nov-16-2022

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.


RE: Alternative to regex to extract date from whole timestamp? - rob101 - Nov-16-2022

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)



RE: Alternative to regex to extract date from whole timestamp? - Winfried - Nov-16-2022

Thank you.


RE: [SOLVED] Alternative to regex to extract date from whole timestamp? - carecavoador - Nov-16-2022

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


RE: [SOLVED] Alternative to regex to extract date from whole timestamp? - Winfried - Nov-16-2022

Brilliant!


RE: [SOLVED] Alternative to regex to extract date from whole timestamp? - snippsat - Nov-16-2022

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'



RE: [SOLVED] Alternative to regex to extract date from whole timestamp? - carecavoador - Nov-16-2022

(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!