In case anyone is interested, I did a straight port of the PHP code for calculating the Glitch date and time to Objective C. See below. I
think it's working correctly, but seem to keep coming up with an incorrect day. The year, month, hour and minute are right, but the day seems off. Take a look and tell me where I've gone wrong...
+(NSString *) server_date_time {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ";
NSDate *startDate = [df dateFromString:@"2009-04-04 05:00:00 UTC"];
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSArray *dayNames = [NSArray arrayWithObjects:@"Hairday", @"Moonday", @"Twoday", @"Weddingday", @"Theday", @"Fryday", @"Standday", @"Fabday", nil];
NSArray *monthNames = [NSArray arrayWithObjects:@"Primuary", @"Spork", @"Bruise", @"Candy", @"Fever", @"Junuary", @"Septa", @"Remember", @"Doom", @"Widdershins", @"Eleventy", @"Recurse", nil];
NSTimeInterval secs = [now timeIntervalSinceDate:startDate];int year = floor(secs / 4435200);
secs -= year * 4435200;
int day_of_year = floor(secs / 14400);
secs -= day_of_year * 14400;
int hour = floor(secs / 600);
secs -= hour * 600;
int minute = floor(secs / 10);
secs -= minute * 10;int month_of_year = 0;
int day_of_month = 0;
int months[] = {29, 3, 53, 17, 73, 19, 13, 37, 5, 47, 11, 1};
int cd = 0;
for (int i=0; i<12; i++){
cd += months[i];
if (cd > day_of_year){
month_of_year = i+1;
day_of_month = day_of_year + 1 - (cd - months[i]);
break;
}
}
int days_since_epoch = day_of_year + (307 * year);
int day_of_week = days_since_epoch % 8;
NSString *ap = (hour >= 12)?@"pm":@"am";
if (hour > 12) hour -= 12; NSString *sfx = @"th";
NSString *dnum = [NSString stringWithFormat:@"%i", day_of_month];
if ([dnum hasSuffix:@"1"])
sfx = @"st";
else if ([dnum hasSuffix:@"2"])
sfx = @"nd";
else if ([dnum hasSuffix:@"3"])
sfx = @"rd";
NSString *dayName = [dayNames objectAtIndex:day_of_week];
NSString *monthName = [monthNames objectAtIndex:month_of_year];
NSString *out = [NSString stringWithFormat:@"%i:%i %@, %@ %i%@ of %@, year %i", hour, minute, ap, dayName, day_of_month, sfx, monthName, year];
NSLog(@"%@",out);
return out;
}
Posted 16 months ago by
Jifka

|
Permalink