How to get the seconds of day [on hold]2019 Community Moderator ElectionJoda Time getSecondOfDay on DST switch dayHow to get the current time in PythonAdd days to JavaScript DateHow to convert Milliseconds to “X mins, x seconds” in Java?How do I get time of a Python program's execution?How to efficiently concatenate strings in Go?Convert seconds to Hour:Minute:SecondSimple date class in JavaGet current time and date on AndroidHow do you write multiline strings in Go?How do i add years, months, days, hours, minutes or seconds to JavaScript Date object?

Can a Mimic (container form) actually hold loot?

Align equations with text before one of them

How to chmod files that have a specific set of permissions

How do you make a gun that shoots melee weapons and/or swords?

What is the oldest European royal house?

How spaceships determine each other's mass in space?

Preparing as much as possible of a cake in advance

Is divide-by-zero a security vulnerability?

The (Easy) Road to Code

Can inspiration allow the Rogue to make a Sneak Attack?

What is the meaning of option 'by' in TikZ Intersections

Why is there an extra space when I type "ls" on the Desktop?

Error in TransformedField

Where do you go through passport control when transiting through another Schengen airport on your way out of the Schengen area?

ESPP--any reason not to go all in?

Convert an array of objects to array of the objects' values

What is better: yes / no radio, or simple checkbox?

Remove object from array based on array of some property of that object

Does the US political system, in principle, allow for a no-party system?

Do natural melee weapons (from racial traits) trigger Improved Divine Smite?

“I had a flat in the centre of town, but I didn’t like living there, so …”

Why won't the strings command stop?

Sundering Titan and basic normal lands and snow lands

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?



How to get the seconds of day [on hold]



2019 Community Moderator ElectionJoda Time getSecondOfDay on DST switch dayHow to get the current time in PythonAdd days to JavaScript DateHow to convert Milliseconds to “X mins, x seconds” in Java?How do I get time of a Python program's execution?How to efficiently concatenate strings in Go?Convert seconds to Hour:Minute:SecondSimple date class in JavaGet current time and date on AndroidHow do you write multiline strings in Go?How do i add years, months, days, hours, minutes or seconds to JavaScript Date object?










-6















How to get seconds of day (1 - 86400) in Go?



Just like http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractDateTime.html#getSecondOfDay()



Update/Clarifications



  • Wanted equivalent of the joda/SecondOfDay in golang

  • Expected to start at 0 and 86400 at end of the day

  • Required when rewriting in golang a java opensource function which in turn is using joda/secondOfDay

  • Googled '24 hour to seconds' to get 86400

  • While asking question I could only think of now.Unix()-yesterdayMidnight.Unix() and the simple accepted answer didn't come to my mind

  • Obviously didn't think about Daylight Saving

  • Wanted to see if there is some built in function or popular/standard library









share|improve this question















put on hold as unclear what you're asking by Flimzy, Volker, peterSO, John Dvorak, gnat yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • 2





    What have you tried? Include your code. What problems did you encounter?

    – Flimzy
    yesterday






  • 3





    "seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

    – peterSO
    yesterday















-6















How to get seconds of day (1 - 86400) in Go?



Just like http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractDateTime.html#getSecondOfDay()



Update/Clarifications



  • Wanted equivalent of the joda/SecondOfDay in golang

  • Expected to start at 0 and 86400 at end of the day

  • Required when rewriting in golang a java opensource function which in turn is using joda/secondOfDay

  • Googled '24 hour to seconds' to get 86400

  • While asking question I could only think of now.Unix()-yesterdayMidnight.Unix() and the simple accepted answer didn't come to my mind

  • Obviously didn't think about Daylight Saving

  • Wanted to see if there is some built in function or popular/standard library









share|improve this question















put on hold as unclear what you're asking by Flimzy, Volker, peterSO, John Dvorak, gnat yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • 2





    What have you tried? Include your code. What problems did you encounter?

    – Flimzy
    yesterday






  • 3





    "seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

    – peterSO
    yesterday













-6












-6








-6








How to get seconds of day (1 - 86400) in Go?



Just like http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractDateTime.html#getSecondOfDay()



Update/Clarifications



  • Wanted equivalent of the joda/SecondOfDay in golang

  • Expected to start at 0 and 86400 at end of the day

  • Required when rewriting in golang a java opensource function which in turn is using joda/secondOfDay

  • Googled '24 hour to seconds' to get 86400

  • While asking question I could only think of now.Unix()-yesterdayMidnight.Unix() and the simple accepted answer didn't come to my mind

  • Obviously didn't think about Daylight Saving

  • Wanted to see if there is some built in function or popular/standard library









share|improve this question
















How to get seconds of day (1 - 86400) in Go?



Just like http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractDateTime.html#getSecondOfDay()



Update/Clarifications



  • Wanted equivalent of the joda/SecondOfDay in golang

  • Expected to start at 0 and 86400 at end of the day

  • Required when rewriting in golang a java opensource function which in turn is using joda/secondOfDay

  • Googled '24 hour to seconds' to get 86400

  • While asking question I could only think of now.Unix()-yesterdayMidnight.Unix() and the simple accepted answer didn't come to my mind

  • Obviously didn't think about Daylight Saving

  • Wanted to see if there is some built in function or popular/standard library






go time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 hours ago







yodhevauhe

















asked yesterday









yodhevauheyodhevauhe

3251825




3251825




put on hold as unclear what you're asking by Flimzy, Volker, peterSO, John Dvorak, gnat yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









put on hold as unclear what you're asking by Flimzy, Volker, peterSO, John Dvorak, gnat yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2





    What have you tried? Include your code. What problems did you encounter?

    – Flimzy
    yesterday






  • 3





    "seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

    – peterSO
    yesterday












  • 2





    What have you tried? Include your code. What problems did you encounter?

    – Flimzy
    yesterday






  • 3





    "seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

    – peterSO
    yesterday







2




2





What have you tried? Include your code. What problems did you encounter?

– Flimzy
yesterday





What have you tried? Include your code. What problems did you encounter?

– Flimzy
yesterday




3




3





"seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

– peterSO
yesterday





"seconds of day (1 - 86400)." During the transition to and from daylight saving time there are more or less than 86,400 seconds in a day.

– peterSO
yesterday












2 Answers
2






active

oldest

votes


















1














Note:
This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.



Update:
Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).



package main 

import (
"fmt"
"time"
)

func getSecondOfDay(t time.Time) int
return 60*60*t.Hour() + 60*t.Minute() + t.Second()


func main()
t := time.Now()
fmt.Println(getSecondOfDay(t))






share|improve this answer










New contributor




perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

    – icza
    yesterday






  • 1





    Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

    – icza
    yesterday







  • 1





    The one option you have still gives invalid results on days when daylight saving time happens.

    – icza
    yesterday






  • 1





    @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

    – icza
    yesterday






  • 1





    @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

    – perl
    yesterday



















3














If we define "seconds of day" as the "elapsed seconds since midnight", then to get correct result even on days when daylight saving time happens we should subtract the time representing midnight from the given time. For that, we may use Time.Sub().



func daySeconds(t time.Time) int 
year, month, day := t.Date()
t2 := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
return int(t.Sub(t2).Seconds())



Testing it:



for _, t := range []time.Time
time.Date(2019, 1, 1, 0, 0, 30, 0, time.UTC),
time.Date(2019, 1, 1, 0, 1, 30, 0, time.UTC),
time.Date(2019, 1, 1, 0, 12, 30, 0, time.UTC),
time.Date(2019, 1, 1, 12, 12, 30, 0, time.UTC),

fmt.Println(daySeconds(t))



Output (try it on the Go Playground):



30
90
750
43950


Let's see how this function gives correct result when daylight saving time happens. In Hungary, 25 March 2018 is a day when the clock was turned forward 1 hour at 02:00:00, from 2 am to 3 am.



