2016-08-29 12 views
0

関数の引数として渡された任意の時刻文字列に対応するUTC時刻文字列を取得する関数を記述しました。夏時間の取得と夏時間の処理

+(NSString *)getUTCTime:(NSString *)selectedTime 
{ 
    NSString *strFormat; 
    if ([self is24HourFormat:selectedTime]){ 
     strFormat = @"HH:mm"; 
    } 
    else{ 
     strFormat = @"hh:mm a"; 
    } 
    NSLocale *baseLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 

    NSDateFormatter *dateFormatterlocal = [[NSDateFormatter alloc] init]; 
    [dateFormatterlocal setTimeZone:[NSTimeZone systemTimeZone]]; 
    dateFormatterlocal.locale = baseLocale; 
    [dateFormatterlocal setDateFormat:strFormat]; 

    NSDate *selectedDate = [dateFormatterlocal dateFromString:selectedTime]; 

    NSDateFormatter *dateFormatterUTC = [[NSDateFormatter alloc] init]; 
    [dateFormatterUTC setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
    dateFormatterUTC.locale = baseLocale; 
    [dateFormatterUTC setDateFormat:strFormat]; 


    NSString *utcTimeString = [dateFormatterUTC stringFromDate:selectedDate]; 
    return utcTimeString; 
} 

インドのタイムゾーンムンバイでは問題ありません。しかし、私がロンドンにタイムゾーンを変更すると、私は関数の引数を渡しているのと同じ時間を返します。現在、ロンドンではUTC + 1です。 NSDateFormatterは夏時間を処理しますか?夏時間のために、以下に示すよう

+0

正確に何が必要ですか?あなたはutc形式で時間が欲しいですか? – Lion

+0

ユーザーが選択した時間に対応するUTC時間をサーバーに送信する必要があります。 –

+0

定義上、UTCにはタイムゾーンがありません。あなたはGMT + 1を意味すると思います。 – Avi

答えて

1

あなたはNSTimeZone方法を使用することができます:/ //どうかを教えてくれる日ライトセービングこの日のために存在する

  • daylightSavingTimeOffsetNSTimeZone Class Reference

    1. がisDaylightSavingTimeForDateを参照してください。 /夏時間のオフセット量を教えてくれます

    以下のコードを使用して、適切な日にオフセットを使用して節約

    var timeZone:NSTimeZone = NSTimeZone(name:"Europe/London")! 
    print(timeZone.daylightSavingTime) //this will return true if day light saving present 
    print(timeZone.daylightSavingTimeOffset/(60*60))//this will give you number of hours by which the day light saving offset should be 
    print("timezone: \(timeZone)")//will give you detail about timezone 
    
  • +0

    ロンドンタイムゾーンの場合、0.000のオフセットが返されます。しかし、今はロンドンのUTC + 1です。 –

    +0

    @VarunMehta私は**コメント**を更新し、それがうまく動作し、ヨーロッパ/ロンドンのために私に1つのオフセットを与える遊び場でテストしました。上記のコードとすべてのベストをチェックしてください。 –