ear-fung.us I’m a programmer. I’m also pro-grammar.

17May/106

The Hackers’ Code

The Hacker's Code

"A hacker of the Old Code."

  • Hackers come and go, but a great hack is forever.
  • Public goods belong to the public.*
  • Software hoarding is evil.
  • Software does the greatest good given to the greatest number.
  • Don't be evil.
  • Sourceless software sucks.
  • People have rights.
  • Organizations live on sufferance.
  • Governments are organizations.
  • If it is wrong when citizens do it, it is wrong when governments do it.
  • Information wants to be free.
  • Information deserves to be free.
  • Being legal doesn't make it right.
  • Being illegal doesn't make it wrong.
  • Subverting tyranny is the highest duty.
  • Trust your technolust!

*A good is public if the marginal production cost is lower than the marginal billing cost.

Discuss.

[via]

14Nov/082

All Sorts of UNIX Timestamp Goodness

I'm writing some tracking software and came upon the need to be able to get the two timestamps (UNIX timestamp: the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds) of a given period of time. This period of time would be represented in words via a GET parameter in my application. To you, I present my switch statement to produce those two timestamps.

The hardest of all was getting "this week last year". Please note that all weeks begin on Monday.
You can check the resulting timestamps using this cool online timestamp converter.

switch($period)
{
	case "yesterday":
		$timestamp_start = mktime(0, 0, 0, date("n"), date("d")-1, date("Y"));
		$timestamp_stop = mktime(23, 59, 59, date("n"), date("d")-1, date("Y"));
		break;
	case "this_week":
		$timestamp_start = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - ((date("N")-1)*3600*24);
		$timestamp_stop = time();
		break;
	case "this_month":
		$timestamp_start = mktime(0,0,0, date("n"), 1, date("Y"));
		$timestamp_stop = time();
		break;
	case "this_year":
		$timestamp_start = mktime(0,0,0, 1, 1, date("Y"));
		$timestamp_stop = time();
		break;
	case "last_week":
		$timestamp_start = mktime(0, 0, 0, date("n"), date("j")-6, date("Y")) - ((date("N"))*3600*24);
		$timestamp_stop = mktime(23, 59, 59, date("n"), date("j"), date("Y")) - ((date("N"))*3600*24);
		break;
	case "last_month":
		$timestamp_start = mktime(0,0,0,date("n")-1, 1, date("Y"));
		$timestamp_stop =  mktime(23, 59, 59, date("n"), date("d")-date("j"), date("Y"));
		break;
	case "last_year":
		$timestamp_start = mktime(0,0,0,1, 1, date("Y")-1);
		$timestamp_stop = mktime(23,59,59,12,31, date("Y")-1);
		break;
	case "last_half_hour":
		$timestamp_start = mktime(date("h"), date("i")-30, date("s"), date("n"), date("d"), date("Y"));
		$timestamp_stop = time();
		break;
	case "last_hour":
		$timestamp_start = mktime(date("h")-1, date("i"), date("s"), date("n"), date("d"), date("Y"));
		$timestamp_stop = time();
		break;
	case "last_24_hours":
		$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-1, date("Y"));
		$timestamp_stop = time();
		break;
	case "last_seven_days":
		$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-7, date("Y"));
		$timestamp_stop = time();
		break;
	case "last_thirty_days":
		$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-30, date("Y"));
		$timestamp_stop = time();
		break;
	case "same_day_last_week":
		$timestamp_start = mktime(0, 0, 0, date("n"), date("d")-7, date("Y"));
		$timestamp_stop = mktime(23, 59, 59, date("n"), date("d")-7, date("Y"));
		break;
	case "same_week_last_year":
		$timestamp_start = mktime(0, 0, 0, date("n"), date("j"), date("Y")-1)
		- ( (date("N", mktime(0, 0, 0, date("n"), date("j"), date("Y") - 1 ) ) - 1 ) * 86400 );
		$timestamp_stop = mktime(23, 59, 59, date("n"), date("j") + 7, date("Y")-1)
		- ( (date("N", mktime(0, 0, 0, date("n"), date("j"), date("Y") - 1 ) ) - 1 ) * 86400 );
		break;
	case "same_month_last_year":
		$timestamp_start = mktime(0,0,0, date("n"), 1, date("Y") - 1);
		$timestamp_stop = mktime(23, 59, 59, date("n"), date("t"), date("Y") - 1);
		break;
	case "today":
	default:
		$timestamp_start = mktime(0,0,0, date("n"), date("d"), date("Y"));
		$timestamp_stop = time();
		break;
}
3Jul/087

My First Objective-C Cocoa Code – “String Between”

I needed a string searching method in Objective-C for a little app I'm writing that searched a string and found substrings in-between two other strings... sort of a poor man's regex. It extends the NSString object so once you've imported it in your main header, you can call the method on an NSString object. I couldn't find anything in the Cocoa API that did what I was looking for so I did it myself.

Here's what I came up with... I present to you: "StringBetween"

StringBetween.h

@interface NSString (StringBetween)

-(NSString *)stringBetween:(NSString*)aString and:(NSString *)bString;

@end

StringBetween.m

#import "StringBetween.h"
@implementation NSString (StringBetween)

-(NSString *)stringBetween:(NSString *)aString and:(NSString *)bString
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];

    //Find the range of the first string
    NSRange aRange = [self rangeOfString:aString options:(NSCaseInsensitiveSearch)];

    if(aRange.length > 0){
        //Make a new range to search for the next string
        NSRange searchRange;
        searchRange.location = aRange.location;
        searchRange.length = [self length] - aRange.location;

        //Find the next string
        NSRange bRange = [self rangeOfString:bString options:(NSCaseInsensitiveSearch) range:searchRange];

        //NSLog(@"%d, %d", aRange.location, aRange.length);
        //NSLog(@"%d, %d", bRange.location, bRange.length);

        if(bRange.length > 0)
        {
            searchRange.location = aRange.location + aRange.length;
            searchRange.length = bRange.location - searchRange.location;

           return [self substringWithRange:searchRange];
        }
    }
    return @"";
    [autoreleasepool release];
}
@end

Here is an actual implementation example:

main.m

#import <cocoa/Cocoa.h>
#import "StringBetween.h"

int main (int argc, const char * argv[]) {

    NSString *thisString = @"The quick brown fox jumped over the lazy dog.";

    NSLog([thisString stringBetween:@"qui" and:@"the"]);
    //Returns @"ck brown fox jumped over "

    NSLog([thisString stringBetween:@"garbage" and:@"more garbage"]);
    //Returns @""

    return 0;
}

I hope I didn't muck up that code too bad. I'm still learning Objective-C.

Here's an X-Code project download if you want to see it in action.
Download this xcode project