web-dev-qa-db-fra.com

iPhone: convertir une chaîne de date en un horodatage relatif

J'ai un timestamp comme une chaîne comme:

Jeu., 21 mai 09 19:10:09 -0700

et j'aimerais le convertir en un horodatage relatif du type «il y a 20 minutes» ou «il y a 3 jours».

Quelle est la meilleure façon de faire cela en utilisant Objective-C pour iPhone?

39
Gilean
-(NSString *)dateDiff:(NSString *)origDate {
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setFormatterBehavior:NSDateFormatterBehavior10_4];
    [df setDateFormat:@"EEE, dd MMM yy HH:mm:ss VVVV"];
    NSDate *convertedDate = [df dateFromString:origDate];
    [df release];
    NSDate *todayDate = [NSDate date];
    double ti = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1) {
        return @"never";
    } else  if (ti < 60) {
        return @"less than a minute ago";
    } else if (ti < 3600) {
        int diff = round(ti / 60);
        return [NSString stringWithFormat:@"%d minutes ago", diff];
    } else if (ti < 86400) {
        int diff = round(ti / 60 / 60);
        return[NSString stringWithFormat:@"%d hours ago", diff];
    } else if (ti < 2629743) {
        int diff = round(ti / 60 / 60 / 24);
        return[NSString stringWithFormat:@"%d days ago", diff];
    } else {
        return @"never";
    }   
}
72
Gilean

Voici des méthodes de Cocoa pour vous aider à obtenir des informations pertinentes (ne savez pas si elles sont toutes disponibles dans Coca-Touch).

    NSDate * today = [NSDate date];
    NSLog(@"today: %@", today);

    NSString * str = @"Thu, 21 May 09 19:10:09 -0700";
    NSDate * past = [NSDate dateWithNaturalLanguageString:str
                            locale:[[NSUserDefaults 
                            standardUserDefaults] dictionaryRepresentation]];

    NSLog(@"str: %@", str);
    NSLog(@"past: %@", past);

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | 
                             NSDayCalendarUnit | 
                             NSHourCalendarUnit | NSMinuteCalendarUnit | 
                             NSSecondCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:past
                                                  toDate:today
                                                 options:0];

    NSLog(@"months: %d", [components month]);
    NSLog(@"days: %d", [components day]);
    NSLog(@"hours: %d", [components hour]);
    NSLog(@"seconds: %d", [components second]);

L'objet NSDateComponents semble contenir la différence en unités pertinentes (comme spécifié) . Si vous spécifiez toutes les unités, vous pouvez alors utiliser cette méthode:

void dump(NSDateComponents * t)
{
    if ([t year]) NSLog(@"%d years ago", [t year]);
    else if ([t month]) NSLog(@"%d months ago", [t month]);
    else if ([t day]) NSLog(@"%d days ago", [t day]);
    else if ([t minute]) NSLog(@"%d minutes ago", [t minute]);
    else if ([t second]) NSLog(@"%d seconds ago", [t second]);
}

Si vous voulez calculer vous-même, vous pouvez regarder:

NSDate timeIntervalSinceDate

Et ensuite, utilisez les secondes dans l'algorithme.

