Search code examples
swiftwatchos-2apple-watch-complication

Display complication current time in 24 hour format for specific timezone


I am making a complication, and want to display "UTC" timezone time using 24-hour format in the complication.

CLKTimeTextProvider will work, but it seems to only use the default format the user prefers, whereas I need to force it to always show 24-hour time.

Any thoughts? Is there a property I am not seeing?

switch family {            
case .UtilitarianLarge:
        let template = CLKComplicationTemplateUtilitarianLargeFlat()
        template.imageProvider = nil
        template.textProvider = CLKTimeTextProvider(date: NSDate(), timeZone: NSTimeZone(name: "UTC"))
        return CLKComplicationTimelineEntry(date: date, complicationTemplate: template)

Solution

  • There's no CLKTimeTextProvider property that would currently let you override the user's region and locale settings.

    Apart from submitting a feature request, you could workaround the issue by converting the date to a 24-hour formatted time, then display that localized string using a CLKSimpleTextProvider

    let dateFormatter = NSDateFormatter()
    let localeFormatString = NSDateFormatter.dateFormatFromTemplate("HH:mm", options: 0, locale: NSLocale.currentLocale())
    dateFormatter.dateFormat = localeFormatString
    dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
    template.textProvider = CLKSimpleTextProvider(text: dateFormatter.stringFromDate(NSDate()))
    

    Update:

    Based on your comments, you might be able to accomplish what you want by using a CLKRelativeDateStyleTimer which counted up from 00:00 midnight. Since it's counting up hours and minutes, it would happen to count up in 24-hour format. You would have to update your complication at midnight, to reset it to 00:00 for the next day.