0

Coldfusion で Java オブジェクトを使用しているため、コードが少しずれています。

次のような関数があります。

function getJODAOffset(sTimezone){
    local.oDateTimeZone = createObject('java','org.joda.time.DateTimeZone');
    local.oInstant = createObject('Java','org.joda.time.Instant');

    local.oFormatter = createObject("Java",'org.joda.time.format.DateTimeFormat');
    local.oFormatter = local.oFormatter.forPattern('ZZ');

    local.tTime = local.oDateTimeZone.forID(arguments.sTimezone).getStandardOffset(local.oInstant); //sTimezone = 'Europe/London';

    return local.oFormatter.withZone(local.oDateTimeZone.forID(arguments.sTimezone)).print(local.tTime);

}

これにより、「 +00:00 」を期待しているときに「 +01:00 」が出力されますが、その理由はわかりません。

4

1 に答える 1

5

わかりました、私は今それを手に入れたと思います。

まず、あなたのコードがどのように機能するのかまったくわかりませんgetStandardOffset(Instant)私が見る限り、方法はありませんgetStandardOffset(long)。を呼び出すことで修正できますが、getMillis()Coldfusion が何をしているのかはわかりません。

とにかく、ここで問題を再現できます:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        DateTimeZone zone = DateTimeZone.forID("Europe/London");
        Instant now = new Instant();
        long offset = zone.getStandardOffset(now.getMillis());
        System.out.println("Offset = " + offset);
        DateTimeFormatter format = DateTimeFormat.forPattern("ZZ")
                                                 .withZone(zone);
        System.out.println(format.print(offset));
    }
}

出力:

Offset = 0
+01:00

問題は、オフセットDateTimeFormatter.printに渡していることです。これは、「エポックからのミリ秒」の値であるインスタントを期待しています。したがって、次のものと同等に扱っています。

format.print(new Instant(0))

現在new Instant(0)は UTC 1970 年 1 月 1 日の午前 0 時を表していますが、ヨーロッパ/ロンドンのタイム ゾーンはその時点で正真正銘の +01:00 でした。これがオフセットです。つまり、これは Joda Time のバグではなく、使用方法のバグです。

1 つのオプションは、代わりに、見つけたオフセットに固定さDateTimeZoneれたを作成し、そのゾーンを使用して任意のインスタントをフォーマットすることです。

DateTimeZone fixedZone = DateTimeZone.forOffsetMillis(offset);
DateTimeFormatter format = DateTimeFormat.forPattern("ZZ")
                                         .withZone(fixedZone);
System.out.println(format.print(0L)); // Value won't affect offset
于 2012-11-08T14:23:38.010 に答える