2009-08-11 7 views
15

provo a convertire un valore come "898.171813964844" in 00:17:02 (hh: mm: ss).Converti NSNumber (doppio) valore nel tempo

Come può essere fatto nell'obiettivo c?

Grazie per l'aiuto!

+0

È possibile utilizzare semplici operazioni aritmetiche, ma non vedo come 898.171813964844 sarebbe 00:17:02. A cosa si riferisce il galleggiante? – Xetius

+0

Ehi, in ruby ​​faccio semplicemente Time.at (time - 3600) .strftime ("% H:% M:% S") e ottieni il risultato corretto. (il tempo è il valore float) – phx

risposta

30

soluzione finale:

NSNumber *time = [NSNumber numberWithDouble:([online_time doubleValue] - 3600)]; 
NSTimeInterval interval = [time doubleValue];  
NSDate *online = [NSDate date]; 
online = [NSDate dateWithTimeIntervalSince1970:interval];  
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"HH:mm:ss"]; 

NSLog(@"result: %@", [dateFormatter stringFromDate:online]); 
+0

Perché ottengo qualcosa come 8 ore 1 minuto e 34 secondi quando ho il valore 94.000 nel mio intervallo? Sto facendo qualcosa di sbagliato? – Ben

+0

@ Ben: è probabile perché il fuso orario è di +8 ore. Prova a impostare il fuso orario su GMT '[dateFormatter setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: 0]];' –

+0

Perché -3600 da tempo? –

1
  1. Convertire il vostro valore NSNumber ad un NSTimeInterval con -doubleValue
  2. Convertire il vostro valore NSTimeInterval ad un NSDate con +dateWithTimeIntervalSinceNow:
  3. Convertire tua NSDate ad un NSString con -descriptionWithCalendarFormat:timeZone:locale:
+0

Intervallo NSTimeInterval = [time doubleValue]; NSDate * date = [data NSDate]; date = [NSDate dateWithTimeIntervalSinceNow: interval]; NSString * value = [data descriptionWithCalendarFormat: @ "% I:% M:% S" timeZone: [NSTimeZone localTimeZone] locale: nil]; piace questo? Ma ottengo: avviso NSDate potrebbe non rispondere a -descriptionWithCalendarFormat .... – phx

+0

Il modo non deprecato (o modo iPhone SDK) di convertire un NSDate in una stringa consiste nell'utilizzare un NSDateFormatter. – Wevah

+0

Inoltre, stai provando a convertire il numero in un tempo assoluto (vale a dire, un timestamp?) O unità (ad esempio, il numero è solo un numero di secondi?)? – Wevah

8

Dando per scontato che sono solo interessati a ore, minuti e secondi e che il valore di input è inferiore o uguale a 86400 si potrebbe fare qualcosa del genere:

NSNumber *theDouble = [NSNumber numberWithDouble:898.171813964844]; 

int inputSeconds = [theDouble intValue]; 
int hours = inputSeconds/3600; 
int minutes = (inputSeconds - hours * 3600)/60; 
int seconds = inputSeconds - hours * 3600 - minutes * 60; 

NSString *theTime = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds]; 
3

So che la risposta è già stata accettata, ma qui è la mia risposta utilizzando NSDateFormatter e tenendo conto fuso orario (alle ore di fuso orario [ad es. GMT + 4] essendo inaspettatamente aggiunto @ Ben)

NSTimeInterval intervalValue = 898.171813964844; 
    NSDateFormatter *hmsFormatter = [[NSDateFormatter alloc] init]; 
    [hmsFormatter setDateFormat:@"HH:mm:ss"]; 
    [hmsFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    NSLog(@"formatted date: %@", [hmsFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:intervalValue]]); 

[nota laterale] @phx: supponendo 898,171813964844 è in secondi, ciò rappresenterebbe 00:14:58 non 00:17:02.