iPhone/Objective -C - Ayuda con NSDatecomponents y problemas de NSCalendar con zonas horarias en el extranjero

StackOverflow https://stackoverflow.com/questions/5912647

Pregunta

Utilizo el siguiente método de utilidad para mostrar los segundos, minutos, horas, etc.

por ejemplo, hace 1 hora

Tengo un amigo en el extranjero que prueba nuestra aplicación para iPhone y cada vez que envían un elemento, el método siempre devuelve "hace 1 hora" porque Nueva Zelanda está 2 horas por delante y el servidor está alojado en Australia, que obviamente está 2 horas atrás.

Es solo hasta que nuestro servidor SQL se ponga al día; Cuando la fecha del artículo comienza a calcular correctamente.

¿Me pregunto cómo puedo manejar las diferencias de la zona horaria y modificar mi método de utilidad para que funcione a nivel mundial? No dude en ayudar a modificar el método para que no sean tantas líneas de código (si se siente realmente aburrido, y generoso).

Gracias por adelantado.

+ (NSString *)elapsedTime:(NSDate *)date {
    NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] components:desiredComponents
                                                                         fromDate:date
                                                                           toDate:[NSDate date]
                                                                          options:0];

    // Format to be used to generate string to display.
    NSString *scannedFormat = @"%d %@ ago";
    NSInteger number = 0;
    NSString *unit;

    if ([elapsedTimeUnits year] > 0) {
        number = [elapsedTimeUnits year];
        unit = [NSString stringWithFormat:@"year"];
    } else if ([elapsedTimeUnits month] > 0) {
        number = [elapsedTimeUnits month];
        unit = [NSString stringWithFormat:@"month"];
    } else if ([elapsedTimeUnits week] > 0) {
        number = [elapsedTimeUnits week];
        unit = [NSString stringWithFormat:@"week"];
    } else if ([elapsedTimeUnits day] > 0) {
        number = [elapsedTimeUnits day];
        unit = [NSString stringWithFormat:@"day"];
    } else if ([elapsedTimeUnits hour] > 0) {
        number = [elapsedTimeUnits hour];
        unit = [NSString stringWithFormat:@"hour"];
    } else if ([elapsedTimeUnits minute] > 0) {
        number = [elapsedTimeUnits minute];
        unit = [NSString stringWithFormat:@"minute"];
    } else if ([elapsedTimeUnits second] > 0) {
        number = [elapsedTimeUnits second];
        unit = [NSString stringWithFormat:@"second"];
    } else {
        number = 1;
        unit = @"second";
    }

    // Check if unit number is greater than 1, then append 's' at the end.
    if (number > 1) {
        unit = [NSString stringWithFormat:@"%@s", unit];
    }

    // Resultant string required.
    NSString *scannedString = [NSString stringWithFormat:scannedFormat, number, unit];

    return scannedString;
}

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top