DateTime.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // LoopFollow
  2. // DateTime.swift
  3. import Foundation
  4. class dateTimeUtils {
  5. static func displayTimeZone() -> TimeZone {
  6. if Storage.shared.graphTimeZoneEnabled.value,
  7. let tz = TimeZone(identifier: Storage.shared.graphTimeZoneIdentifier.value)
  8. {
  9. return tz
  10. }
  11. return .current
  12. }
  13. static func displayCalendar() -> Calendar {
  14. var calendar = Calendar.current
  15. calendar.timeZone = displayTimeZone()
  16. return calendar
  17. }
  18. static func applyDisplayTimeZone(to formatter: DateFormatter) {
  19. formatter.timeZone = displayTimeZone()
  20. }
  21. static func getTimeIntervalMidnightToday() -> TimeInterval {
  22. let now = Date()
  23. let formatter = DateFormatter()
  24. formatter.dateFormat = "yyyy-MM-dd"
  25. let dayString = formatter.string(from: now)
  26. var midnight = dayString + " 00:00:00"
  27. let newFormatter = DateFormatter()
  28. newFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  29. newFormatter.locale = Locale(identifier: "en_US")
  30. let newDate = newFormatter.date(from: midnight)
  31. guard let midnightTimeInterval = newDate?.timeIntervalSince1970 else { return 0 }
  32. return midnightTimeInterval
  33. }
  34. static func getTimeIntervalMidnightYesterday() -> TimeInterval {
  35. let now = Date().addingTimeInterval(-86400)
  36. let formatter = DateFormatter()
  37. formatter.dateFormat = "yyyy-MM-dd"
  38. let dayString = formatter.string(from: now)
  39. var midnight = dayString + " 00:00:00"
  40. let newFormatter = DateFormatter()
  41. newFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  42. newFormatter.locale = Locale(identifier: "en_US")
  43. let newDate = newFormatter.date(from: midnight)
  44. guard let midnightTimeInterval = newDate?.timeIntervalSince1970 else { return 0 }
  45. return midnightTimeInterval
  46. }
  47. static func getTimeIntervalNHoursAgo(N: Int) -> TimeInterval {
  48. let today = Date()
  49. let nHoursAgo = Calendar.current.date(byAdding: .hour, value: -N, to: today)!
  50. return nHoursAgo.timeIntervalSince1970
  51. }
  52. static func getNowTimeIntervalUTC() -> TimeInterval {
  53. let now = Date()
  54. let formatter = DateFormatter()
  55. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  56. formatter.locale = Locale(identifier: "en_US")
  57. formatter.timeZone = TimeZone(abbreviation: "UTC")
  58. let utc = formatter.string(from: now)
  59. let day = formatter.date(from: utc)
  60. guard let utcTime = day?.timeIntervalSince1970 else { return 0 }
  61. return utcTime
  62. }
  63. static func getDateTimeString(addingMinutes minutes: Int? = nil, addingHours hours: Int? = nil, addingDays days: Int? = nil) -> String {
  64. let currentDate = Date()
  65. var date = currentDate
  66. if let minutesToAdd = minutes {
  67. date = Calendar.current.date(byAdding: .minute, value: minutesToAdd, to: date)!
  68. }
  69. if let hoursToAdd = hours {
  70. date = Calendar.current.date(byAdding: .hour, value: hoursToAdd, to: date)!
  71. }
  72. if let daysToAdd = days {
  73. date = Calendar.current.date(byAdding: .day, value: daysToAdd, to: date)!
  74. }
  75. let dateFormatter = DateFormatter()
  76. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
  77. dateFormatter.locale = Locale(identifier: "en_US")
  78. dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
  79. return dateFormatter.string(from: date)
  80. }
  81. static func printNow() -> String {
  82. let date = Date()
  83. let formatter = DateFormatter()
  84. formatter.dateFormat = "HH:mm:ss"
  85. formatter.locale = Locale(identifier: "en_US")
  86. return formatter.string(from: date)
  87. }
  88. static func is24Hour() -> Bool {
  89. let dateFormat = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!
  90. return dateFormat.firstIndex(of: "a") == nil
  91. }
  92. static func formattedDate(from date: Date?) -> String {
  93. guard let date = date else {
  94. return "Unknown"
  95. }
  96. let dateFormatter = DateFormatter()
  97. dateFormatter.dateStyle = .short
  98. dateFormatter.timeStyle = .short
  99. dateFormatter.locale = Locale.current
  100. return dateFormatter.string(from: date)
  101. }
  102. }