Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何在几秒钟内得到一个月_Go - Fatal编程技术网

Go 如何在几秒钟内得到一个月

Go 如何在几秒钟内得到一个月,go,Go,没有做一些相当简单的事情,并且找不到答案 也不是谷歌叔叔,也不是这里 我试图在几秒钟内得到一个完整的月(整数) 比这更优雅的东西: s := 3600 * 24 * 30 还尝试: m := time.Hour * 24 * 30 但这会返回类型“time.Duration”,我也不能将其转换为int 注:不太在意每个月(28-31日)的压力日 但如果可以使用特定的月份作为输入,它将是超级的 我不是一个Goxpert,所以去吧(看看我在那里做了什么?)放松点 提前谢谢 使用时

没有做一些相当简单的事情,并且找不到答案 也不是谷歌叔叔,也不是这里

我试图在几秒钟内得到一个完整的月(整数) 比这更优雅的东西:

    s := 3600 * 24 * 30
还尝试:

    m := time.Hour * 24 * 30
但这会返回类型“time.Duration”,我也不能将其转换为int

注:不太在意每个月(28-31日)的压力日 但如果可以使用特定的月份作为输入,它将是超级的

我不是一个Goxpert,所以去吧(看看我在那里做了什么?)放松点

提前谢谢

使用时间。持续时间秒()

包干管

import (
    "fmt"
    "time"
)

func main() {
    m := time.Hour * 24 * 30
    fmt.Println("In float: ", m.Seconds())
    fmt.Println("In int: ", int(m.Seconds()))
}
使用time.Duration Seconds()的示例:

包干管

import (
    "fmt"
    "time"
)

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

上的示例如果您确实关心月份中的不同天数,则可以使用
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
。这很优雅。

如果您确实关心月份中的不同日期,您可以使用
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
。一个月的天数取决于月份和年份(闰年或非闰年)。当转换到夏令时或从夏令时转换到夏令时,一天中的秒数会有所不同。因此,一个月内的秒数将因月份、年份和时区而异

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

比如说,

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

一个月的天数取决于月份和年份(闰年或非闰年)。当转换到夏令时或从夏令时转换到夏令时,一天中的秒数会有所不同。因此,一个月内的秒数将因月份、年份和时区而异

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

比如说,

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

同意前面的答案,但如果你真的想删除24*30,这是可能的

只是闲逛;时间。时间有一些很好的比较功能,所以这是另一个选项:

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()))
}

上的示例与前面的答案一致,但如果确实要删除24*30,则是可能的

只是闲逛;时间。时间有一些很好的比较功能,所以这是另一个选项:

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()))
}

因为你只有30天的月,我们这里有一个常数,绝对不需要任何计算。利用基础数学,我们简化了

30*24*60*60


因为你们只有30天的月份,我们这里有一个常数,绝对不需要任何计算。利用基础数学,我们简化了

30*24*60*60



感谢@wlredeye的帮助,希望有办法摆脱
24*30
alsotime。小时是表示持续时间常数的最大常数。所以我认为你不能避免使用multiplyThanks@wlredeye来达到这个目的,希望有办法摆脱
24*30
alsotime。小时是代表持续时间常数的最大常数。所以我认为你不能避免使用乘法,如果你不仅仅对“一个月30天有多少秒”感兴趣,那么答案是恒定的,30*24*60*60是完美的,完全优雅的。我完全同意沃尔克的观点,仅仅为了执行一些简单的乘法,就可以跳过各种各样的障碍,这并不是一件优雅的事情。30*24*60*60非常优雅。任何阅读代码的人都会立即意识到这一点,而且它不涉及不必要的函数调用和包导入。好的,谢谢,使用这个30天,使用@Danilo answer一个月,如果你不仅仅对“30天一个月有多少秒”感兴趣的话答案是恒定的,30*24*60*60是完美的,非常优雅。我完全同意沃尔克的观点,仅仅为了执行一些简单的乘法,就可以跳过各种各样的障碍,这并不是一件优雅的事情。30*24*60*60非常优雅。任何阅读代码的人都会立即意识到这一点,它不涉及不必要的函数调用和包导入。好的,谢谢,使用这个30天,使用@Danilo answer进行一个月的spesifc secoundsThanks@Danilo。我会投赞成票,但没有足够的声誉。别担心。如果它对你有效,你可以接受它作为答案。谢谢@Danilo。我会投赞成票,但没有足够的声誉。别担心。如果它对你有效,你可以接受它作为答案。由于偶然,你选择了一个30天的月。“在操场上,时间从2009-11-10 23:00:00 UTC开始”。看,加了整整30天variant@peterSO我想你不明白福的意图。他想指出AddDate函数在给定的时间上正好加上1个月。这是一个完美的解决方案。它处理各种月长。你偶然选择了一个30天的月。“在操场上,时间从2009-11-10 23:00:00 UTC开始”。看,加了整整30天variant@peterSO我想你不明白福的意图。他想指出AddDate函数在给定的时间上正好加上1个月。这是一个完美的解决方案。它处理各种月长。