Timer.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. © Siemens AG, 2017-2019
  3. Author: Dr. Martin Bischoff (martin.bischoff@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. // Adding Timestamp switching
  15. // Shimadzu corp , 2019, Akira NODA (a-noda@shimadzu.co.jp / you.akira.noda@gmail.com)
  16. // Removing MonoBehaviour inheritance
  17. // Siemens AG , 2019, Berkay Alp Cakal (berkay_alp.cakal.ct@siemens.com)
  18. // Added allocation free alternatives
  19. // UoK , 2019, Odysseas Doumas (od79@kent.ac.uk / odydoum@gmail.com)
  20. using System;
  21. using RosSharp.RosBridgeClient.MessageTypes.Std;
  22. namespace RosSharp.RosBridgeClient
  23. {
  24. public class Timer
  25. {
  26. public static DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  27. public virtual MessageTypes.Std.Time Now()
  28. {
  29. Now(out uint secs, out uint nsecs);
  30. return new MessageTypes.Std.Time(secs, nsecs);
  31. }
  32. public virtual void Now(MessageTypes.Std.Time stamp)
  33. {
  34. uint secs; uint nsecs;
  35. Now(out secs, out nsecs);
  36. stamp.secs = secs; stamp.nsecs = nsecs;
  37. }
  38. private static void Now(out uint secs, out uint nsecs)
  39. {
  40. TimeSpan timeSpan = DateTime.Now.ToUniversalTime() - UNIX_EPOCH;
  41. double msecs = timeSpan.TotalMilliseconds;
  42. secs = (uint)(msecs / 1000);
  43. nsecs = (uint)((msecs / 1000 - secs) * 1e+9);
  44. }
  45. }
  46. }