Skip to content

Instantly share code, notes, and snippets.

@Me1000
Created August 14, 2012 06:09
Show Gist options
  • Select an option

  • Save Me1000/3346813 to your computer and use it in GitHub Desktop.

Select an option

Save Me1000/3346813 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#define SECOND_INTERVAL 1
#define MINUTE_INTERVAL SECOND_INTERVAL * 60
#define HOUR_INTERVAL MINUTE_INTERVAL * 60
#define DAY_INTERVAL HOUR_INTERVAL * 24
#define WEEK_INTERVAL DAY_INTERVAL * 7
int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
NSDate *now = [NSDate date];
NSInteger time = (NSInteger)[now timeIntervalSinceReferenceDate];
NSInteger mod = time % DAY_INTERVAL;
NSInteger expectedValue = (NSInteger)[now timeIntervalSinceReferenceDate];
while (expectedValue - DAY_INTERVAL >= 0)
expectedValue -= DAY_INTERVAL;
NSLog(@"Day: %d", DAY_INTERVAL);
NSLog(@"Time: %d", time);
NSLog(@"Modded value: %d", mod);
NSLog(@"Loopy value: %d", expectedValue);
NSLog(@"Is this right? %@", expectedValue == mod ? @"Why yes, yes it is..." : @"Nope, magic C things are breaking!");
[p release];
}
@Me1000
Copy link
Author

Me1000 commented Aug 14, 2012

output:
2012-08-14 01:15:04.550 Untitled 2[30690:707] Day: 86400
2012-08-14 01:15:04.554 Untitled 2[30690:707] Time: 366617704
2012-08-14 01:15:04.554 Untitled 2[30690:707] Modded value: 0
2012-08-14 01:15:04.555 Untitled 2[30690:707] Loopy value: 22504
2012-08-14 01:15:04.556 Untitled 2[30690:707] Is this right? Nope, magic C things are breaking!

@JensAyton
Copy link

Days are not always 86400 seconds long. This type of calculation is almost always the Wrong Thing. Use -[NSCalendar components:fromDate:toDate:options:] instead.

@Me1000
Copy link
Author

Me1000 commented Aug 14, 2012

Yep, good call. Defines were easier for terrible prototyping :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment