Created
August 14, 2012 06:09
-
-
Save Me1000/3346813 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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]; | |
| } |
Author
Days are not always 86400 seconds long. This type of calculation is almost always the Wrong Thing. Use -[NSCalendar components:fromDate:toDate:options:] instead.
Author
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
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!