Permalink
Browse files

Add Demo Project | Readme updated

  • Loading branch information...
1 parent e439b0e commit aededfe1f641fc695d3462e678477e5589f19f94 Rousseau Quentin committed Nov 24, 2012
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,131 +0,0 @@
-//
-// NSDate+HumanizedTime.m
-// HumanizedTimeDemo
-//
-// Created by Sarp Erdag on 2/29/12.
-// Copyright (c) 2012 Sarp Erdag. All rights reserved.
-//
-
-#import "NSDate+HumanizedTime.h"
-
-#define LOCALISABLE_FULL @"Localizable_full"
-#define LOCALISABLE_SHORT @"Localizable"
-#define SUFFIX_UNTIL @"Until"
-#define PREFIX_LEFT @"left"
-#define PREFIX_AGO @"ago"
-
-@implementation NSDate (HumanizedTime)
-
-- (NSString *) stringWithHumanizedTimeDifference:(NSDateHumanizedType) humanizedType withFullString:(BOOL) fullStrings
-{
- NSTimeInterval timeInterval = [self timeIntervalSinceNow];
-
- int secondsInADay = 3600*24;
- int secondsInAWeek = 3600*24*7;
- int secondsInAMonth = 3600*24*30; //To fix, not precise
- int secondsInAYear = 3600*24*365;
- int yearsDiff = abs(timeInterval/secondsInAYear);
- int monthsDiff = abs(timeInterval/secondsInAMonth);
- int weeksDiff = abs(timeInterval/secondsInAWeek);
- int daysDiff = abs(timeInterval/secondsInADay);
- int hoursDiff = abs((abs(timeInterval) - (daysDiff * secondsInADay)) / 3600);
- int minutesDiff = abs((abs(timeInterval) - ((daysDiff * secondsInADay) + (hoursDiff * 60))) / 60);
- int secondsDiff = abs((abs(timeInterval) - ((daysDiff * secondsInADay) + (minutesDiff * 60))));
-
- NSString *yearString;
- NSString *dateString;
- NSString *weekString;
- NSString *dayString;
- NSString *hourString;
- NSString *minuteString;
- NSString *secondString;
-
- NSDateFormatter *yearDateFormatter = [[NSDateFormatter alloc] init];
- yearDateFormatter.dateFormat = @"YYYY-MM-dd";
-
- NSDateFormatter *dateDateFormatter = [[NSDateFormatter alloc] init];
- dateDateFormatter.dateFormat = @"dd MMM";
-
- NSString *translation_table = (fullStrings) ? LOCALISABLE_FULL : LOCALISABLE_SHORT;
-
- switch (humanizedType)
- {
- case NSDateHumanizedSuffixNone:
- {
- yearString = [yearDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
- dateString = [dateDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
- weekString = [NSString stringWithFormat:@"%d%@", weeksDiff, NSLocalizedStringFromTable(@"WeekKey", translation_table, @"")];
- dayString = [NSString stringWithFormat:@"%d%@", daysDiff, NSLocalizedStringFromTable(@"DayKey", translation_table, @"")];
- hourString = [NSString stringWithFormat:@"%d%@", hoursDiff, NSLocalizedStringFromTable(@"HourKey", translation_table, @"")];
- minuteString = [NSString stringWithFormat:@"%d%@", minutesDiff, NSLocalizedStringFromTable(@"MinuteKey", translation_table, @"")];
- secondString = [NSString stringWithFormat:@"%@", NSLocalizedStringFromTable(@"SecondKey", translation_table, @"")];
- break;
- }
- case NSDateHumanizedSuffixLeft:
- {
- yearString = [NSString stringWithFormat:@"%@ %@", SUFFIX_UNTIL, [yearDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]]];
- dateString = [NSString stringWithFormat:@"%@ %@", SUFFIX_UNTIL, [dateDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]]];
- weekString = [NSString stringWithFormat:@"%d %@%@ %@", weeksDiff, NSLocalizedStringFromTable(@"WeekKey", translation_table, @""), (weeksDiff == 1 ? @"" : @"s"), PREFIX_LEFT];
- dayString = [NSString stringWithFormat:@"%d %@%@ %@", daysDiff, NSLocalizedStringFromTable(@"DayKey", translation_table, @""), (daysDiff == 1 ? @"" : @"s"), PREFIX_LEFT];
- hourString = [NSString stringWithFormat:@"%d %@%@ %@", hoursDiff, NSLocalizedStringFromTable(@"HourKey", translation_table, @""), (hoursDiff == 1 ? @"" : @"s"), PREFIX_LEFT];
- minuteString = [NSString stringWithFormat:@"%d %@%@ %@", minutesDiff, NSLocalizedStringFromTable(@"MinuteKey", translation_table, @""), (minutesDiff == 1 ? @"" : @"s"), PREFIX_LEFT];
- secondString = [NSString stringWithFormat:@"%d %@s %@", secondsDiff, NSLocalizedStringFromTable(@"SecondKey", translation_table, @""), PREFIX_LEFT];
-
- break;
- }
- case NSDateHumanizedSuffixAgo:
- {
- yearString = [yearDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
- dateString = [[dateDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]] stringByAppendingString:@"."];
- weekString = [NSString stringWithFormat:@"%d %@%@ %@", weeksDiff, NSLocalizedStringFromTable(@"WeekKey", translation_table, @""), (weeksDiff == 1 ? @"" : @"s"), PREFIX_AGO];
- dayString = [NSString stringWithFormat:@"%d %@%@ %@", daysDiff, NSLocalizedStringFromTable(@"DayKey", translation_table, @""), (daysDiff == 1 ? @"" : @"s"), PREFIX_AGO];
- hourString = [NSString stringWithFormat:@"%d %@%@ %@", hoursDiff, NSLocalizedStringFromTable(@"HourKey", translation_table, @""), (hoursDiff == 1 ? @"" : @"s"), PREFIX_AGO];
- minuteString = [NSString stringWithFormat:@"%d %@%@ %@", minutesDiff, NSLocalizedStringFromTable(@"MinuteKey", translation_table, @""), (minutesDiff == 1 ? @"" : @"s"), PREFIX_AGO];
- secondString = [NSString stringWithFormat:@"%d %@s %@", secondsDiff, NSLocalizedStringFromTable(@"SecondKey", translation_table, @""), PREFIX_AGO];
- break;
- }
- }
-
- if (yearsDiff > 1)
- {
- return yearString;
- }
-
- if (monthsDiff > 0)
- {
- return dateString;
- }
- else
- {
- if (weeksDiff > 0)
- {
- return weekString;
- }
- else
- {
- if (daysDiff > 0)
- {
- return dayString;
- }
- else
- {
- if (hoursDiff == 0)
- {
- if (minutesDiff == 0)
- return secondString;
- else
- return minuteString;
- }
- else
- {
- if (hoursDiff == 1)
- return [NSString stringWithFormat:@"%@ 1%@", NSLocalizedString(@"AboutKey", @""), NSLocalizedString(@"HourKey", @"")];
- else
- return hourString;
- }
- }
- }
- }
-}
-
-@end
View
@@ -42,7 +42,7 @@ NSDateHumanizedSuffixNone && withFullString:NO
``` plain
Year : 2010-09-14 | if > 1 year
-Months : 2Apr | if > 1 months but in the same year
+Months : 2Apr. | if > 1 months but in the same year
Weeks : 3w
@@ -78,9 +78,9 @@ NSDateHumanizedSuffixLeft | && withFullString:NO
---------------------
``` plain
-Year : Until 2010-09-14 | if > 1 year
+Year : Until the 2010-09-14 | if > 1 year
-Months : Until 2Apr | if > 1 months but in the same year
+Months : Until the 2Apr. | if > 1 months but in the same year
Weeks : 3w left
@@ -97,9 +97,9 @@ NSDateHumanizedSuffixLeft | && withFullString:YES
---------------------
``` plain
-Year : Until 2010-09-14 | if > 1 year
+Year : Until the 2010-09-14 | if > 1 year
-Months : Until 2April | if > 1 months but in the same year
+Months : Until the 2April | if > 1 months but in the same year
Weeks : 3 week(s) left | With dynamic pluralize
@@ -118,7 +118,7 @@ NSDateHumanizedSuffixAgo | && withFullString:NO
``` plain
Year : 2010-09-14 | if > 1 year
-Months : 2Apr | if > 1 months but in the same year
+Months : 2Apr. | if > 1 months but in the same year
Weeks : 3w ago
@@ -136,7 +136,7 @@ NSDateHumanizedSuffixAgo | && withFullString:YES
---------------------
``` plain
-Year : 2010-09-14 | if > 1 year
+Year : 2010-09-14 | if > 1 year
Months : 2 April | if > 1 months but in the same year
@@ -148,7 +148,7 @@ Hours : 4 hour(s) ago | With dynamic pluralize
Minutes : 9 minute(s) ago | With dynamic pluralize
-Secondes : 3 seconde(s) ago left | With dynamic pluralize
+Secondes : 3 seconde(s) ago | With dynamic pluralize
```
@@ -0,0 +1,179 @@
+//
+// NSDate+HumanizedTime.m
+// HumanizedTimeDemo
+//
+// Created by Sarp Erdag on 2/29/12.
+// Copyright (c) 2012 Sarp Erdag. All rights reserved.
+//
+
+#import "NSDate+HumanizedTime.h"
+
+#define LOCALISABLE_FULL @"Localizable_full"
+#define LOCALISABLE_SHORT @"Localizable"
+#define SUFFIX_UNTIL @"Until the "
+#define PREFIX_LEFT @" left"
+#define PREFIX_AGO @" ago"
+
+@implementation NSDate (HumanizedTime)
+
+- (NSString *) stringWithHumanizedTimeDifference:(NSDateHumanizedType) humanizedType withFullString:(BOOL) fullStrings
+{
+ NSTimeInterval timeInterval = [self timeIntervalSinceNow];
+
+ int secondsInADay = 3600*24;
+ int secondsInAWeek = 3600*24*7;
+ int secondsInAMonth = 3600*24*30; //To fix, not precise
+ int secondsInAYear = 3600*24*365;
+ int yearsDiff = abs(timeInterval/secondsInAYear);
+ int monthsDiff = abs(timeInterval/secondsInAMonth);
+ int weeksDiff = abs(timeInterval/secondsInAWeek);
+ int daysDiff = abs(timeInterval/secondsInADay);
+ int hoursDiff = abs((abs(timeInterval) - (daysDiff * secondsInADay)) / 3600);
+ int minutesDiff = abs((abs(timeInterval) - ((daysDiff * secondsInADay) + (hoursDiff * 60))) / 60);
+ int secondsDiff = abs((abs(timeInterval) - ((daysDiff * secondsInADay) + (minutesDiff * 60))));
+
+ NSString *yearString;
+ NSString *dateString;
+ NSString *weekString;
+ NSString *dayString;
+ NSString *hourString;
+ NSString *minuteString;
+ NSString *secondString;
+ NSString *full_yearString;
+ NSString *full_dateString;
+ NSString *full_weekString;
+ NSString *full_dayString;
+ NSString *full_hourString;
+ NSString *full_minuteString;
+ NSString *full_secondString;
+
+ NSDateFormatter *yearDateFormatter = [[NSDateFormatter alloc] init];
+ yearDateFormatter.dateFormat = @"YYYY-MM-dd";
+
+ NSDateFormatter *full_yearDateFormatter = [[NSDateFormatter alloc] init];
+ full_yearDateFormatter.dateFormat = @"YYYY-MM-dd";
+
+ NSDateFormatter *dateDateFormatter = [[NSDateFormatter alloc] init];
+ dateDateFormatter.dateFormat = @"dd MMM.";
+
+ NSDateFormatter *full_dateDateFormatter = [[NSDateFormatter alloc] init];
+ full_dateDateFormatter.dateFormat = @"dd MMMM";
+
+ NSString *translation_table = (fullStrings) ? LOCALISABLE_FULL : LOCALISABLE_SHORT;
+
+ //NSDateHumanizedSuffixNone
+ yearString = [yearDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
+ dateString = [dateDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
+ weekString = [NSString stringWithFormat:@"%d%@", weeksDiff, NSLocalizedStringFromTable(@"WeekKey", translation_table, @"")];
+ dayString = [NSString stringWithFormat:@"%d%@", daysDiff, NSLocalizedStringFromTable(@"DayKey", translation_table, @"")];
+ hourString = [NSString stringWithFormat:@"%d%@", hoursDiff, NSLocalizedStringFromTable(@"HourKey", translation_table, @"")];
+ minuteString = [NSString stringWithFormat:@"%d%@", minutesDiff, NSLocalizedStringFromTable(@"MinuteKey", translation_table, @"")];
+ secondString = [NSString stringWithFormat:@"%d%@", secondsDiff, NSLocalizedStringFromTable(@"SecondKey", translation_table, @"")];
+
+ full_yearString = [full_yearDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
+ full_dateString = [full_dateDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]];
+ full_weekString = [NSString stringWithFormat:@"%d %@%@", weeksDiff,
+ NSLocalizedStringFromTable(@"WeekKey", translation_table, @""),
+ (weeksDiff == 1) ? @"" : @"s"];
+ full_dayString = [NSString stringWithFormat:@"%d %@%@", daysDiff,
+ NSLocalizedStringFromTable(@"DayKey", translation_table, @""),
+ (daysDiff == 1) ? @"" : @"s"];
+ full_hourString = [NSString stringWithFormat:@"%d %@%@", hoursDiff,
+ NSLocalizedStringFromTable(@"HourKey", translation_table, @""),
+ (hoursDiff == 1) ? @"" : @"s"];
+ full_minuteString = [NSString stringWithFormat:@"%d %@%@", minutesDiff,
+ NSLocalizedStringFromTable(@"MinuteKey", translation_table, @""),
+ (minutesDiff == 1) ? @"" : @"s"];
+ full_secondString = [NSString stringWithFormat:@"%d %@%@", secondsDiff,
+ NSLocalizedStringFromTable(@"SecondKey", translation_table, @""),
+ (secondsDiff == 1) ? @"" : @"s"];
+
+ switch (humanizedType)
+ {
+ default: break;
+ case NSDateHumanizedSuffixLeft:
+ {
+ if(!fullStrings)
+ {
+ yearString = [SUFFIX_UNTIL stringByAppendingString:yearString];
+ dateString = [SUFFIX_UNTIL stringByAppendingString:dateString];
+ weekString = [weekString stringByAppendingString:PREFIX_LEFT];
+ dayString = [dayString stringByAppendingString:PREFIX_LEFT];
+ hourString = [hourString stringByAppendingString:PREFIX_LEFT];
+ minuteString = [minuteString stringByAppendingString:PREFIX_LEFT];
+ secondString = [secondString stringByAppendingString:PREFIX_LEFT];
+ }
+ else
+ {
+ full_yearString = [SUFFIX_UNTIL stringByAppendingString:full_yearString];
+ full_dateString = [SUFFIX_UNTIL stringByAppendingString:full_dateString];
+ full_weekString = [full_weekString stringByAppendingString:PREFIX_LEFT];
+ full_dayString = [full_dayString stringByAppendingString:PREFIX_LEFT];
+ full_hourString = [full_hourString stringByAppendingString:PREFIX_LEFT];
+ full_minuteString = [full_minuteString stringByAppendingString:PREFIX_LEFT];
+ full_secondString = [full_secondString stringByAppendingString:PREFIX_LEFT];
+ }
+ break;
+ }
+ case NSDateHumanizedSuffixAgo:
+ {
+ if(!fullStrings)
+ {
+ weekString = [weekString stringByAppendingString:PREFIX_AGO];
+ dayString = [dayString stringByAppendingString:PREFIX_AGO];
+ hourString = [hourString stringByAppendingString:PREFIX_AGO];
+ minuteString = [minuteString stringByAppendingString:PREFIX_AGO];
+ secondString = [secondString stringByAppendingString:PREFIX_AGO];
+ }
+ else
+ {
+ full_weekString = [full_weekString stringByAppendingString:PREFIX_AGO];
+ full_dayString = [full_dayString stringByAppendingString:PREFIX_AGO];
+ full_hourString = [full_hourString stringByAppendingString:PREFIX_AGO];
+ full_minuteString = [full_minuteString stringByAppendingString:PREFIX_AGO];
+ full_secondString = [full_secondString stringByAppendingString:PREFIX_AGO];
+ }
+ break;
+ }
+ }
+
+ if (yearsDiff > 1)
+ {
+ return (fullStrings)? full_yearString : yearString;
+ }
+
+ if (monthsDiff > 0)
+ {
+ return (fullStrings)? full_dateString : dateString;
+ }
+ else
+ {
+ if (weeksDiff > 0)
+ {
+ return (fullStrings)? full_weekString : weekString;
+ }
+ else
+ {
+ if (daysDiff > 0)
+ {
+ return (fullStrings)? full_dayString : dayString;
+ }
+ else
+ {
+ if (hoursDiff == 0)
+ {
+ if (minutesDiff == 0)
+ return (fullStrings)? full_secondString : secondString;
+ else
+ return (fullStrings)? full_minuteString : minuteString;
+ }
+ else
+ {
+ return (fullStrings)? full_hourString : hourString;
+ }
+ }
+ }
+ }
+}
+
+@end
@@ -8,9 +8,5 @@
"MinuteKey" = "m";
-"SecondKey" = "now";
-
-"LaterKey" = "later";
-
-"AboutKey" = "about";
+"SecondKey" = "s";
@@ -8,9 +8,4 @@
"MinuteKey" = "minute";
-"SecondKey" = "seconde";
-
-"LaterKey" = "later";
-
-"AboutKey" = "about";
-
+"SecondKey" = "seconde";
Oops, something went wrong.

0 comments on commit aededfe

Please sign in to comment.