如何将一个月转换为秒数

huangapple go评论112阅读模式
英文:

How to get one month in seconds

问题

无法做一些相当简单的事情,无法找到答案,无论是从谷歌大叔还是从这里。

我正在尝试将一个完整的月份转换为秒(一个整数)
有没有更优雅的方法:

s := 3600 * 24 * 30

也尝试过:

m := time.Hour * 24 * 30

但它返回的是'time.Duration'类型,我也无法将其转换为整数

注意:我并不真的关心每个月的天数(28-31)
但如果可以使用特定的月份作为输入,那就太好了。

我不是一个Goxpert,所以请对我温柔点..

提前谢谢。

英文:

Failing to do something fairly simple and can't find an answer
nor from uncle Google nor from here

I'm trying to get one full month in seconds (an int)
Something more elegant then this:

	s := 3600 * 24 * 30

also tried :

    m := time.Hour * 24 * 30

but that returns type 'time.Duration' which I also can't convert to an int

NOTE: Don't really care for the pressie days of each month (28-31)
but if it's possible to use specific month as input it will be super.

I'm not a Goxpert so Go (see what I did there?) easy on me..

Thanks in advance.

答案1

得分: 6

使用time.Duration的Seconds()方法:

package main

import (
	"fmt"
	"time"
)

func main() {
	m := time.Hour * 24 * 30
	fmt.Println("以浮点数形式输出:", m.Seconds())
	fmt.Println("以整数形式输出:", int(m.Seconds()))
}

playground上的示例

英文:

Use time.Duration Seconds() :

package main

import (
	"fmt"
	"time"
)

func main() {
	m := time.Hour * 24 * 30
	fmt.Println("In float: ", m.Seconds())
	fmt.Println("In int: ", int(m.Seconds()))
}

Example on playground

答案2

得分: 4

如果你关心每个月的天数差异,你可以使用time.Sub来获取持续时间:

package main

import (
  "fmt"
  "time"
)

func secondsInMonth(y int, m time.Month) int {
  start := time.Date(y, m, 0, 0, 0, 0, 0, time.UTC)
  end := time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC)
  return int(end.Sub(start).Seconds())
}

func main() {
  month := time.February
  year := 2016

  fmt.Printf("Days in %s %d: %d\n", month, year, secondsInMonth(year, month))
}

如果你不关心,只需使用30*24*60*60。这是最简洁的方法。

英文:

If you do care about the different days in months, you can use time.Sub to get the duration:

package main

import (
  "fmt"
  "time"
)

func secondsInMonth(y int, m time.Month) int {
  start := time.Date(y, m, 0, 0, 0, 0, 0, time.UTC)
  end := time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC)
  return int(end.Sub(start).Seconds())
}

func main() {
  month := time.February
  year := 2016

  fmt.Printf("Days in %s %d: %d\n", month, year, secondsInMonth(year, month))
}

If you dont care just do 30*24*60*60. Thats as elegant as it gets.

答案3

得分: 0

同意之前的答案,但如果你真的想要去除24 * 30,是有可能的。

只是闲逛一下;time.Time有一些很好的比较功能,所以这是另一种选择:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

    // 当前月份的秒数 - 可能会有变化
	duration := now.AddDate(0, 1, 0).Sub(now)
	fmt.Println("以整数表示:", int(duration.Seconds()))

    // 类似的,但确切为30天
	duration := now.AddDate(0, 0, 30).Sub(now)
	fmt.Println("以整数表示:", int(duration.Seconds()))
}

playground上的示例

英文:

Agree with the previous answers, but if you really want to remove the 24 * 30 it is possible.

Just goofing around; time.Time has some nice comparison features so this is another option:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

    // the current month in seconds - can vary
	duration := now.AddDate(0, 1, 0).Sub(now)
	fmt.Println("In int: ", int(duration.Seconds()))

    // similar, but exactly 30 days
	duration := now.AddDate(0, 0, 30).Sub(now)
	fmt.Println("In int: ", int(duration.Seconds()))
}

Example on playground

答案4

得分: -1

一个月的天数取决于月份和年份(闰年或非闰年)。一天中的秒数在转换到夏令时或从夏令时转换时会有所变化。因此,一个月的秒数会根据月份、年份和时区而有所变化。

例如,洛杉矶位于太平洋时区。一月和三月都有31天。由于三月份有夏令时的转换,所以三月比一月少一个小时(3600秒)。

以下是示例代码:

package main

import (
	"fmt"
	"time"
)

func secondsInMonth(month time.Month, year int, loc *time.Location) int {
	u := time.Date(year, month, 1, 0, 0, 0, 0, loc)
	v := time.Date(year, month+1, 1, 0, 0, 0, 0, loc)
	return int(v.Sub(u).Seconds())
}

func main() {
	loc, err := time.LoadLocation("America/Los_Angeles")
	if err != nil {
		fmt.Println(err)
		return
	}
	year := 2016
	jan := secondsInMonth(time.January, year, loc)
	feb := secondsInMonth(time.February, year, loc)
	mar := secondsInMonth(time.March, year, loc)
	apr := secondsInMonth(time.April, year, loc)
	fmt.Println(jan, feb, mar, apr)
	fmt.Println(jan - mar)
}

输出结果:

2678400 2505600 2674800 2592000
3600
英文:

The number of days in a month depends on the month and the year (leap or non-leap year). The number of seconds in a day varies when there is a transition to or from daylight savings time. Therefore, the number of seconds in a month will vary by month, year, and time zone.

For example, Los Angeles is in the Pacific Time Zone. January and March both have 31 days. Because of the daylight savings time transition in March, there is one hour less (3600 = 60*60 seconds) in March than January.

For example,

package main

import (
	"fmt"
	"time"
)

func secondsInMonth(month time.Month, year int, loc *time.Location) int {
	u := time.Date(year, month, 1, 0, 0, 0, 0, loc)
	v := time.Date(year, month+1, 1, 0, 0, 0, 0, loc)
	return int(v.Sub(u).Seconds())
}

func main() {
	loc, err := time.LoadLocation("America/Los_Angeles")
	if err != nil {
		fmt.Println(err)
		return
	}
	year := 2016
	jan := secondsInMonth(time.January, year, loc)
	feb := secondsInMonth(time.February, year, loc)
	mar := secondsInMonth(time.March, year, loc)
	apr := secondsInMonth(time.April, year, loc)
	fmt.Println(jan, feb, mar, apr)
	fmt.Println(jan - mar)
}

Output:

2678400 2505600 2674800 2592000
3600

答案5

得分: -1

由于你只有30天的月份,这里有一个常数,绝对不需要进行任何计算。使用基本数学,我们简化

30*24*60*60

const secsInMonth = 2592000
英文:

Since you are only having 30 day months, we have a constant here and there is absolutely no need for any calculation. Using basic maths, we simplify

30*24*60*60

to

const secsInMonth = 2592000

huangapple
  • 本文由 发表于 2016年2月18日 20:37:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35481704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定