Skip to content

Instantly share code, notes, and snippets.

@theoknock
Created September 22, 2020 02:08
Show Gist options
  • Save theoknock/db16e20aa7bc36d90612b2cdba562b9c to your computer and use it in GitHub Desktop.
Save theoknock/db16e20aa7bc36d90612b2cdba562b9c to your computer and use it in GitHub Desktop.
Converting current CMTime to NSString
typedef CMTime(^CurrentCMTime)(void);
static CurrentCMTime _Nonnull current_cmtime = ^ CMTime (void) {
return CMClockGetTime(CMClockGetHostTimeClock());
};
typedef NSString * _Nonnull (^StringFromTime)(CMTime);
static StringFromTime _Nonnull cmTimeString = ^ NSString * (CMTime cm_time) {
NSString *stringFromCMTime;
float seconds = round(CMTimeGetSeconds(cm_time));
int hh = (int)floorf(seconds / 3600.0f);
int mm = (int)floorf((seconds - hh * 3600.0f) / 60.0f);
int ss = (((int)seconds) % 60);
if (hh > 0)
{
stringFromCMTime = [NSString stringWithFormat:@"%02d:%02d:%02d", hh, mm, ss];
}
else
{
stringFromCMTime = [NSString stringWithFormat:@"%02d:%02d", mm, ss];
}
return stringFromCMTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment