Task: In an application written in C #, the time value is entered in the interval [00:00:00, 24:00:00) and entered into a variable of type TimeSpan . Then this value should be transmitted over the network and correctly read by an application written in C ++.

Question 1: How to transmit TimeSpan ? (For me, it is still being transmitted as a double TimeSpan.TotalHours).

Question 2: How and by what means do you read this value in C ++?

PS In C ++ I am a teapot.


UPD. Still there is an idea to transfer separately hours of minute and second

  • one
    In general, the TimeSpan uniquely determined by one long value ( Ticks ); you can transmit it. Or, if this is redundant for you, just pass an integer number of seconds - Andrey NOP
  • The first thought is to transmit int-ohm (the number of seconds in the interval). Within 24 hours, int-a is enough, even if milliseconds are sent instead of seconds - Regent
  • @ AndreiNOP good. And how then on the C ++ application side do you convert long Ticks to hh:mm:ss format? - Andrei Khotko
  • @ AndreiNOP The general idea is clear, thanks - Andrei Khotko
  • referencesource.microsoft.com/#mscorlib/system/timespan.cs,108 on pluses can write the same thing? - Andrey NOP

1 answer 1

You can send as you like, as long as on the receiving side you can restore the original value. You can even double , representing the number of hours, but the easiest way to do this is by using integer int (or even uint ) numbers, so there are less chances to run into some rounding errors and other problems of incompatibility of floating-point representations (although for the most part still rely everywhere on IEEE-754 ).

If the time interval is converted to an integer that corresponds to a count of seconds (if greater accuracy is needed, for example, milliseconds, the whole type can be expanded), then after translation, following the byte order and transmission over the network at the receiving (C ++) side, you can write something like (assuming that the number was placed in a 32-bit unsigned int ):

 #include <cstdint> std::uint32_t sec = 0; if (sizeof(sec) != recv(sockfd, &sec, sizeof(sec), 0)) { // в sec получено кол-во секунд } 

As an alternative, which will exclude the influence of the order of bytes, you can offer the transmission of the interval as a string, for example "12:34:56" . However, in this case, the size of the transmitted data will increase, however, it will be easier to receive and transmit such a string between different architectures that can participate in the exchange over the network.