{"id":3796,"date":"2010-07-27T12:34:23","date_gmt":"2010-07-27T17:34:23","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=3796"},"modified":"2010-07-27T12:48:53","modified_gmt":"2010-07-27T17:48:53","slug":"cool-method-for-milliseconds-since-midnight-for-cc","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/3796","title":{"rendered":"Cool Method for Milliseconds Since Midnight for C\/C++"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2007\/10\/cplusplus.jpg\" alt=\"cplusplus.jpg\" title=\"cplusplus.jpg\" border=\"0\" width=\"150\" height=\"149\" style=\"float:right;\" \/><\/p>\n<p>I was working with exchange data today, and realized that the \"timestamp in microseconds since epoch\" wasn't really a great timestamp, and, in fact, the exchanges are using the reference point of midnight, and only to milliseconds. The problem was, I didn't have a simple method that could give me the time with respect to midnight. I didn't see anything that was really helpful on google either.<\/p>\n<p>Then it hit me... It was like a flash - like all great insights are: use the seconds in the response from <tt>gettimeofday()<\/tt> as the input to <tt>localtime_r()<\/tt> and then you have one time \"instant\" defined, but you can pick off the hours, minutes, and secons and then add in the milliseconds by dividing the microseconds.<\/p>\n<p>Like this:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">uint32_t<\/span> TransferStats<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">msecSinceMidnight<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #ff0000; font-style: italic;\">\/*\n     * This is really interesting. I need to get the msec since midnight,\n     * and the cleanest way I could think to do this was to get the\n     * timeval struct and then take the tv_sec component of it and pass\n     * it to localtime_r to get the hour, min, sec parts of the time and\n     * then piece it all together again. Kinda slick.\n     *\/<\/span>\n    <span style=\"color: #666666;\">\/\/ get the time now as the (sec + usec) since epoch<\/span>\n    <span style=\"color: #0000ff;\">struct<\/span> timeval tv<span style=\"color: #008080;\">;<\/span>\n    gettimeofday<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">&amp;<\/span>tv, <span style=\"color: #0000ff;\">NULL<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ now take the seconds component and get the current hour, min, sec<\/span>\n    <span style=\"color: #0000ff;\">struct<\/span> <span style=\"color: #0000ff;\">tm<\/span>   now<span style=\"color: #008080;\">;<\/span>\n    localtime_r<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">&amp;<\/span>tv.<span style=\"color: #007788;\">tv_sec<\/span>, <span style=\"color: #000040;\">&amp;<\/span>now<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ now let's calculate the time since midnight...<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #008000;\">&#40;<\/span>tv.<span style=\"color: #007788;\">tv_usec<\/span><span style=\"color: #000040;\">\/<\/span><span style=\"color: #0000dd;\">1000<\/span> <span style=\"color: #000040;\">+<\/span> <span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#40;<\/span>now.<span style=\"color: #007788;\">tm_hour<\/span> <span style=\"color: #000040;\">*<\/span> <span style=\"color: #0000dd;\">60<\/span> <span style=\"color: #000040;\">+<\/span> now.<span style=\"color: #007788;\">tm_min<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #000040;\">*<\/span> <span style=\"color: #0000dd;\">60<\/span>\n                                <span style=\"color: #000040;\">+<\/span> now.<span style=\"color: #007788;\">tm_sec<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #000040;\">*<\/span> <span style=\"color: #0000dd;\">1000<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was working with exchange data today, and realized that the &#8220;timestamp in microseconds since epoch&#8221; wasn&#8217;t really a great timestamp, and, in fact, the exchanges are using the reference point of midnight, and only to milliseconds. The problem was, I didn&#8217;t have a simple method that could give me the time with respect to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3796","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/3796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=3796"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/3796\/revisions"}],"predecessor-version":[{"id":3797,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/3796\/revisions\/3797"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=3796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=3796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=3796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}