golang time.Duration calculation

1,475 views
Skip to first unread message

Tong Sun

unread,
Jul 6, 2016, 5:43:55 PM7/6/16
to golang-nuts
I know golang time.Duration supports basic calculations. but the problem I'm trying to answer is, 

How many 100 seconds does one hour has?

This is my code:

It's not fully working. Please help. Thanks

Tong Sun

unread,
Jul 6, 2016, 5:51:53 PM7/6/16
to golang-nuts
oh, please make it 65 seconds, I need the float point calculations.

Rob Pike

unread,
Jul 6, 2016, 5:53:37 PM7/6/16
to Tong Sun, golang-nuts
Just divide the durations: https://play.golang.org/p/ILE11eLzSx



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tong Sun

unread,
Jul 6, 2016, 5:55:18 PM7/6/16
to golang-nuts, sunto...@gmail.com

oh, please make it 65 seconds

https://play.golang.org/p/JiftSjddjx

I need the float point calculations. I.e., I need to result to be 55.38, instead of 55. 

Tong Sun

unread,
Jul 6, 2016, 6:01:22 PM7/6/16
to golang-nuts, sunto...@gmail.com

To make the point, let's use 68 seconds


I need the result to be 52.94, instead of 52. 

Dan Kortschak

unread,
Jul 6, 2016, 6:21:20 PM7/6/16
to Tong Sun, golang-nuts
Type convert *prior* to the division.

https://play.golang.org/p/7cwTFu_3im

On Wed, 2016-07-06 at 15:01 -0700, Tong Sun wrote:
> To make the point, let's use *68 seconds*,

Jakob Borg

unread,
Jul 7, 2016, 4:21:30 AM7/7/16
to Tong Sun, golang-nuts
Or, as an alternative, use the built in methods to get seconds in
floating point:

https://play.golang.org/p/euWSlfDf3Y

//jb
Reply all
Reply to author
Forward
0 new messages