NSDateComponents.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // NSDateComponents.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 9/13/15.
  6. // Copyright © 2015 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. extension DateComponents {
  10. init(mySentryBytes: Data) {
  11. self.init()
  12. hour = Int(mySentryBytes[0] & 0b00011111)
  13. minute = Int(mySentryBytes[1] & 0b00111111)
  14. second = Int(mySentryBytes[2] & 0b00111111)
  15. year = Int(mySentryBytes[3]) + 2000
  16. month = Int(mySentryBytes[4] & 0b00001111)
  17. day = Int(mySentryBytes[5] & 0b00011111)
  18. calendar = Calendar(identifier: .gregorian)
  19. }
  20. init(pumpEventData: Data, offset: Int, length: Int = 5) {
  21. self.init(pumpEventBytes: pumpEventData.subdata(in: offset..<offset + length))
  22. }
  23. init(pumpEventBytes: Data) {
  24. self.init()
  25. if pumpEventBytes.count == 5 {
  26. second = Int(pumpEventBytes[0] & 0b00111111)
  27. minute = Int(pumpEventBytes[1] & 0b00111111)
  28. hour = Int(pumpEventBytes[2] & 0b00011111)
  29. day = Int(pumpEventBytes[3] & 0b00011111)
  30. month = Int((pumpEventBytes[0] & 0b11000000) >> 4) +
  31. Int((pumpEventBytes[1] & 0b11000000) >> 6)
  32. year = Int(pumpEventBytes[4] & 0b01111111) + 2000
  33. } else {
  34. day = Int(pumpEventBytes[0] & 0b00011111)
  35. month = Int((pumpEventBytes[0] & 0b11100000) >> 4) +
  36. Int((pumpEventBytes[1] & 0b10000000) >> 7)
  37. year = Int(pumpEventBytes[1] & 0b01111111) + 2000
  38. }
  39. calendar = Calendar(identifier: .gregorian)
  40. }
  41. init(glucoseEventBytes: Data) {
  42. self.init()
  43. year = Int(glucoseEventBytes[3] & 0b01111111) + 2000
  44. month = Int((glucoseEventBytes[0] & 0b11000000) >> 4) +
  45. Int((glucoseEventBytes[1] & 0b11000000) >> 6)
  46. day = Int(glucoseEventBytes[2] & 0b00011111)
  47. hour = Int(glucoseEventBytes[0] & 0b00011111)
  48. minute = Int(glucoseEventBytes[1] & 0b00111111)
  49. calendar = Calendar(identifier: .gregorian)
  50. }
  51. }