Created
December 14, 2019 03:47
-
-
Save kwcooper/afbe896cbaf88c5e1b071a19d463a653 to your computer and use it in GitHub Desktop.
# Function that can strip the times from a handful of the gmail timestamps I have encountered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def strpMailTime(timestamp): | |
if len(timestamp) < 27: | |
# Ex. '5 Nov 2018 12:05:07 -0500' len 25 | |
# '28 Oct 2019 03:33:57 -0400' len 26 | |
dt = datetime.datetime.strptime(timestamp, "%d %b %Y %H:%M:%S %z") | |
elif len(timestamp) > 29 and len(timestamp) < 32: | |
# 'Fri, 6 Dec 2019 15:22:49 +0000' len 30 | |
# 'Mon, 09 Dec 2019 14:19:26 +0000' len 31 | |
dt = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %z") | |
elif len(timestamp) > 35 and len(timestamp) < 39: | |
# 'Wed, 2 Oct 2019 09:17:40 -0400 (EDT)' len 36 | |
# 'Tue, 10 Dec 2019 09:17:46 -0500 (EST)' len 37 | |
# 'Thu, 16 May 2019 12:48:17 +0200 (CEST)' len 38 | |
# Strip the timezone parens because datetime can't handle them... | |
paren = timestamp.find( '(' ) | |
if paren != -1: | |
timestamp = timestamp[:paren-1] | |
print(timestamp) | |
dt = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %z") | |
return dt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment