Last active
August 29, 2015 14:11
-
-
Save HomerJSimpson/11ef05d42be538b02cd2 to your computer and use it in GitHub Desktop.
NSDate experiment
This file contains 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
main | |
*.o |
This file contains 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> | |
@interface DSTimer : NSObject | |
+(instancetype)timerWithInterval:(double)interval | |
andBlock:(void(^)())callback; | |
-(instancetype)initWithInterval:(double)interval | |
andBlock:(void(^)())callback; | |
- (void)start; | |
- (void)cancel; | |
@end |
This file contains 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> | |
#import "DSTimer.h" | |
@implementation DSTimer { | |
dispatch_source_t _timer; | |
double _interval; | |
void (^_callback)(); | |
} | |
+(instancetype)timerWithInterval:(double)interval | |
andBlock:(void(^)())callback { | |
return [[DSTimer alloc] initWithInterval:interval andBlock:callback]; | |
} | |
-(instancetype)initWithInterval:(double)interval | |
andBlock:(void(^)())callback { | |
if(self = [super init]) { | |
_interval = interval; | |
_callback = callback; | |
} | |
return self; | |
} | |
- (void)start { | |
NSLog(@"start: %@", self); | |
_timer = dispatch_source_create( | |
DISPATCH_SOURCE_TYPE_TIMER, | |
0, | |
0, | |
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
); | |
if(!_timer) { | |
exit(1); | |
} | |
dispatch_source_set_timer( | |
_timer, dispatch_time(DISPATCH_TIME_NOW, _interval * NSEC_PER_SEC), | |
_interval * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10 | |
); | |
dispatch_source_set_event_handler(_timer, _callback); | |
dispatch_resume(_timer); | |
} | |
- (void)cancel { | |
if(_timer) { | |
dispatch_source_cancel(_timer); | |
_timer = nil; | |
} | |
} | |
@end |
This file contains 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> | |
#import "DSTimer.h" | |
NSString *biggamenextdate = @"12/23/14"; | |
NSString *biggamenextestimate = @"113"; | |
int | |
main (int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSLog(@"%@", | |
[NSString localizedStringWithFormat:@"%-22s %11s\n", "megamillions", "113"] | |
); | |
NSLog(@"%@", | |
[NSString localizedStringWithFormat:@"%-8s %s\n", "next ", "00/00/0000 0d:00h:00m:00s"] | |
); | |
NSLog(@"%@", | |
[NSString localizedStringWithFormat:@"%-22s %11s\n", "powerball", "80"] | |
); | |
NSLog(@"%@", | |
[NSString localizedStringWithFormat:@"%-8s %s\n", "next ", "00/00/0000 0d:00h:00m:00s"] | |
); | |
NSDateFormatter *format = [[NSDateFormatter alloc] init]; | |
[format setDateFormat:@"MM/dd/yy HH:mm:ss zz"]; | |
[format setTimeZone:[NSTimeZone defaultTimeZone]]; | |
NSLog(@"date: %@", [format stringFromDate:[NSDate date]]); | |
NSLog(@"date: %@", [format stringFromDate:[format dateFromString: | |
[NSString localizedStringWithFormat:@"%@ 23:00:00 EST", biggamenextdate ]] | |
]); | |
NSLog(@"default tz: %@", [NSTimeZone defaultTimeZone]); | |
// date calculations | |
// biggamenextdate - now = [days hour minutes seconds] | |
NSLog(@"\n\ndate calculations\n########################################################################\n"); | |
void (^tick)() = ^void { | |
NSLog(@"tick"); | |
NSDateComponents *components = [[NSCalendar currentCalendar] | |
components : NSCalendarUnitSecond | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitDay | |
fromDate : [NSDate date] | |
toDate : [format dateFromString : | |
[NSString localizedStringWithFormat:@"%@ 23:00:00 EST", biggamenextdate] | |
] | |
options : 0 | |
]; | |
NSLog(@"%@", components); | |
}; | |
DSTimer *timer = [DSTimer timerWithInterval:0.5 andBlock:tick]; | |
[timer start]; | |
[[NSRunLoop currentRunLoop] run]; | |
} | |
return 0; | |
} |
This file contains 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
LDFLAGS = -framework Foundation | |
main : DSTimer.o | |
all : main | |
main : main.m | |
clean : | |
-rm -f main DSTimer.o | |
run : all | |
./main |
This file contains 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
#! /Users/nlehman/local/macports/bin/perl | |
use strict; | |
use warnings; | |
use LWP::Simple; | |
use JSON; | |
my $content = from_json(get("https://query.yahooapis.com/v1/public/yql/homerj/retirement?format=json")); | |
my @elements = split(/&/, $content->{query}{results}{html}{body}); | |
foreach my $element (@elements) { | |
chomp $element; | |
my ($key, $value) = split(/=/, $element); | |
$value =~ s/^\s+|\s+$//g if $value; | |
printf("%-22s %9s\n", $key, $value) if $key && $key =~ /^big|power/; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment