2

Any quick ways to get the time and convert to timestamp?

string="2018-10-17 12:31:46 UTC::@:[2771]:LOG: checkpoint starting: time"

I can use split to get the first 4

>>> string.split(":")[:3]
['2018-10-17 12', '31', '46 UTC']

But how to merge them back to a time string and convert to timestamp?

Update

@jezrael Use your solution, I convert it to timestamp, but seems the time is drifted.

>>> date = parser.parse(':'.join(string.split(":")[:3]))
>>> print(date)
2018-10-17 12:31:46+00:00
>>> timestamp = int(round(time.mktime(date.timetuple()) * 1000))
>>> print(timestamp)
1539743506000

I used below codes to upload logs to cloudwatch, it used the date/timestamp I got from log string.

 logs = boto3.client('logs')

 date = parser.parse(':'.join(string.split(":")[:3]))
 timestamp = int(round(time.mktime(date.timetuple()) * 1000))
 event_response = logs.put_log_events(
    logGroupName=LOG_GROUP,
    logStreamName=LOG_STREAM,
    logEvents=[{
        'timestamp': timestamp,
        'message': string
    }],
    sequenceToken=str(next_sequence_token))

The date with real logs in coudwatch is different:

enter image description here

Update #2

Finally I did with below codes, it needs python v3.3+

$ python3
Python 3.7.0 (default, Oct  4 2018, 14:10:21)
[Clang 10.0.0 (clang-1000.10.44.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from dateutil import parser
>>> string="2018-10-17 12:31:46 UTC::@:[2771]:LOG: checkpoint starting: time"
>>> date = parser.parse(':'.join(string.split(":")[:3]))
>>> timestamp = int(round(date.timestamp() * 1000))
>>> print(timestamp)
1539779506000
Bill
  • 2,494
  • 5
  • 26
  • 61
  • What return for you `print (date)` ? – jezrael Oct 18 '18 at 06:29
  • Sure, I have updated – Bill Oct 18 '18 at 06:53
  • I find [this](https://stackoverflow.com/a/8778548) and I believe need last 2 lines of answer `utc_naive = dt.replace(tzinfo=None) - dt.utcoffset() timestamp = (utc_naive - datetime(1970, 1, 1)).total_seconds()` – jezrael Oct 18 '18 at 07:28
  • forget that code. Your codes resolves the first part of my question, I need get timestamp, how to do that? – Bill Oct 18 '18 at 07:42
  • Do you think `unix timestamp` ? Solution from comment above not working? Because here is `utc` datetime, so `time.mktime(date.timetuple())` cannot be used [check this](https://stackoverflow.com/questions/19801727/convert-datetime-to-unix-timestamp-and-convert-it-back-in-python#comment44234138_27914405) – jezrael Oct 18 '18 at 07:47
  • Thanks, finally I did with `date.timestamp()`, it needs python v3.3+ – Bill Oct 18 '18 at 12:10
  • 1
    Super, I am really happy. Datetimes are not easy :( – jezrael Oct 18 '18 at 12:11

2 Answers2

4

We can use join with parser module.

from dateutil import parser

string="2018-10-17 12:31:46 UTC::@:[2771]:LOG: checkpoint starting: time"

date = parser.parse(':'.join(string.split(":")[:3]))
print (date)
2018-10-17 12:31:46+00:00
CodeIt
  • 3,492
  • 3
  • 26
  • 37
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

for me the simple way is split by UTC:: and use the parser from dateutil:

from dateutil import parser
string="2018-10-17 12:31:46 UTC::@:[2771]:LOG: checkpoint starting: time"
parser.parse(string.split(' UTC::')[0])

the result is

datetime.datetime(2018, 10, 17, 12, 31, 46)
Brown Bear
  • 19,655
  • 10
  • 58
  • 76