Timespec.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.tsi.app.common.utils;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.util.concurrent.TimeUnit;
  5. public class Timespec {
  6. protected long tv_sec;
  7. protected long tv_nsec;
  8. public Timespec(ByteBuffer buffer) {
  9. buffer.order(ByteOrder.nativeOrder());
  10. this.tv_sec = buffer.getLong();
  11. this.tv_nsec = buffer.getLong();
  12. }
  13. public Timespec(long sec, long nsec) {
  14. this.tv_sec = sec;
  15. this.tv_nsec = nsec;
  16. }
  17. public Timespec() {
  18. init();
  19. }
  20. public void init() {
  21. this.tv_sec = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
  22. this.tv_nsec = System.nanoTime();
  23. }
  24. public void tv_nsec(long nsec) {
  25. this.tv_nsec = nsec;
  26. }
  27. public void tv_sec(long sec) {
  28. this.tv_sec = sec;
  29. }
  30. public void set(long nsec, long sec) {
  31. this.tv_nsec = nsec;
  32. this.tv_sec = sec;
  33. }
  34. public byte[] tv_sec() {
  35. ByteBuffer buffer = ByteBuffer.allocate(8);
  36. buffer.putLong(this.tv_sec);
  37. return buffer.array();
  38. }
  39. public byte[] tv_nsec() {
  40. ByteBuffer buffer = ByteBuffer.allocate(8);
  41. buffer.putLong(this.tv_nsec);
  42. return buffer.array();
  43. }
  44. public byte[] bytes() {
  45. ByteBuffer buffer = ByteBuffer.allocate(16);
  46. buffer.order(ByteOrder.LITTLE_ENDIAN);
  47. buffer.putLong(this.tv_sec);
  48. buffer.putLong(this.tv_nsec);
  49. return buffer.array();
  50. }
  51. public long timestamp() {
  52. return this.tv_nsec;
  53. }
  54. public long times() {
  55. return this.tv_sec;
  56. }
  57. }