Avertissement : Si cette interface devient obsolète (je ne l'ai pas cochée), la méthode préférée d'Apple pour ce faire via NSDateFormatters, comme suggéré dans les commentaires ci-dessous, a également l'air plutôt chouette. , il peut encore être utile pour certains de regarder la logique utilisée.

22
stefanB

Je ne peux pas encore éditer, mais j'ai pris le code de Gilean et apporté quelques modifications pour en faire une catégorie de NSDateFormatter.

Il accepte une chaîne de format afin de fonctionner avec des chaînes arbitraires et j’ai ajouté les clauses si les événements singuliers devaient être grammaticalement corrects.

À votre santé,

Carl C-M

@interface NSDateFormatter (Extras)
+ (NSString *)dateDifferenceStringFromString:(NSString *)dateString
                                  withFormat:(NSString *)dateFormat;

@end

@implementation NSDateFormatter (Extras)

+ (NSString *)dateDifferenceStringFromString:(NSString *)dateString
                                  withFormat:(NSString *)dateFormat
{
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  [dateFormatter setDateFormat:dateFormat];
  NSDate *date = [dateFormatter dateFromString:dateString];
  [dateFormatter release];
  NSDate *now = [NSDate date];
  double time = [date timeIntervalSinceDate:now];
  time *= -1;
  if(time < 1) {
    return dateString;
  } else if (time < 60) {
    return @"less than a minute ago";
  } else if (time < 3600) {
    int diff = round(time / 60);
    if (diff == 1) 
      return [NSString stringWithFormat:@"1 minute ago", diff];
    return [NSString stringWithFormat:@"%d minutes ago", diff];
  } else if (time < 86400) {
    int diff = round(time / 60 / 60);
    if (diff == 1)
      return [NSString stringWithFormat:@"1 hour ago", diff];
    return [NSString stringWithFormat:@"%d hours ago", diff];
  } else if (time < 604800) {
    int diff = round(time / 60 / 60 / 24);
    if (diff == 1) 
      return [NSString stringWithFormat:@"yesterday", diff];
    if (diff == 7) 
      return [NSString stringWithFormat:@"last week", diff];
    return[NSString stringWithFormat:@"%d days ago", diff];
  } else {
    int diff = round(time / 60 / 60 / 24 / 7);
    if (diff == 1)
      return [NSString stringWithFormat:@"last week", diff];
    return [NSString stringWithFormat:@"%d weeks ago", diff];
  }   
}

@end
14

Dans l'intérêt de l'exhaustivité, basée sur la réponse de @ Gilean, voici le code complet d'une catégorie simple sur NSDate qui imite les astucieux assistants de Rails. Pour un rafraîchissement sur les catégories, ce sont des méthodes d'instance que vous utiliseriez avec des objets NSDate. Donc, si j’ai un NSDate qui représente hier, [myDate distanceOfTimeInWordsToNow] => "1 jour".

J'espère que c'est utile!

@interface NSDate (NSDate_Relativity)

-(NSString *)distanceOfTimeInWordsSinceDate:(NSDate *)aDate;
-(NSString *)distanceOfTimeInWordsToNow;

@end



@implementation NSDate (NSDate_Relativity)


-(NSString *)distanceOfTimeInWordsToNow {
    return [self distanceOfTimeInWordsSinceDate:[NSDate date]];

}

-(NSString *)distanceOfTimeInWordsSinceDate:(NSDate *)aDate {
    double interval = [self timeIntervalSinceDate:aDate];

    NSString *timeUnit;
    int timeValue;

    if (interval < 0) {
        interval = interval * -1;        
    }

    if (interval< 60) {
        return @"seconds";

    } else if (interval< 3600) { // minutes

        timeValue = round(interval / 60);

        if (timeValue == 1) {
            timeUnit = @"minute";

        } else {
            timeUnit = @"minutes";

        }


    } else if (interval< 86400) {
        timeValue = round(interval / 60 / 60);

        if (timeValue == 1) {
            timeUnit = @"hour";

        } else {
            timeUnit = @"hours";
        }


    } else if (interval< 2629743) {
        int days = round(interval / 60 / 60 / 24);

        if (days < 7) {

            timeValue = days;

            if (timeValue == 1) {
                timeUnit = @"day";
            } else {
                timeUnit = @"days";
            }

        } else if (days < 30) {
            int weeks = days / 7;

            timeValue = weeks;

            if (timeValue == 1) {
                timeUnit = @"week";
            } else {
                timeUnit = @"weeks";
            }


        } else if (days < 365) {

            int months = days / 30;
            timeValue = months;

            if (timeValue == 1) {
                timeUnit = @"month";
            } else {
                timeUnit = @"months";
            }

        } else if (days < 30000) { // this is roughly 82 years. After that, we'll say 'forever'
            int years = days / 365;
            timeValue = years;

            if (timeValue == 1) {
                timeUnit = @"year";
            } else {
                timeUnit = @"years";
            }

        } else {
            return @"forever ago";
        }
    }

    return [NSString stringWithFormat:@"%d %@", timeValue, timeUnit];

}

@end
8
Chris Ladd

Il y a déjà beaucoup de réponses qui viennent à la même solution mais il ne peut pas nuire d'avoir des choix. Voici ce que je suis venu avec.

- (NSString *)stringForTimeIntervalSinceCreated:(NSDate *)dateTime
{
    NSDictionary *timeScale = @{@"second":@1,
                                @"minute":@60,
                                @"hour":@3600,
                                @"day":@86400,
                                @"week":@605800,
                                @"month":@2629743,
                                @"year":@31556926};
    NSString *scale;
    int timeAgo = 0-(int)[dateTime timeIntervalSinceNow];
    if (timeAgo < 60) {
        scale = @"second";
    } else if (timeAgo < 3600) {
        scale = @"minute";
    } else if (timeAgo < 86400) {
        scale = @"hour";
    } else if (timeAgo < 605800) {
        scale = @"day";
    } else if (timeAgo < 2629743) {
        scale = @"week";
    } else if (timeAgo < 31556926) {
        scale = @"month";
    } else {
        scale = @"year";
    }

    timeAgo = timeAgo/[[timeScale objectForKey:scale] integerValue];
    NSString *s = @"";
    if (timeAgo > 1) {
        s = @"s";
    } 
    return [NSString stringWithFormat:@"%d %@%@ ago", timeAgo, scale, s];
}
6
Dean Kelly

J'ai pris le code de Carl Coryell-Martin et créé une catégorie NSDate plus simple qui ne contient pas d'avertissements sur le formatage des singuliers, et je range aussi singulier la semaine dernière:

@interface NSDate (Extras)
- (NSString *)differenceString;
@end

@implementation NSDate (Extras)

- (NSString *)differenceString{
    NSDate* date = self;
    NSDate *now = [NSDate date];
    double time = [date timeIntervalSinceDate:now];
    time *= -1;
    if (time < 60) {
        int diff = round(time);
        if (diff == 1)
            return @"1 second ago";
        return [NSString stringWithFormat:@"%d seconds ago", diff];
    } else if (time < 3600) {
        int diff = round(time / 60);
        if (diff == 1)
            return @"1 minute ago";
        return [NSString stringWithFormat:@"%d minutes ago", diff];
    } else if (time < 86400) {
        int diff = round(time / 60 / 60);
        if (diff == 1)
            return @"1 hour ago";
        return [NSString stringWithFormat:@"%d hours ago", diff];
    } else if (time < 604800) {
        int diff = round(time / 60 / 60 / 24);
        if (diff == 1)
            return @"yesterday";
        if (diff == 7)
            return @"a week ago";
        return[NSString stringWithFormat:@"%d days ago", diff];
    } else {
        int diff = round(time / 60 / 60 / 24 / 7);
        if (diff == 1)
            return @"a week ago";
        return [NSString stringWithFormat:@"%d weeks ago", diff];
    }   
}

@end
4
malhal

En rapide

Usage:

let time = NSDate(timeIntervalSince1970: timestamp).timeIntervalSinceNow
let relativeTimeString = NSDate.relativeTimeInString(time)
println(relativeTimeString)

Extension:

extension NSDate {
    class func relativeTimeInString(value: NSTimeInterval) -> String {
        func getTimeData(value: NSTimeInterval) -> (count: Int, suffix: String) {
            let count = Int(floor(value))
            let suffix = count != 1 ? "s" : ""
            return (count: count, suffix: suffix)
        }

        let value = -value
        switch value {
            case 0...15: return "just now"

            case 0..<60:
                let timeData = getTimeData(value)
                return "\(timeData.count) second\(timeData.suffix) ago"

            case 0..<3600:
                let timeData = getTimeData(value/60)
                return "\(timeData.count) minute\(timeData.suffix) ago"

            case 0..<86400:
                let timeData = getTimeData(value/3600)
                return "\(timeData.count) hour\(timeData.suffix) ago"

            case 0..<604800:
                let timeData = getTimeData(value/86400)
                return "\(timeData.count) day\(timeData.suffix) ago"

            default:
                let timeData = getTimeData(value/604800)
                return "\(timeData.count) week\(timeData.suffix) ago"
        }
    }
}
3
dimpiax

Utilisez la classe NSDate:

timeIntervalSinceDate

renvoie l'intervalle en secondes.

Exercice rapide pour implémenter ceci dans objectif-c:

  1. Obtenez le temps "maintenant" NSDate
  2. Obtenez le NSDate avec lequel vous souhaitez comparer
  3. Obtenir l'intervalle en secondes à l'aide de timeIntervalSinceDate

Puis implémentez ce pseudo-code:

if (x < 60) // x seconds ago

else if( x/60 < 60) // floor(x/60) minutes ago

else if (x/(60*60) < 24) // floor(x/(60*60) hours ago

else if (x/(24*60*60) < 7) // floor(x(24*60*60) days ago

etc...

alors vous devez décider si un mois est 30,31 ou 28 jours. Keep it simple - choisissez 30.

Il y a peut-être un meilleur moyen, mais il est 2 heures du matin et c'est la première chose qui m'est venue à l'esprit.

1
Nael El Shawwa

Ma solution:

- (NSString *) dateToName:(NSDate*)dt withSec:(BOOL)sec {

    NSLocale *locale = [NSLocale currentLocale];
    NSTimeInterval tI = [[NSDate date] timeIntervalSinceDate:dt];
    if (tI < 60) {
      if (sec == NO) {
           return NSLocalizedString(@"Just Now", @"");
       }
       return [NSString stringWithFormat:
                 NSLocalizedString(@"%d seconds ago", @""),(int)tI];
     }
     if (tI < 3600) {
       return [NSString stringWithFormat:
                 NSLocalizedString(@"%d minutes ago", @""),(int)(tI/60)];
     }
     if (tI < 86400) {
      return [NSString stringWithFormat:
                 NSLocalizedString(@"%d hours ago", @""),(int)tI/3600];
     }

     NSDateFormatter *relativeDateFormatter = [[NSDateFormatter alloc] init];
     [relativeDateFormatter setTimeStyle:NSDateFormatterNoStyle];
     [relativeDateFormatter setDateStyle:NSDateFormatterMediumStyle];
     [relativeDateFormatter setDoesRelativeDateFormatting:YES];
     [relativeDateFormatter setLocale:locale];

     NSString * relativeFormattedString = 
            [relativeDateFormatter stringForObjectValue:dt];
     return relativeFormattedString;
}
1
ppaulojr

J'ai vu qu'il y avait plusieurs fois des fonctions dans des extraits de code sur Stack Overflow et j'en voulais une qui donne vraiment le sens le plus clair du temps (depuis que quelque chose s'est passé). Pour moi, cela signifie style "il y a le temps" pour des intervalles de temps courts (il y a 5 minutes, il y a 2 heures) et des dates spécifiques pour des périodes plus longues (15 avril 2011 au lieu de 2 ans auparavant). Fondamentalement, je pensais que Facebook avait fait un très bon travail dans ce domaine et je voulais simplement suivre leur exemple (car je suis sûr qu'ils réfléchissaient beaucoup à cela et qu'il était très facile et clair de comprendre du point de vue du consommateur).

Après une longue période de recherches sur Google, j'ai été assez surpris de constater que personne ne l'avait implémenté autant que je sache. Décidé que je le voulais assez pour passer le temps à écrire et pensais que je partagerais. 

Espérons que vous apprécierez :)

Obtenez le code ici: https://github.com/nikilster/NSDate-Time-Ago

0
N V

Je ne suis pas sûr de savoir pourquoi cela n’est pas dans cacao-touch, c’est bien.

Configurez certains types pour conserver les données, cela facilitera la tâche si vous avez besoin de les localiser un peu plus. (évidemment augmentez si vous avez besoin de plus de temps)

typedef struct DayHours {
    int Days;
    double Hours;
} DayHours;


+ (DayHours) getHourBasedTimeInterval:(double) hourBased withHoursPerDay:(double) hpd
{
    int NumberOfDays = (int)(fabs(hourBased) / hpd);
    float hoursegment = fabs(hourBased) - (NumberOfDays * hpd);
    DayHours dh;
    dh.Days = NumberOfDays;
    dh.Hours = hoursegment;
    return dh;
}

REMARQUE: J'utilise un calcul basé sur l'heure, car c'est là que sont mes données. NSTimeInterval est basé sur la seconde. J'ai également dû convertir entre les deux.

0
Bluephlame