aboutsummaryrefslogtreecommitdiff
path: root/libbutl/timestamp.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-03-30 20:47:38 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-04-01 13:10:06 +0300
commit0598a6d315ecadc5dd58cd8f5dde7c603c3f493c (patch)
treefa24de8d71bb6501d889956878666674d44adda7 /libbutl/timestamp.cxx
parent34466f10506fd57836e89cffda7738c3008321b1 (diff)
Add daytime() function
Diffstat (limited to 'libbutl/timestamp.cxx')
-rw-r--r--libbutl/timestamp.cxx28
1 files changed, 28 insertions, 0 deletions
diff --git a/libbutl/timestamp.cxx b/libbutl/timestamp.cxx
index 296e070..0242758 100644
--- a/libbutl/timestamp.cxx
+++ b/libbutl/timestamp.cxx
@@ -662,4 +662,32 @@ namespace butl
return system_clock::from_time_t (time) +
chrono::duration_cast<duration> (t.second);
}
+
+ duration
+ daytime (timestamp t)
+ {
+ // Convert the time from Epoch to the local time.
+ //
+ time_t time (system_clock::to_time_t (t));
+
+ std::tm tm;
+ if (details::localtime (&time, &tm) == nullptr)
+ throw_generic_error (errno);
+
+ // Roll the time back to the latest midnight.
+ //
+ tm.tm_hour = 0;
+ tm.tm_min = 0;
+ tm.tm_sec = 0;
+
+ // Convert the local midnight to time from Epoch.
+ //
+ time = mktime (&tm);
+ if (time == -1)
+ throw_generic_error (errno);
+
+ // Note: preserves the accuracy of the original time.
+ //
+ return t - system_clock::from_time_t (time);
+ }
}