Skip to content

Get seconds since midnight #4063

Answered by vitoyucepi
iSerganov asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @iSerganov,
There are two ways to get the seconds since midnight

  1. Get the remainder after dividing by a number of seconds in a day after adjusting for the time zone.
    def seconds_since_midnight()
      current_time = int(time())
      let _.{utc_diff} = time.zone()
      SECONDS_IN_DAY = 86400
      (current_time + utc_diff) mod SECONDS_IN_DAY
    end
  2. Using the local time in the form of a data structure.
    def seconds_since_midnight()
      let {hour, min, sec} = time.local()
      SECONDS_IN_HOUR = 3600
      SECONDS_IN_MINUTE = 60
      hour * SECONDS_IN_HOUR + min * SECONDS_IN_MINUTE + sec
    end

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@iSerganov
Comment options

Answer selected by iSerganov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants