title: c++ get milliseconds since 01/01/0001 00:00:00
body: <p>I need a timestamp in a special format for an API call:</p>
  
  <blockquote>
    <p>Dates are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.</p>
  </blockquote>
  
  <p>My first assumption was to use:</p>
  
  <pre><code>auto now = std::chrono::system_clock::now();
  std::cout &lt;&lt; "millisceconds since epoch: "
            &lt;&lt; std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(
                 now.time_since_epoch()).count() 
  </code></pre>
  
  <p>But of course the output is the time interval from the UNIX epoch 
  <code>Thu Jan 1 00:00:00 1970</code></p>
  
  <p>So for a <code>now = "Wed Dec 12 13:30:00 2018"</code> it returns <code>1544617800000</code>ms.</p>
  
  <p>How do I get the milliseconds elapsed since 12:00:00 midnight, January 1, 0001?</p>
  
  <h2>Context OSISoft API</h2>
  
  <p>The OSISoft API Documentation for specifying a date range is quite strange</p>
  
  <blockquote>
    <p>Numeric Range Queries</p>
    
    <p>The previous examples were Range Queries against string fields. Numeric values > can also be searched for with Range Queries.</p>
    
    <p>The only fields that are indexed as numeric fields are the CreationDate and ChangeDate fields for the respective PI Point attributes. To index these fields > add them to the list of PI Point Attributes. This configuration may be viewed > or modified on the Settings page.</p>
    
    <p>These date time values are indexed as numeric values via a conversion: Dates  are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1,  0001.</p>
    
    <p>In the following example query is a request for last changed date equal to or > greater than February 26th, 22:16:50.000 (This is Universal Time). This DateTime, following the aforementioned conversion, would be represented as  numeric value: 63655280210000. Therefore the query submitted is:</p>
    
    <p><code>https://MyServer/piwebapi/search/query?q=changedate:[63655280210000 TO *]</code></p>
  </blockquote>
  
  <p>From this documention I have asked this question on how to get the milliseconds elapsed since 12:00:00 midnight, January 1, 0001.</p>
  
  <p>I also linked the Question to <a href="https://pisquare.osisoft.com/message/124982-re-how-to-set-up-a-numeric-range-query-for-time-stamps-in-osisoft-piwebapi-indexed-search#comment-124982" rel="nofollow noreferrer">PISquare</a></p>