From 3eb6ecef82935bb8d69f545650266c857b53855d Mon Sep 17 00:00:00 2001 From: Mike Mattozzi Date: Wed, 29 Mar 2023 07:56:06 -0400 Subject: [PATCH] Fixing time display when there is no quote to be more normally formatted --- .../LiteraryClockScreenSaverView.h | 1 + .../LiteraryClockScreenSaverView.m | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.h b/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.h index d6a482a..03ee944 100644 --- a/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.h +++ b/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.h @@ -49,6 +49,7 @@ - (void) setupTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents:(HighlightedQuote *)quote; - (void) setupCitatationTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents:(HighlightedQuote *)quote; - (NSString*) createFormattedTime; +- (NSString*) createHumanFormattedTime; - (BOOL) selectNextQuote; - (void) resumeAnimation; diff --git a/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.m b/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.m index 76e642d..504609b 100644 --- a/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.m +++ b/LiteraryClockScreenSaver/LiteraryClockScreenSaverView.m @@ -161,7 +161,7 @@ - (void) setupTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents [highlightedString addAttribute:NSFontAttributeName value:boldFontName range:[quote rangeOfHighlight]]; [highlightedString endEditing]; } else { - highlightedString = [[NSMutableAttributedString alloc] initWithString:[self createFormattedTime]]; + highlightedString = [[NSMutableAttributedString alloc] initWithString:[self createHumanFormattedTime]]; [highlightedString beginEditing]; [self addBasicMainTextAttributes:highlightedString]; [highlightedString endEditing]; @@ -282,6 +282,27 @@ - (NSString*) createFormattedTime { return formattedTime; } +- (NSString*) createHumanFormattedTime { + NSDate* now = [NSDate date]; + + NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; + NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; + NSInteger hour = [dateComponents hour]; + NSInteger minute = [dateComponents minute]; + + NSString *ampm = @"am"; + if (hour > 12) { + hour = hour - 12; + ampm = @"pm"; + } + + NSString *paddedHour = [NSString stringWithFormat:@"%02ld", hour]; + NSString *paddedMinute = [NSString stringWithFormat:@"%02ld", minute]; + NSString *formattedTime = [NSString stringWithFormat:@"%@:%@ %@", paddedHour, paddedMinute, ampm]; + + return formattedTime; +} + - (BOOL) selectNextQuote { NSString *formattedTime = [self createFormattedTime]; BOOL quoteChanged = NO;