Permalink
Browse files

merging datetime changes

  • Loading branch information...
2 parents 3fa34b9 + 94120e6 commit 1cac2adaf74a144472780a99d505dd96e0e891cb @ettore committed Nov 23, 2012
Showing with 43 additions and 4 deletions.
  1. +6 −3 clcg_date_utils.h → clcg_datetime_utils.h
  2. +37 −1 clcg_date_utils.m → clcg_datetime_utils.m
@@ -26,8 +26,8 @@
// Created by Ettore Pasquini on 9/20/11.
//
-#ifndef CLCG_DATE_UTILS_H_
-#define CLCG_DATE_UTILS_H_
+#ifndef CLCG_DATETIME_UTILS_H_
+#define CLCG_DATETIME_UTILS_H_
#import <Foundation/Foundation.h>
@@ -37,7 +37,10 @@ extern "C" {
/** Get the year out of a date object. */
NSInteger clcg_date_year(NSDate *d);
-
+
+/** Formats `d` as `X days Y hour Z seconds agp` from todays date/time. */
+NSString *clcg_ago_format(NSDate *d);
+
#ifdef __cplusplus
}
#endif
@@ -26,7 +26,7 @@
// Created by Ettore Pasquini on 9/20/11.
//
-#import "clcg_date_utils.h"
+#import "clcg_datetime_utils.h"
/** Get the year out of a date object. */
NSInteger clcg_date_year(NSDate *d)
@@ -38,3 +38,39 @@ NSInteger clcg_date_year(NSDate *d)
comps = [greg components:NSYearCalendarUnit fromDate:d];
return [comps year];
}
+
+NSString *clcg_ago_format(NSDate *d)
+{
+ NSCalendar *greg;
+ unsigned int flags;
+ NSDateComponents *dc;
+ NSDate *now = [NSDate date];
+ NSInteger t;
+ NSString *res, *interval;
+
+ greg = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
+ flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
+ NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
+ dc = [greg components:flags fromDate:d toDate:now options:0];
+ [greg release];
+
+ if ((t = [dc year])) {
+ interval = ((t == 1) ? CLCG_LOC(@"year") : CLCG_LOC(@"years"));
+ } else if ((t = [dc month])) {
+ interval = ((t == 1) ? CLCG_LOC(@"month") : CLCG_LOC(@"months"));
+ } else if ((t = [dc day])) {
+ interval = ((t == 1) ? CLCG_LOC(@"day") : CLCG_LOC(@"days"));
+ } else if ((t = [dc hour])) {
+ interval = ((t == 1) ? CLCG_LOC(@"hour") : CLCG_LOC(@"hours"));
+ } else if ((t = [dc minute])) {
+ interval = ((t == 1) ? CLCG_LOC(@"minute") : CLCG_LOC(@"minutes"));
+ } else if ((t = [dc second])) {
+ interval = ((t == 1) ? CLCG_LOC(@"second") : CLCG_LOC(@"seconds"));
+ } else {
+ return CLCG_LOC(@"just about now");
+ }
+
+ res = [NSString stringWithFormat:@"%d %@ %@",t,interval,CLCG_LOC(@"ago")];
+
+ return res;
+}

0 comments on commit 1cac2ad

Please sign in to comment.