0

SharePoint 2007 で、ドキュメントをドキュメント ライブラリに保存したとき、リスト ビューの [変更済み] の下に表示される値は次のとおりです。

18/6/2012 13:06

しかし、SPListItem.File.TimeLastModified であると想定しているフィールドにプログラムでアクセスすると、次のように返されました。

18/6/2012 3:06:43 AM

これは、1 が切り捨てられたことを意味するため、値を読み取っているものは何でも午後 1 時から午前 3 時になりました。

サイトと Web はすべて正しいタイム ゾーンを継承しています。TimeLastModified で正しい時刻を表示するにはどうすればよいですか? それとも、これはまったく可能ですか?

ありがとう。

4

1 に答える 1

1

プロパティはTimeLastModified常に UTC で値を返します。SP ページに表示される日付/時刻の値は、通常、現在のユーザーのカルチャに従ってタイム ゾーンに変換されます。すべてをユーザーのタイム ゾーンで表示し、内部的には値を UTC で保存することをお勧めします。

UTC 値を現在のユーザーのタイム ゾーンに変換し、それを UI に出力する場合は、次のコードを使用できます。

SPFile file = ...;
SPWeb web = ...; // SPContext.Current.Web or file.Item.ParentList.ParentWeb or ...
DateTime time = UTCToWebTime(file.TimeLastModified, web);
string text = FormatWebTime(time, web);

DateTime UTCToWebTime(DateTime utcTime, SPWeb web) {
    SPTimeZone timeZone = web.RegionalSettings.TimeZone;
    DateTime localTime = timeZone.UTCToLocalTime(utcTime);
    return DateTime.SpecifyKind(localTime, DateTimeKind.Local);
}

// Uses SPRegionalSettings to be more accurate then value.ToString(web.Locale).
string FormatWebTime(DateTime value, SPWeb web) {
    SPRegionalSettings regionalSettings = web.RegionalSettings;
    DateOptions dateOptions = new DateOptions(
        regionalSettings.LocaleId.ToString(CultureInfo.InvariantCulture),
        (SPCalendarType) regionalSettings.CalendarType, null,
        regionalSettings.FirstDayOfWeek.ToString(CultureInfo.InvariantCulture),
        regionalSettings.AdjustHijriDays.ToString(CultureInfo.InvariantCulture),
        null, null));
    string timePattern = regionalSettings.Time24 ?
        dateOptions.TimePattern24Hour : dateOptions.TimePattern12Hour;
    DateTimeFormatInfo format = web.Locale.DateTimeFormat;
    return value.ToString(format.ShortDatePattern, format) + " " +
        value.ToString(timePattern, format);
}

--- フェルダ

于 2012-06-18T20:03:44.430 に答える