Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WesleyBatista/9db8475ec65f559c9a37ac7d5436a6e9 to your computer and use it in GitHub Desktop.
Save WesleyBatista/9db8475ec65f559c9a37ac7d5436a6e9 to your computer and use it in GitHub Desktop.
import dateutil.parser
dates = [
# dates that comes as str from kafka message:
'2019-05-27T12:54:28.777652636Z',
'2019-05-27T12:54:28.128Z', # added
'2019-05-27T12:54:28.120Z', # added
'2019-05-27T12:54:28.100Z', # added
'2019-05-27T12:54:28.1Z', # added
'2019-05-27T12:54:29.993908166Z',
]
def parser_current(dt):
return int(str(dt.timestamp()).replace('.', ''))
def parser_1(dt):
return str(int(dt.timestamp())) + dt.strftime('%f')
def parser_2(dt):
return int(dt.timestamp() * 1000000)
for item in dates:
print(f'running for "{item}"')
dt = dateutil.parser.parse(item)
print(f'parser_current() {parser_current(dt)}')
print(f'parser_1() {parser_1(dt)}')
print(f'parser_2() {parser_2(dt)}')
print('')
@WesleyBatista
Copy link
Author

output:

running for "2019-05-27T12:54:28.777652636Z"
parser_current() 1558961668777652
parser_1() 1558961668777652
parser_2() 1558961668777652

running for "2019-05-27T12:54:28.128Z"
parser_current() 1558961668128
parser_1() 1558961668128000
parser_2() 1558961668128000

running for "2019-05-27T12:54:28.120Z"
parser_current() 155896166812
parser_1() 1558961668120000
parser_2() 1558961668120000

running for "2019-05-27T12:54:28.100Z"
parser_current() 15589616681
parser_1() 1558961668100000
parser_2() 1558961668100000

running for "2019-05-27T12:54:28.1Z"
parser_current() 15589616681
parser_1() 1558961668100000
parser_2() 1558961668100000

running for "2019-05-27T12:54:29.993908166Z"
parser_current() 1558961669993908
parser_1() 1558961669993908
parser_2() 1558961669993908

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment