2009-06-27 10 views
11

Mi piacerebbe scrivere un metodo di data fuzzy per il calcolo delle date in Objective-C per iPhone. C'è una spiegazione popolare qui:Algoritmo Fuzzy Date in Objective-C

Calculate relative time in C#

Tuttavia essa contiene argomenti mancanti. Come potrebbe essere usato in Objective-C ?. Grazie.

const int SECOND = 1; 
const int MINUTE = 60 * SECOND; 
const int HOUR = 60 * MINUTE; 
const int DAY = 24 * HOUR; 
const int MONTH = 30 * DAY; 

if (delta < 1 * MINUTE) 
{ 
    return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago"; 
} 
if (delta < 2 * MINUTE) 
{ 
    return "a minute ago"; 
} 
if (delta < 45 * MINUTE) 
{ 
    return ts.Minutes + " minutes ago"; 
} 
if (delta < 90 * MINUTE) 
{ 
    return "an hour ago"; 
} 
if (delta < 24 * HOUR) 
{ 
    return ts.Hours + " hours ago"; 
} 
if (delta < 48 * HOUR) 
{ 
    return "yesterday"; 
} 
if (delta < 30 * DAY) 
{ 
    return ts.Days + " days ago"; 
} 
if (delta < 12 * MONTH) 
{ 
    int months = Convert.ToInt32(Math.Floor((double)ts.Days/30)); 
    return months <= 1 ? "one month ago" : months + " months ago"; 
} 
else 
{ 
    int years = Convert.ToInt32(Math.Floor((double)ts.Days/365)); 
    return years <= 1 ? "one year ago" : years + " years ago"; 
} 

risposta

23

Le date sono rappresentate in Cocoa utilizzando la classe NSDate. È disponibile un metodo conveniente in NSDate per ottenere il delta in secondi tra due istanze di data, timeIntervalSinceDate:. Questo viene chiamato su un'istanza NSDate, prendendo un altro oggetto NSDate come argomento. Restituisce un NSTimeInterval (che è un typedef per un doppio), che è rappresentativo del numero di secondi tra le due date.

Dato questo, sarebbe abbastanza semplice adattare il codice che hai dato sopra ad un contesto Objective-C/Cocoa. Dal momento che il delta calcolato NSDate in secondi, dato due date, si potrebbe facilmente adattare il codice di cui sopra:

//Constants 
#define SECOND 1 
#define MINUTE (60 * SECOND) 
#define HOUR (60 * MINUTE) 
#define DAY (24 * HOUR) 
#define MONTH (30 * DAY) 

- (NSString*)timeIntervalWithStartDate:(NSDate*)d1 withEndDate:(NSDate*)d2 
{ 
    //Calculate the delta in seconds between the two dates 
    NSTimeInterval delta = [d2 timeIntervalSinceDate:d1]; 

    if (delta < 1 * MINUTE) 
    { 
     return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%d seconds ago", (int)delta]; 
    } 
    if (delta < 2 * MINUTE) 
    { 
     return @"a minute ago"; 
    } 
    if (delta < 45 * MINUTE) 
    { 
     int minutes = floor((double)delta/MINUTE); 
     return [NSString stringWithFormat:@"%d minutes ago", minutes]; 
    } 
    if (delta < 90 * MINUTE) 
    { 
     return @"an hour ago"; 
    } 
    if (delta < 24 * HOUR) 
    { 
     int hours = floor((double)delta/HOUR); 
     return [NSString stringWithFormat:@"%d hours ago", hours]; 
    } 
    if (delta < 48 * HOUR) 
    { 
     return @"yesterday"; 
    } 
    if (delta < 30 * DAY) 
    { 
     int days = floor((double)delta/DAY); 
     return [NSString stringWithFormat:@"%d days ago", days]; 
    } 
    if (delta < 12 * MONTH) 
    { 
     int months = floor((double)delta/MONTH); 
     return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%d months ago", months]; 
    } 
    else 
    { 
     int years = floor((double)delta/MONTH/12.0); 
     return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%d years ago", years]; 
    } 
} 

Questo sarebbe poi chiamato, passando l'inizio e la fine NSDate oggetti come argomenti, e dovrebbe restituire un NSString con l'intervallo di tempo.

+0

Questo funziona a meraviglia. Grazie! –

+3

L'unico problema con questa implementazione è che non fa distinzione tra 24 ore come un giorno e un giorno di calendario. Ad esempio, se sto confrontando le 23:00 e le 2:00, la differenza dovrebbe essere "ieri" non "3 ore fa" Cercare in NSCalendar e nella sua classe NSDateComponents associata. – retainCount

1

È possibile ottenere il delta tra due NSDate oggetti utilizzando il metodo timeIntervalSinceDate:. Questo ti darà il delta in pochi secondi.

Da questo è possibile calcolare minuti/ore/giorni/mesi/anni dividendo per l'importo appropriato.

1

In alternativa, è possibile evitare l'errore incline aritmetica calendario facendo affidamento sulle componenti di calendario si può tirare dalla differenza tra due date :

NSDate *nowDate = [[NSDate alloc] init]; 
NSDate *targetDate = nil; // some other date here of your choosing, obviously nil isn't going to get you very far 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSUInteger unitFlags = NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; 
NSDateComponents *components = [gregorian components:unitFlags 
              fromDate:dateTime 
               toDate:nowDate options:0]; 
NSInteger months = [components month]; 
NSInteger weeks = [components week]; 
NSInteger days = [components day]; 
NSInteger hours = [components hour]; 
NSInteger minutes = [components minute]; 

la chiave è la messa a punto delle bandiere unità - questo permette di impostare quali unità di tempo si desidera che la data/ora per essere suddiviso in. Se desideri solo ore, imposterai NSHourCalendarUnit e quel valore continuerà ad aumentare man mano che le date si sposteranno ulteriormente, perché non c'è un'unità più grande per iniziare l'incremento.

Una volta ottenuti i componenti, è possibile procedere con la logica prescelta, magari modificando il flusso condizionale di @ alex.

Questo è quello che ho buttato insieme:

if (months > 1) { 
    // Simple date/time 
    if (weeks >3) { 
     // Almost another month - fuzzy 
     months++; 
    } 
    return [NSString stringWithFormat:@"%ld months ago", (long)months]; 
} 
else if (months == 1) { 
    if (weeks > 3) { 
     months++; 
     // Almost 2 months 
     return [NSString stringWithFormat:@"%ld months ago", (long)months]; 
    } 
    // approx 1 month 
    return [NSString stringWithFormat:@"1 month ago"]; 
} 
// Weeks 
else if (weeks > 1) { 
    if (days > 6) { 
     // Almost another month - fuzzy 
     weeks++; 
    } 
    return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks]; 
} 
else if (weeks == 1 || 
     days > 6) { 
    if (days > 6) { 
     weeks++; 
     // Almost 2 weeks 
     return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks]; 
    } 
    return [NSString stringWithFormat:@"1 week ago"]; 
} 
// Days 
else if (days > 1) { 
    if (hours > 20) { 
     days++; 
    } 
    return [NSString stringWithFormat:@"%ld days ago", (long)days]; 
} 
else if (days == 1) { 
    if (hours > 20) { 
     days++; 
     return [NSString stringWithFormat:@"%ld days ago", (long)days]; 
    } 
    return [NSString stringWithFormat:@"1 day ago"]; 
} 
// Hours 
else if (hours > 1) { 
    if (minutes > 50) { 
     hours++; 
    } 
    return [NSString stringWithFormat:@"%ld hours ago", (long)hours]; 
} 
else if (hours == 1) { 
    if (minutes > 50) { 
     hours++; 
     return [NSString stringWithFormat:@"%ld hours ago", (long)hours]; 
    } 
    return [NSString stringWithFormat:@"1 hour ago"]; 
} 
// Minutes 
else if (minutes > 1) { 
    return [NSString stringWithFormat:@"%ld minutes ago", (long)minutes]; 
} 
else if (minutes == 1) { 
    return [NSString stringWithFormat:@"1 minute ago"]; 
} 
else if (minutes < 1) { 
    return [NSString stringWithFormat:@"Just now"]; 
}