loc, err := time.LoadLocation("CET")
if err != nil
fmt.Println(err)
return


t := time.Date(2018, 3, 25, 0, 0, 30, 0, loc)
fmt.Println(t)
fmt.Println(daySeconds(t))

t = t.Add(2 * time.Hour)
fmt.Println(t)
fmt.Println(daySeconds(t))


This outputs (try it on the Go Playground):



2018-03-25 00:00:30 +0100 CET
30
2018-03-25 03:00:30 +0200 CEST
7230


We print the daySeconds of a time being 30 seconds after midnight, which is 30 of course. Then we add 2 hours to the time (2 hours = 2*3600 seconds = 7200), and the daySeconds of this new time will be properly 7200 + 30 = 7230, even though the time changed 3 hours.






share|improve this answer































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Note:
    This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.



    Update:
    Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).



    package main 

    import (
    "fmt"
    "time"
    )

    func getSecondOfDay(t time.Time) int
    return 60*60*t.Hour() + 60*t.Minute() + t.Second()


    func main()
    t := time.Now()
    fmt.Println(getSecondOfDay(t))






    share|improve this answer










    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.















    • 2





      This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

      – icza
      yesterday






    • 1





      Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

      – icza
      yesterday







    • 1





      The one option you have still gives invalid results on days when daylight saving time happens.

      – icza
      yesterday






    • 1





      @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

      – icza
      yesterday






    • 1





      @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

      – perl
      yesterday
















    1














    Note:
    This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.



    Update:
    Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).



    package main 

    import (
    "fmt"
    "time"
    )

    func getSecondOfDay(t time.Time) int
    return 60*60*t.Hour() + 60*t.Minute() + t.Second()


    func main()
    t := time.Now()
    fmt.Println(getSecondOfDay(t))






    share|improve this answer










    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.















    • 2





      This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

      – icza
      yesterday






    • 1





      Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

      – icza
      yesterday







    • 1





      The one option you have still gives invalid results on days when daylight saving time happens.

      – icza
      yesterday






    • 1





      @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

      – icza
      yesterday






    • 1





      @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

      – perl
      yesterday














    1












    1








    1







    Note:
    This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.



    Update:
    Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).



    package main 

    import (
    "fmt"
    "time"
    )

    func getSecondOfDay(t time.Time) int
    return 60*60*t.Hour() + 60*t.Minute() + t.Second()


    func main()
    t := time.Now()
    fmt.Println(getSecondOfDay(t))






    share|improve this answer










    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.










    Note:
    This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.



    Update:
    Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).



    package main 

    import (
    "fmt"
    "time"
    )

    func getSecondOfDay(t time.Time) int
    return 60*60*t.Hour() + 60*t.Minute() + t.Second()


    func main()
    t := time.Now()
    fmt.Println(getSecondOfDay(t))







    share|improve this answer










    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer








    edited yesterday





















    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered yesterday









    perlperl

    39917




    39917




    New contributor




    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    perl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    • 2





      This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

      – icza
      yesterday






    • 1





      Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

      – icza
      yesterday







    • 1





      The one option you have still gives invalid results on days when daylight saving time happens.

      – icza
      yesterday






    • 1





      @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

      – icza
      yesterday






    • 1





      @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

      – perl
      yesterday













    • 2





      This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

      – icza
      yesterday






    • 1





      Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

      – icza
      yesterday







    • 1





      The one option you have still gives invalid results on days when daylight saving time happens.

      – icza
      yesterday






    • 1





      @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

      – icza
      yesterday






    • 1





      @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

      – perl
      yesterday








    2




    2





    This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

    – icza
    yesterday





    This doesn't work on days when daylight saving happens. Also, Time.Truncate() operates on time as an absolute duration since the zero time, so it may also return incorrect result.

    – icza
    yesterday




    1




    1





    Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

    – icza
    yesterday






    Read the doc of Time.Truncate(). Here's an example: play.golang.org/p/hAfBrYWNeZ

    – icza
    yesterday





    1




    1





    The one option you have still gives invalid results on days when daylight saving time happens.

    – icza
    yesterday





    The one option you have still gives invalid results on days when daylight saving time happens.

    – icza
    yesterday




    1




    1





    @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

    – icza
    yesterday





    @perl Exactly my point. If the clock is turned back, on that day there is more than 86400 seconds. So reporting more is the correct answer.

    – icza
    yesterday




    1




    1





    @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

    – perl
    yesterday






    @icza: I have added a comment to my answer to clarify the point, and referred to your answer for the "seconds since midnight" version

    – perl
    yesterday














    3














    If we define "seconds of day" as the "elapsed seconds since midnight", then to get correct result even on days when daylight saving time happens we should subtract the time representing midnight from the given time. For that, we may use Time.Sub().



    func daySeconds(t time.Time) int 
    year, month, day := t.Date()
    t2 := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
    return int(t.Sub(t2).Seconds())



    Testing it:



    for _, t := range []time.Time
    time.Date(2019, 1, 1, 0, 0, 30, 0, time.UTC),
    time.Date(2019, 1, 1, 0, 1, 30, 0, time.UTC),
    time.Date(2019, 1, 1, 0, 12, 30, 0, time.UTC),
    time.Date(2019, 1, 1, 12, 12, 30, 0, time.UTC),

    fmt.Println(daySeconds(t))



    Output (try it on the Go Playground):



    30
    90
    750
    43950


    Let's see how this function gives correct result when daylight saving time happens. In Hungary, 25 March 2018 is a day when the clock was turned forward 1 hour at 02:00:00, from 2 am to 3 am.



    loc, err := time.LoadLocation("CET")
    if err != nil
    fmt.Println(err)
    return


    t := time.Date(2018, 3, 25, 0, 0, 30, 0, loc)
    fmt.Println(t)
    fmt.Println(daySeconds(t))

    t = t.Add(2 * time.Hour)
    fmt.Println(t)
    fmt.Println(daySeconds(t))


    This outputs (try it on the Go Playground):



    2018-03-25 00:00:30 +0100 CET
    30
    2018-03-25 03:00:30 +0200 CEST
    7230


    We print the daySeconds of a time being 30 seconds after midnight, which is 30 of course. Then we add 2 hours to the time (2 hours = 2*3600 seconds = 7200), and the daySeconds of this new time will be properly 7200 + 30 = 7230, even though the time changed 3 hours.






    share|improve this answer





























      3














      If we define "seconds of day" as the "elapsed seconds since midnight", then to get correct result even on days when daylight saving time happens we should subtract the time representing midnight from the given time. For that, we may use Time.Sub().



      func daySeconds(t time.Time) int 
      year, month, day := t.Date()
      t2 := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
      return int(t.Sub(t2).Seconds())



      Testing it:



      for _, t := range []time.Time
      time.Date(2019, 1, 1, 0, 0, 30, 0, time.UTC),
      time.Date(2019, 1, 1, 0, 1, 30, 0, time.UTC),
      time.Date(2019, 1, 1, 0, 12, 30, 0, time.UTC),
      time.Date(2019, 1, 1, 12, 12, 30, 0, time.UTC),

      fmt.Println(daySeconds(t))



      Output (try it on the Go Playground):



      30
      90
      750
      43950


      Let's see how this function gives correct result when daylight saving time happens. In Hungary, 25 March 2018 is a day when the clock was turned forward 1 hour at 02:00:00, from 2 am to 3 am.



      loc, err := time.LoadLocation("CET")
      if err != nil
      fmt.Println(err)
      return


      t := time.Date(2018, 3, 25, 0, 0, 30, 0, loc)
      fmt.Println(t)
      fmt.Println(daySeconds(t))

      t = t.Add(2 * time.Hour)
      fmt.Println(t)
      fmt.Println(daySeconds(t))


      This outputs (try it on the Go Playground):



      2018-03-25 00:00:30 +0100 CET
      30
      2018-03-25 03:00:30 +0200 CEST
      7230


      We print the daySeconds of a time being 30 seconds after midnight, which is 30 of course. Then we add 2 hours to the time (2 hours = 2*3600 seconds = 7200), and the daySeconds of this new time will be properly 7200 + 30 = 7230, even though the time changed 3 hours.






      share|improve this answer



























        3












        3








        3







        If we define "seconds of day" as the "elapsed seconds since midnight", then to get correct result even on days when daylight saving time happens we should subtract the time representing midnight from the given time. For that, we may use Time.Sub().



        func daySeconds(t time.Time) int 
        year, month, day := t.Date()
        t2 := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
        return int(t.Sub(t2).Seconds())



        Testing it:



        for _, t := range []time.Time
        time.Date(2019, 1, 1, 0, 0, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 0, 1, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 0, 12, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 12, 12, 30, 0, time.UTC),

        fmt.Println(daySeconds(t))



        Output (try it on the Go Playground):



        30
        90
        750
        43950


        Let's see how this function gives correct result when daylight saving time happens. In Hungary, 25 March 2018 is a day when the clock was turned forward 1 hour at 02:00:00, from 2 am to 3 am.



        loc, err := time.LoadLocation("CET")
        if err != nil
        fmt.Println(err)
        return


        t := time.Date(2018, 3, 25, 0, 0, 30, 0, loc)
        fmt.Println(t)
        fmt.Println(daySeconds(t))

        t = t.Add(2 * time.Hour)
        fmt.Println(t)
        fmt.Println(daySeconds(t))


        This outputs (try it on the Go Playground):



        2018-03-25 00:00:30 +0100 CET
        30
        2018-03-25 03:00:30 +0200 CEST
        7230


        We print the daySeconds of a time being 30 seconds after midnight, which is 30 of course. Then we add 2 hours to the time (2 hours = 2*3600 seconds = 7200), and the daySeconds of this new time will be properly 7200 + 30 = 7230, even though the time changed 3 hours.






        share|improve this answer















        If we define "seconds of day" as the "elapsed seconds since midnight", then to get correct result even on days when daylight saving time happens we should subtract the time representing midnight from the given time. For that, we may use Time.Sub().



        func daySeconds(t time.Time) int 
        year, month, day := t.Date()
        t2 := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
        return int(t.Sub(t2).Seconds())



        Testing it:



        for _, t := range []time.Time
        time.Date(2019, 1, 1, 0, 0, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 0, 1, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 0, 12, 30, 0, time.UTC),
        time.Date(2019, 1, 1, 12, 12, 30, 0, time.UTC),

        fmt.Println(daySeconds(t))



        Output (try it on the Go Playground):



        30
        90
        750
        43950


        Let's see how this function gives correct result when daylight saving time happens. In Hungary, 25 March 2018 is a day when the clock was turned forward 1 hour at 02:00:00, from 2 am to 3 am.



        loc, err := time.LoadLocation("CET")
        if err != nil
        fmt.Println(err)
        return


        t := time.Date(2018, 3, 25, 0, 0, 30, 0, loc)
        fmt.Println(t)
        fmt.Println(daySeconds(t))

        t = t.Add(2 * time.Hour)
        fmt.Println(t)
        fmt.Println(daySeconds(t))


        This outputs (try it on the Go Playground):



        2018-03-25 00:00:30 +0100 CET
        30
        2018-03-25 03:00:30 +0200 CEST
        7230


        We print the daySeconds of a time being 30 seconds after midnight, which is 30 of course. Then we add 2 hours to the time (2 hours = 2*3600 seconds = 7200), and the daySeconds of this new time will be properly 7200 + 30 = 7230, even though the time changed 3 hours.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        iczaicza

        172k25347380




        172k25347380













            Popular posts from this blog

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page