2017-03-15 31 views
6

chcę przekonwertować czasu serwera UTC na czas lokalny i wiza versa. Oto mój kod ..Swift 3.0: Konwersja czasu serwera UTC na czas lokalny i wiza versa

var isTimeFromServer = true 
var time:String! 
var period:String! 
let timeString = "6:59 AM" //Current UTC time 

if isTimeFromServer { 

    let index = timeString.index(timeString.startIndex, offsetBy: 5) 
    let twelve = timeString.substring(to: index) 

    var dateString:String! 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "H:mm" 
    let date12 = dateFormatter.date(from: twelve)! 

    dateFormatter.dateFormat = "h:mm a" 
    let date22 = dateFormatter.string(from: date12) 

    //print(date22) 
    dateString = date22 
    //print("dateString=\(dateString)") 

    time = dateString.components(separatedBy: " ")[0] 
    period = dateString.components(separatedBy: " ")[1] 

} 
else { 
    time = timeString.components(separatedBy: " ")[0] 
    period = timeString.components(separatedBy: " ")[1] 
} 

var hour = Int(time.components(separatedBy: ":")[0]) 

hour = period == "AM" ? hour : hour! + 12 
let minute = Int(time.components(separatedBy: ":")[1]) 
let calender = NSCalendar.current 
var datecomponent = DateComponents() 
datecomponent.calendar = calender 
datecomponent.hour = hour 
datecomponent.minute = minute 

if !isTimeFromServer { 
    // local to UTC 
    datecomponent.timeZone = TimeZone.current 
} 
else { 
    datecomponent.timeZone = TimeZone(abbreviation: "UTC") 
} 

let date = datecomponent.date 
let dateFormatter = DateFormatter() 

if !isTimeFromServer { 
    dateFormatter.dateFormat = "H:mm" 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 
    dateFormatter.string(from: date!) 
} 
else { 
    //UTC to local 
    dateFormatter.dateFormat = "h:mm a" 
    dateFormatter.timeZone = TimeZone.current 
    dateFormatter.string(from: date!) 
} 

mam czasu lokalnego

o/t: "12:52"

Ale rzeczywisty czas lokalny i czas wyjścia jest różnica 23 minuty. Więc proszę pomóżcie rozwiązać ten problem. Z góry dziękuję.

Odpowiedz

19

Nie wiem co jest nie tak z kodem.
Wygląda jednak na zbyt wiele niepotrzebnych rzeczy, jak przy ustawianiu kalendarza, pobieranie pewnych elementów ze sznurka. Oto moja mała wersja funkcji UTCToLocal i localToUTC.
Ale do tego trzeba przekazać ciąg w określonym formacie. Ponieważ zdecydowanie rozpakowałem obiekty z datami. Możesz jednak użyć pewnych warunków ochronnych, aby zapobiec awariom aplikacji.

func localToUTC(date:String) -> String { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "h:mm a" 
    dateFormatter.calendar = NSCalendar.current 
    dateFormatter.timeZone = TimeZone.current 

    let dt = dateFormatter.date(from: date) 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 
    dateFormatter.dateFormat = "H:mm:ss" 

    return dateFormatter.string(from: dt!) 
} 

func UTCToLocal(date:String) -> String { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "H:mm:ss" 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 

    let dt = dateFormatter.date(from: date) 
    dateFormatter.timeZone = TimeZone.current 
    dateFormatter.dateFormat = "h:mm a" 

    return dateFormatter.string(from: dt!) 
} 

i wywołanie funkcji jak poniżej.

print(UTCToLocal(date: "13:07:00")) 
print(localToUTC(date: "06:40 PM")) 

Mam nadzieję, że to ci pomoże.
Szczęśliwe kodowanie!

+1

Dokładnie tego chcę .. Dzięki @Mrugesh. – NiravS

+0

Świetnie !!! Dzięki! – iDev750

+1

jedynym minusem jest ineffecieny. każda iteracja tej funkcji zajmuje około 0,23 milisekundy. dla porównania tysięcy dat będzie to nieefektywne – quemeful

1

Spróbuj go:

func convertUTCToLocal(timeString: String) -> String? { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "h:mm a" 

    dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") 
    let timeUTC = dateFormatter.date(from: timeString) 

    if timeUTC != nil { 
     dateFormatter.timeZone = NSTimeZone.local 

     let localTime = dateFormatter.string(from: timeUTC!) 
     return localTime 
    } 

    return nil 
} 



func convertLocalToUTC(localTime: String) -> String? { 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "h:mm a" 
    dateFormatter.timeZone = NSTimeZone.local 
    let timeLocal = dateFormatter.date(from: localTime) 

    if timeLocal != nil { 
     dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") 

     let timeUTC = dateFormatter.string(from: timeLocal!) 
     return timeUTC 
    } 
    return nil 
} 


    var isTimeFromServer = true 
    var time:String! 
    var period:String! 
    let timeString = "6:59 AM" //Current UTC time 


    if isTimeFromServer { 
     print(convertUTCToLocal(timeString: timeString)) 
    } else { 
     print(convertLocalToUTC(localTime: timeString)) 
    } 
+3

Niektóre * * wyjaśnienia byłyby użyteczne dla przyszłych czytelników: Co jest nie tak z kodem na pytanie? Co zmieniłeś i dlaczego? –

5

odpowiedź Mrugesh jest idealny, ale jeśli ktoś musiał użyć własnych formatów, albo w jakimś innym formacie, mam uogólnić tak można dać inny format lub same w obu parametrów.

func localToUTC(date:String, fromFormat: String, toFormat: String) -> String { 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = fromFormat 
    dateFormatter.calendar = NSCalendar.current 
    dateFormatter.timeZone = TimeZone.current 
    dateFormatter.date 
    let dt = (from: date) 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 
    dateFormatter.dateFormat = toFormat 

    return dateFormatter.string(from: dt!) 
} 

func UTCToLocal(date:String, fromFormat: String, toFormat: String) -> String { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = fromFormat 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 

    let dt = dateFormatter.date(from: date) 
    dateFormatter.timeZone = TimeZone.current 
    dateFormatter.dateFormat = toFormat 

return dateFormatter.string(from: dt!) 
} 

let localDateAsString = UTCToLocal(date: dateAsString!, fromFormat: "hh:mm a, dd MMM yyyy", toFormat: "hh:mm a, dd MMM yyyy") 

Możesz użyć tego jak wyżej. Mam nadzieję, że to pomoże.

+1

znalazłem to bardziej użyteczne ... –

+0

idealne rozwiązanie !!!! – iDev750

+0

let dt = dateFormatter.date (od: date) ,,, lol: D – IsPha