[chrono] Initializing system_clock from microseconds

I have been having some difficulty initializing a chrono::system_clock::time_point object. Specifically, I am trying to load a time stamp from a .pcap file using libpcap. I have a structure, pcap_pkthdr, which contains two fields: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* and microseconds */ }; struct pcap_pkthdr { struct timeval ts; /* time stamp */ bpf_u_int32 caplen; /* length of portion present */ bpf_u_int32 len; /* length this packet (off wire) */ }; Where tv_sec and tv_usec are seconds and microseconds respectively relative to an epoch of Jan 1, 1970. I can use system_clock::from_time_t() to initialize the time_point to an 64-bit integer seconds (std:time_t). The question is, how do I add the microseconds to this value? My latest attempt is: void my_class::calculate_packet_statistics( const struct pcap_pkthdr *header ) { frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ); frame_interval_.intervalTimeStamp_ += microseconds( header->ts.tv_usec ); // This is the problematic line ... } However, this appears to simply add tv_usec to the internal representation of the time_point. The internal representation appears to be in 100ns ticks with an epoch other than Jan 1, 1970. This is on a windows x64 build but the same code will have to work on Linux as well. As a side note, I later do some date calculations using system_clock::to_time_t. I lose my fractional seconds here But since I am only working with dates, this is not a problem. Dan Kelly dan.kelly@sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Le 11/06/13 14:22, Kelly, Dan a écrit :
Hi, there were some issues (https://svn.boost.org/trac/boost/ticket/7525) with windows system_clock epoch that should be fixed with https://svn.boost.org/trac/boost/changeset/82563. This was released on Boost 1.52. Which version are you using? Best, Vicente

I'm using 1.47, so I would expect this issue to apply. However, I don't think this is what I am experiencing. Right now I am testing out a fix which initially appears to work but I have to fully verify it. void my_class::calculate_packet_statistics ( const struct pcap_pkthdr *header ) { frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ); // Cast to a duration using the system_clock's tick period. system_clock::duration us_duration = duration_cast< system_clock::duration >( microseconds( header->ts.tv_usec ) ); frame_interval_.intervalTimeStamp_ += us_duration; ... } I would have thought my previous implementation would have done this cast implicitly, but that doesn't appear to be the case. I'll let you know if this works. Regards, Dan From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vicente J. Botet Escriba Sent: Tuesday, June 11, 2013 13:03 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [chrono] Initializing system_clock from microseconds Le 11/06/13 14:22, Kelly, Dan a écrit : I have been having some difficulty initializing a chrono::system_clock::time_point object. Specifically, I am trying to load a time stamp from a .pcap file using libpcap. I have a structure, pcap_pkthdr, which contains two fields: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* and microseconds */ }; struct pcap_pkthdr { struct timeval ts; /* time stamp */ bpf_u_int32 caplen; /* length of portion present */ bpf_u_int32 len; /* length this packet (off wire) */ }; Where tv_sec and tv_usec are seconds and microseconds respectively relative to an epoch of Jan 1, 1970. I can use system_clock::from_time_t() to initialize the time_point to an 64-bit integer seconds (std:time_t). The question is, how do I add the microseconds to this value? My latest attempt is: void my_class::calculate_packet_statistics( const struct pcap_pkthdr *header ) { frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ); frame_interval_.intervalTimeStamp_ += microseconds( header->ts.tv_usec ); // This is the problematic line ... } However, this appears to simply add tv_usec to the internal representation of the time_point. The internal representation appears to be in 100ns ticks with an epoch other than Jan 1, 1970. This is on a windows x64 build but the same code will have to work on Linux as well. As a side note, I later do some date calculations using system_clock::to_time_t. I lose my fractional seconds here But since I am only working with dates, this is not a problem. Hi, there were some issues (https://svn.boost.org/trac/boost/ticket/7525) with windows system_clock epoch that should be fixed with https://svn.boost.org/trac/boost/changeset/82563. This was released on Boost 1.52. Which version are you using? Best, Vicente ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Le 11/06/13 19:15, Kelly, Dan a écrit :
What about something like frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ) + nanoseconds( header->ts.tv_usec); Best, Vicente

Vincente, If I'm reading this correctly, what you are proposing is the same as what I did, except that I used an implicit duration_cast<>. Yours is certainly more concise and readable. This method did work by the way. Dan From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vicente J. Botet Escriba Sent: Wednesday, June 12, 2013 1:44 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [chrono] Initializing system_clock from microseconds Le 11/06/13 19:15, Kelly, Dan a écrit : I'm using 1.47, so I would expect this issue to apply. However, I don't think this is what I am experiencing. Right now I am testing out a fix which initially appears to work but I have to fully verify it. void my_class::calculate_packet_statistics ( const struct pcap_pkthdr *header ) { frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ); // Cast to a duration using the system_clock's tick period. system_clock::duration us_duration = duration_cast< system_clock::duration >( microseconds( header->ts.tv_usec ) ); frame_interval_.intervalTimeStamp_ += us_duration; ... } I would have thought my previous implementation would have done this cast implicitly, but that doesn't appear to be the case. I'll let you know if this works. What about something like frame_interval_.intervalTimeStamp_ = system_clock::from_time_t( static_cast<time_t>( header->ts.tv_sec ) ) + nanoseconds( header->ts.tv_usec); Best, Vicente ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
participants (2)
-
Kelly, Dan
-
Vicente J. Botet Escriba