Time Handling in C++
Introduction
Time handling in C++ can be tedious and tricky, but hopefully by the end of this blog post, you will understand how to handle time with various different methods. There are two different ways in which to handle time; using the std::chrono
library from C++, or to use C-style date and time via std::time
.
Time handling via std::chrono
- Time handling in std::chrono is managed by 3 different parts.
- Clocks
- Objects that contain a starting point, which is usually the Unix epoch. The Unix epoch is considered the beginning of time for Unix computers and starts on the 1^st January, 1970. These clock objects also have a tick rate, which is usually set to 1 second. Time points
- A duration of time since the epoch of a specified clock. Duration
- Consists of a span of time which is made up a number of ticks of a given time unit.
Let me show you an example that uses clocks.
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main(int argc, char** argv) {
time_t a = system_clock().to_time_t(system_clock().now());
std::cout << a << std::endl;
}
I have used a using directive here and in other examples as it will reduce typing. system_clock().now() returns
// JS code
const menuTrigger = document.querySelector('.menu-trigger')
const menu = document.querySelector('.menu')
const mobileQuery = getComputedStyle(document.body).getPropertyValue('--phoneWidth')
const isMobile = () => window.matchMedia(mobileQuery).matches
const isMobileMenu = () => {
menuTrigger.classList.toggle('hidden', !isMobile())
menu.classList.toggle('hidden', isMobile())
}
isMobileMenu()
menuTrigger.addEventListener('click', () => menu.classList.toggle('hidden'))
window.addEventListener('resize', isMobileMenu)
<!-- HTML code -->
<section id="main">
<div>
<h1 id="title">{{ .Title }}</h1>
{{ range .Pages }}
{{ .Render "summary"}}
{{ end }}
</div>
</section>
Header 4
Curabitur scelerisque felis viverra varius scelerisque. Ut enim libero, molestie gravida blandit at, mollis ornare tellus. Cras arcu mi, ultrices vel pulvinar vel, volutpat eu tortor. Nullam nec eros quis massa ultrices iaculis sed in metus. Praesent sollicitudin sem sit amet orci tempor gravida.
- Maecenas elementum vitae nibh vitae porttitor.
- Aenean consequat, risus ut cursus placerat, arcu nulla sodales risus, ut molestie tellus tellus et dui.
- Integer imperdiet turpis vitae lacus imperdiet, ut ornare ligula auctor. Integer in mi eu velit vehicula suscipit eget vulputate nulla.
- Etiam vitae enim quis velit lobortis placerat a ut sem.
- Curabitur lobortis ante sit amet orci pulvinar, sollicitudin viverra nunc accumsan.
- Praesent fermentum orci quis leo facilisis posuere.
Aliquam erat volutpat. In hac habitasse platea dictumst. Nunc ut tincidunt mauris. Sed at gravida risus, id semper magna. Nullam vitae enim mattis, sodales neque non, pharetra elit. Cras sit amet sagittis augue, et finibus turpis. Ut tempus tincidunt diam vel pharetra. Nulla porttitor odio sit amet nulla scelerisque, quis aliquam mi imperdiet. Sed tincidunt dui vel tellus vestibulum rhoncus. Donec tempus ultrices velit.