Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of seconds since midnight

Tags:

c++

chrono

I wrote the below code to get the number of seconds since midnight.

However, I'm not great with the C time date structs. Is there a simpler way of doing this using a standard C++ library?

// Get today's date
time_t aTime = time(NULL);

// Set the time to midnight
struct tm* tm = localtime(&aTime);
tm->tm_sec = 0;
tm->tm_min = 0;
tm->tm_hour = 0;
tm->tm_isdst = -1;
time_t midnight = mktime(tm);

// Create object representing now
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);

// Number of seconds (now) since Epoch
const uint64_t nowSecsSinceEpoch = now.tv_sec;

// Number of seconds (now) since midnight = seconds (now) since Epoch minus seconds (at midnight) since Epoch
const uint64_t nowSecsSinceMidnight = nowSecsSinceEpoch - midnight;
like image 417
user997112 Avatar asked Apr 17 '20 14:04

user997112


People also ask

How many seconds is midnight?

It is 100 seconds to midnight The Clock has become a universally recognized indicator of the world's vulnerability to catastrophe from nuclear weapons, climate change, and disruptive technologies in other domains.

What is seconds past midnight?

The SECONDS-PAST-MIDNIGHT function returns a value in standard numeric time form that represents the current local time of day provided by the system on which the function is evaluated. The function type is numeric.

How do you find the time of day in seconds in Python?

Get Current Time in PythonUse the time. time() function to get the current time in seconds since the epoch as a floating-point number. This method returns the current timestamp in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00. It returns the current time in seconds.

How do you calculate the time since midnight?

This is a quick online utility for calculating the time since midnight. You can find out how many seconds have passed since midnight for any clock time. Mathematically, for any given clock time, the number of seconds since midnight is equal to the number of seconds in this clock time.

How many seconds past midnight is 8 hours 49 minutes and seconds?

It's a static time that I set already as 8:49:12 "today" and I'm to figure out how many seconds past midnight and "to" midnight this represents. 8 hours, 49 minutes, and 12 seconds. Here is my code now: hour = 8; minute = 59; second = 32; System.out.println ("The static time used for this program was: " + hour + ":" + minute + ":" + second);

How to get seconds since midnight in Java?

LocalTime time = LocalTime.of (8, 59, 32); System.out.println ( "Seconds since midnight: " + time.get (ChronoField.SECOND_OF_DAY)); // 32372 System.out.println ( "Seconds to midnight: " + (86400 - time.get (ChronoField.SECOND_OF_DAY))); // 54028 Your answer is in java.util.Calendar class.

How many seconds are there in a year?

Human-readable time Seconds; 1 hour: 3600 seconds: 1 day: 86400 seconds: 1 week: 604800 seconds: 1 month (30.44 days) 2629743 seconds: 1 year (365.24 days) 31556926 seconds


Video Answer


1 Answers

It depends on if you mean time since midnight UTC, or time since midnight local time, or perhaps in some non-local, far away time zone.

This is also made much easier in C++20. But there exists a preview of the C++20 parts of the <chrono> library that can be used with C++11-17.

Time since midnight UTC

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    auto now = system_clock::now();
    auto today = floor<days>(now);
    auto tod = duration_cast<seconds>(now - today);
}

This simply gets the current time (UTC), truncates it to a days-precision time_point, and then subtracts the two and truncates that difference to seconds precision.

Time since local midnight

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    auto now = current_zone()->to_local(system_clock::now());
    auto today = floor<days>(now);
    auto tod = duration_cast<seconds>(now - today);
}

This version finds your computer's currently set local time zone, and then gets the current local time via the time_zone's to_local() member function. And then proceeds as before.

Time since some other midnight

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    auto now = locate_zone("Australia/Sydney")->to_local(system_clock::now());
    auto today = floor<days>(now);
    auto tod = duration_cast<seconds>(now - today);
}

Finally, this version finds the time_zone associated with Sydney Australia, and then uses that time zone and proceeds as before.

The C++20 preview <chrono> library is free and open-source. It puts everything in namespace date, and in two headers (and one source):

  • date.h: A header-only library that will do the UTC part, but has no time zone support.

  • tz.h: For the time zone support. This requires some installation.

like image 102
Howard Hinnant Avatar answered Oct 22 '22 20:10

Howard Hinnant