Search code examples
c++datetimeboostiso8601

Generate complete iso date time representation strings with fraction and timezone


I want to print an ISO 8601 conforming string of the form of the complete date time representation as in ISO

4.3.2 Complete representations

1985-04-12T10:15:30Z combined with

4.2.2.4 Representations with decimal fraction

as 23:20:50,5

combined as allowed in

4.3.3 Representations other than complete

as in the form of 19850412T101530,42+04 or 1985-04-12T10:15:30.32Z

with boost date time using the current (wall clock), but I am not even understanding how to print a simple date time string of the current time by using the boost documentation.


Solution

  • You say you want to print "current (wall clock)" but your example says "1985" ?...

    The following will print current time in ISO format:

    #include <iostream>
    #include <boost/date_time.hpp>
    
    int main()
    {
        auto now = boost::posix_time::microsec_clock::universal_time();
    
        std::cout << to_iso_extended_string(now) << std::endl;
    }
    

    Outputs:

    2019-04-26T07:52:34.533296
    

    Note: ISO time is always in UTC timezone, so you can just append the "Z" (Zulu) suffix to it.

    For more details refer to the Posix_time documentation.