iosiphoneflutterswiftuiscreen-time

DeviceActivityFilter Custom Filter


I'm trying to set up a DeviceActivityFilter where the start is the current time and the end is midnight. My current implementation is below and is not working and is still showing the user's usage from 00:00-23:59, when I want to show <current_time> - 23:59. Does anyone have any idea why?

Thank you!

var filter = DeviceActivityFilter(
  segment: .daily(
    during: DateInterval(
      start: .now(),
      end: Calendar.current.startOfDay(for: .now).addingTimeInterval(86400 - 1)
    )
  ),
  devices: .init([.iPhone, .iPad]),
  applications: selections.applicationTokens
)

Solution

  • It seems that the .daily segment is for full calendar days.

    Try this approach using .hourly instead of .daily.

    Note the different timezone displayed when using the DateFormatter

    
      let formatter = DateFormatter()
      formatter.dateStyle = .full
      formatter.timeStyle = .full
      formatter.timeZone = .current
    
    
     let start = Date()
     let endOfDay = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: start)!
    
     let filter = DeviceActivityFilter(
         segment: .hourly(during: DateInterval(start: start, end: endOfDay)),
         devices: .init([.iPhone, .iPad]),
         applications: selections.applicationTokens
     )
    
      print("---> UTC start:    \(start)")
      print("---> UTC endOfDay: \(endOfDay)\n")
                    
      print("---> local start:    \(formatter.string(from: start))")
      print("---> local endOfDay: \(formatter.string(from: endOfDay))")