Cálculo del número total de horas, días, minutos y segundos entre dos fechas en Golang

Para obtener el número de días entre dos fechas, podemos calcular el número de días desde el principio 0/0/0000 hasta la primera fecha y la segunda fecha y calcular su diferencia.

Ejemplos:

Input: 2020-04-27 23:35:00, 2018-5-12 12:43:23
Output: 716 days, 10 hours, 51 minutes, 37 seconds

Input: 2014-02-25 05:50:20, 2003-12-13 21:42:12
Output: 3726 days, 8 hours, 8 minutes, 8 seconds

Para calcular la diferencia horaria, podemos calcular horas2-horas1, minutos2-minutos1, segundos2-segundos1 y realizar algunas operaciones (que se muestran en el código de la línea 65 a la línea 80) para evitar valores negativos.

// Golang program for Calculating the total number
// of Hours, Days, Minutes and Seconds 
// between two dates
package main
  
import (
    "fmt"
    "time"
)
  
// This function counts the
// number of leap years
// since the starting of time
// to the current year that
// is passed
func leapYears(date time.Time) (leaps int) {
  
    // returns year, month,
    // date of a time object
    y, m, _ := date.Date()
  
    if m <= 2 {
        y--
    }
    leaps = y/4 + y/400 - y/100
    return leaps
}
  
// The function calculates the
// difference between two dates and times
// and returns the days, hours, minutes,
// seconds between two dates
  
func getDifference(a, b time.Time) (days, hours, minutes, seconds int) {
  
    // month-wise days
    monthDays := [12]int{31, 28, 31, 30, 31,
                 30, 31, 31, 30, 31, 30, 31}
  
    // extracting years, months,
    // days of two dates
    y1, m1, d1 := a.Date()
    y2, m2, d2 := b.Date()
  
    // extracting hours, minutes,
    // seconds of two times
    h1, min1, s1 := a.Clock()
    h2, min2, s2 := b.Clock()
  
    // totalDays since the 
    // beginning = year*365 + number_of_days
    totalDays1 := y1*365 + d1
  
    // adding days of the months 
    // before the current month
    for i := 0; i < (int)(m1)-1; i++ {
        totalDays1 += monthDays[i]
    }
  
    // counting leap years since
    // beginning to the year "a"
    // and adding that many extra 
    // days to the totaldays
    totalDays1 += leapYears(a)
  
    // Similar procedure for second date
    totalDays2 := y2*365 + d2
  
    for i := 0; i < (int)(m2)-1; i++ {
        totalDays2 += monthDays[i]
    }
  
    totalDays2 += leapYears(b)
  
    // Number of days between two days
    days = totalDays2 - totalDays1
  
    // calculating hour, minutes,
    // seconds differences
    hours = h2 - h1
    minutes = min2 - min1
    seconds = s2 - s1
  
    // if seconds difference goes below 0,
    // add 60 and decrement number of minutes
    if seconds < 0 {
        seconds += 60
        minutes--
    }
  
    // performing similar operations
    // on minutes and hours
    if minutes < 0 {
        minutes += 60
        hours--
    }
  
    // performing similar operations
    // on hours and days
    if hours < 0 {
        hours += 24
        days--
    }
  
    return days, hours, minutes, seconds
  
}
  
// Driver code
  
func main() {
  
    // Syntax for time date:
    // d := time.Date(year, month, days, hours,
    // minutes, seconds, nanoseconds, timeZone)
  
    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 occurs after date2 then 
    // swap days since absolute 
    // difference is being calculated
    if date1.After(date2) {
        date1, date2 = date2, date1
    }
    // Calling function and getting
    // difference between two dates
    days, hours, minutes, seconds := getDifference(date1, date2)
  
    // Printing the difference
    fmt.Printf("%v days, %v hours, %v minutes, %v seconds",
                             days, hours, minutes, seconds)
  
}

Producción:

716 days, 10 hours, 51 minutes, 37 seconds

Publicación traducida automáticamente

Artículo escrito por TanmayThaakur y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *