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.

`time` module does not translate well!

See original GitHub issue

Describe the bug when using methods from the time module, the only method that is translated properly is time.sleep(), while the others result the same python code in Go.

Python code example

import time

def main():
    print(time.time())
    print(time.time_ns())
    print(time.ctime(time.time()))

if __name__ == '__main__':
    main()

Current behavior the above three methods are not being translated into Go code but rather written as is in python, and this code is generated. which is not a valid Go code!

package main

import "fmt"

func main() {
	fmt.Println(time.time())
	fmt.Println(time.time_ns())
	fmt.Println(time.ctime(time.time()))
}

Expected behavior

  1. time.time() should be translated into time.Now().Unix() in Go
  2. time.time_ns() should be translated into time.Now().UnixNano() in Go
  3. time.ctime(time.time()) should be translated into time.Now() in Go

Correct Go code example

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().Unix())
	fmt.Println(time.Now().UnixNano())
	fmt.Println(time.Now())
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mbaraacommented, Jul 15, 2021

@mbaraa But they return different types, I believe.

https://pkg.go.dev/time#Time.Unix - int64 (second precision) https://docs.python.org/3/library/time.html#time.time - float (precision better than 1 second on most systems)

I agree it’s a bit weird but I’m not sure of a better way to get nanosecond precision on a float like Python does. Example where this kinda matters:

t1 = time.time()
half_second_operation()
t2 = time.time()
print(t2 - t1)

If you want that to print approximately 0.5 in Go, you’d need to preserve that information right?

I could add an explicit case for int(time.time()) to simplify it to time.Now().Unix()

aha, I thought time.time() returns an int in python. my bad 😅

1reaction
nottheswimmercommented, Jul 15, 2021

@mbaraa But they return different types, I believe.

https://pkg.go.dev/time#Time.Unix - int64 (second precision) https://docs.python.org/3/library/time.html#time.time - float (precision better than 1 second on most systems)

I agree it’s a bit weird but I’m not sure of a better way to get nanosecond precision on a float like Python does. Example where this kinda matters:

t1 = time.time()
half_second_operation()
t2 = time.time()
print(t2 - t1)

If you want that to print approximately 0.5 in Go, you’d need to preserve that information right?

I could add an explicit case for int(time.time()) to simplify it to time.Now().Unix()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy-loaded modules dont get their translations when "isolate ...
When "isolate" is true the file is correctly loaded but the module doesn't have access to previously loaded translations. Expected behavior.
Read more >
A Beginner's Guide to the Python time Module
Python's time module provides a function for getting local time from the number of seconds elapsed since the epoch called localtime() . Notice...
Read more >
Installing a module causes translations to be overwritten
e) Go to admin/modules and install the core Activity Tracker module. Translations are imported. Note: I have no idea why translations would be ......
Read more >
trying to convert a whole column from Spanish to English, with ...
I'm trying to convert a column containing Spanish tweets (removi stop words, tokenize and stemming process were done) to English with the ...
Read more >
Module 3: Translation Cost and Turnaround Times
When you kick off ("authorize") a new translation request, you can select not only the required target languages, but also the preferred workflow...
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