question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

all: refactor by extraction code for conversion from timestampStr to microseconds

See original GitHub issue

I noticed while reviewing https://github.com/census-instrumentation/opencensus-python/pull/327#discussion_r220030262 that there is a legacy duplication of code to convert between timestamp strings to microseconds that is https://github.com/census-instrumentation/opencensus-python/blob/c96083589792a0c278193af1a227c21f9c6c6e97/opencensus/trace/exporters/zipkin_exporter.py#L154-L167

and with that PR there will be three sites with the same code pattern.

I propose that we refactor that code by extracting it to a small helper function

def datetimeStrToMicroseconds(timestamp):
   """
   Convert a timestamp string into a microsecond value as required by Zipkin.
   It returns an int
   """
   dt = datetime.datetime.strptime(timestamp, ISO_DATETIME_REGEX)
   mUs = (calendar.timegm(dt.timetuple()) * 1e6) + dt.microsecond
   return int(round(mUs))

which will then reduce all those call sites to simply

start_timestamp_mus = datetimeStrToMicroseconds(span.start_time)
end_timestamp_mus = datetimeStrToMicroseconds(span.end_time)
epoch_time_mus = datetimeStrToMicroseconds(time_event.timestamp)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
odeke-emcommented, Sep 25, 2018

Great catch, perhaps then we need to put that in the utilities path if any exists, so that we can use that function across the entire repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

esl/usec: Convert anything to microseconds and back. - GitHub
Converts anything* to microseconds and back. Tired of implementing the same timestamp conversion functions in different projects under different names?
Read more >
Get a timestamp in C in microseconds? - Stack Overflow
You have two choices for getting a microsecond timestamp. ... To // convert the whole struct to nanoseconds, do this: nanoseconds ...
Read more >
Refactoring 008 — Convert Variables to Constant
I call that variable a constant. “Refactoring 008 — Convert Variables to Constant” is published by Maximiliano Contieri in Level Up Coding.
Read more >
Convert | AppCode Documentation - JetBrains
Convert refactorings allow converting methods to functions or blocks and vice versa, as well as converting properties to instance variables.
Read more >
Java Program to Convert Milliseconds to Minutes and Seconds
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found