Go语言程序计算两个时间段之间的差值


在本教程中,我们将编写一个Go语言程序来计算以下程序中给定的两个时间段之间的差值。为了获得两个时间段之间的时差,我们可以使用库函数,也可以创建一个单独的用户定义函数来实现结果。

方法一:使用内置函数计算两个时间段之间的差值

在这种方法中,我们将编写一个Go语言程序,使用Go语言中预定义的函数来查找时间段之间的差值。

语法

func (t Time) Sub(u Time) Duration

Go语言中的`sub()`函数用于获取两个日期之间的差值。在这个函数中,前两个参数(即`t`和`u`)是日期值,该函数以小时、分钟和秒返回两个值之间的差值。

算法

步骤1 − 首先,我们需要导入`fmt`和`time`包。`time`包允许我们使用其他预定义的包,例如`time.Date()`。

步骤2 − 启动`main()`函数。

步骤3 − 通过按`yy/mm/dd/hrs/min/sec`的顺序在`time.Date()`函数中传递日期和时间来初始化`firstDate`和`secondDate`变量。

步骤4 − 使用`Sub()`函数查找给定日期之间的差值,此函数以第二个日期作为参数并计算所需的差值。

步骤5 − 以各种格式在屏幕上打印结果。

步骤6 − 我们可以打印年份、天数、月份、周数、小时、秒、毫秒等。

示例

使用内置函数计算两个时间段之间差值的Go语言程序

package main
import (
   "fmt"
   "time"
)
func main() {
   firstDate := time.Date(2022, 4, 13, 3, 0, 0, 0, time.UTC)
   secondDate := time.Date(2010, 2, 12, 6, 0, 0, 0, time.UTC)
   difference := firstDate.Sub(secondDate)
   fmt.Println("The difference between dates", firstDate, "and", secondDate, "is: ")
   fmt.Printf("Years: %d\n", int64(difference.Hours()/24/365))
   fmt.Printf("Months: %d\n", int64(difference.Hours()/24/30))
   fmt.Printf("Weeks: %d\n", int64(difference.Hours()/24/7))
   fmt.Printf("Days: %d\n", int64(difference.Hours()/24))
   fmt.Printf("Hours: %.f\n", difference.Hours())
   fmt.Printf("Minutes: %.f\n", difference.Minutes())
   fmt.Printf("Seconds: %.f\n", difference.Seconds())
   fmt.Printf("Nanoseconds: %d\n", difference.Nanoseconds())
}

输出

The difference between dates 2022-04-13 03:00:00 +0000 UTC and 2010-02-12
06:00:00 +0000 UTC is:
Years: 12
Months: 148
Weeks: 634
Days: 4442
Hours: 106629
Minutes: 6397740
Seconds: 383864400
Nanoseconds: 383864400000000000

方法二:使用用户定义函数计算两个时间段之间的差值

在这种方法中,我们将创建一个不同的函数来计算两个提供的日期之间的差值。该函数将以两个日期作为参数并返回相应的结果。

算法

步骤1 − 首先,我们需要导入`fmt`和`time`包。`time`包允许我们使用其他预定义的函数,例如`time.Date()`。

步骤2 − 创建`leapYear()`函数来计算两个日期之间的闰年数。

步骤3 − 还创建`getDifference()`函数来获取日期和时间之间的差值。`getDifference()`函数返回天数、小时、分钟和秒。

步骤4 − 启动`main()`函数。

步骤5 − 通过按`yy/mm/dd/hrs/min/sec`的顺序在`time.Date()`函数中传递日期和时间来初始化`date1`和`date2`变量。

步骤6 − 如果`date1`发生在`date2`之后,则交换这两个日期。

步骤7 − 通过将两个日期作为参数传递给函数来调用`getDifference()`。

步骤8 − 将函数获得的结果存储在不同的变量中,并使用`fmt.Println()`函数以不同的格式在屏幕上打印它们。

示例

使用用户定义函数计算两个时间段之间差值的Go语言程序

package main
import (
   "fmt"
   "time"
)
func leapYears(date time.Time) (leaps int) {
   y, m, _ := date.Date()
   if m <= 2 {
      y--
   }
   leaps = y/4 + y/400 - y/100
   return leaps
}
func getDifference(a, b time.Time) (days, hours, minutes, seconds int) {
   monthDays := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
   y1, m1, d1 := a.Date()
   y2, m2, d2 := b.Date()
   h1, min1, s1 := a.Clock()
   h2, min2, s2 := b.Clock()
   totalDays1 := y1*365 + d1
   for i := 0; i < (int)(m1)-1; i++ {
      totalDays1 += monthDays[i]
   }
   totalDays1 += leapYears(a)
   totalDays2 := y2*365 + d2
   for i := 0; i < (int)(m2)-1; i++ {
      totalDays2 += monthDays[i]
   }
   totalDays2 += leapYears(b)
   days = totalDays2 - totalDays1
   hours = h2 - h1
   minutes = min2 - min1
   seconds = s2 - s1
   if seconds < 0 {
      seconds += 60
      minutes--
   }
   if minutes < 0 {
      minutes += 60
      hours--
   }
   if hours < 0 {
      hours += 24
      days--
   }
   return days, hours, minutes, seconds
}
func main() {
   date1 := time.Date(2020, 4, 27, 23, 35, 0, 0, time.UTC)
   date2 := time.Date(2018, 5, 12, 12, 43, 23, 0, time.UTC)
   if date1.After(date2) {
      date1, date2 = date2, date1
   }
   days, hours, minutes, seconds := getDifference(date1, date2)
   fmt.Println("The difference between dates", date1, "and", date2, "is: ")
   fmt.Printf("%v days\n%v hours\n%v minutes\n%v seconds", days, hours, minutes, seconds)
}

输出

The difference between dates 2018-05-12 12:43:23 +0000 UTC and 2020-04-27
23:35:00 +0000 UTC is:
716 days
10 hours
51 minutes
37 seconds

结论

我们已经成功地编译并执行了一个Go语言程序来获取两个时间段之间的差值以及示例。在第一个示例中,我们使用了内置函数;在第二个示例中,我们使用了用户定义函数来获得结果。

更新于:2023年1月10日

11K+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.