2009年05月30日

日本の日時の取得

日本の日時を取得したいのですが、
llGetTimestamp()なんかでも時差をプラスしても時間だけで日時はかわらないので
うまく日にちを取得する方法はないでしょうか。アドバイスよろしくおねがいします。


同じカテゴリー(スクリプト関連)の記事画像
オブジェクトのリンクとタッチで開く扉
自動チャットを止めたい
テレポートするとアニメーションが解除される
同じカテゴリー(スクリプト関連)の記事
 テクスチェンジのHUDについて (2015-07-01 04:51)
 モール用のプリムカウンターを探しています (2014-11-29 20:09)
 連続するアニメーションの処理 (2014-08-24 03:23)
 オブジェクトの上にフローティングテキストを出す。 (2014-08-22 16:46)
 チャットログの盗聴について (2014-02-03 03:43)
 椅子に座った後で位置調整できるスクリプト (2013-03-24 16:37)

Posted by sora8  at 17:33 │Comments(1)スクリプト関連

この記事へのコメント
エクセルとかでは作りつけの関数で簡単に処理してしまうので、
いざ真面目に計算しようと思うと苦労してしまいますよね。
探してみたところLSLWikiにPST時間を得る関数の例がありましたので、
これを改変して日本時間用にしてみました。
参考:
http://lslwiki.net/lslwiki/wakka.php?wakka=TextSearch&phrase=llGetTimestamp

以下のスクリプトをオブジェクトに仕込み、タッチすると
その時の現在時間を日本時間で発言すると思います。

string timestamp2JST(string timestamp) {
// Set up constants required for the function (could be made global)
list weekday_names = [ "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ];
list days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Parse the timestamp
list parts = llParseString2List(timestamp, ["-", "T", ":", ".", "Z"], []);

// If it's not a timestamp, then just return the original string
if (llGetListLength(parts) != 7) {
return timestamp;
} else {
// Set up the local variables we need
integer year = llList2Integer(parts, 0);
integer month = llList2Integer(parts, 1);
integer day = llList2Integer(parts, 2);
integer hour = llList2Integer(parts, 3);
integer minute = llList2Integer(parts, 4);
integer second = llList2Integer(parts, 5);

// GMT -> JST (time difference = 9 hr)
hour += 9; //
// Deal with day/month/year boundaries crossed by the adjustment for the time difference
if (hour < 0) {
day--;
hour += 24;
if (day <= 0) {
month--;
if (month <= 0) {
year--;
month += 12;
}

day = llList2Integer(days_in_month, month - 1);

// Do a leap year adjustment if required
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
day++;
}
}
}

// Determine the day of the week
// This highly non-intuitive code is based on an algorithm
// from http://mathforum.org/library/drmath/view/55837.html
if (month < 3) {
month += 12;
--year;
}
integer weekday_number = (day + 2 * month + (3 * (month + 1)) / 5
+ year + year / 4 - year / 100 + year / 400 + 2) % 7;

if (month > 12) {
month -= 12;
++year;
}

// Build the PST date string (fixed format)
string date = llGetSubString(llList2String(weekday_names, weekday_number), 0, 2);
date += " " + (string)year + "/" + (string)month + "/" + (string)day;

// Convert the hour to am/pm
string suffix = " pm";
if (hour < 12) {
suffix = " am";
if (hour == 0) {
hour = 12;
}
} else if (hour > 12) {
hour %= 12;
}

date += " " + (string)hour + ":";
if (minute < 10) {
date += "0" + (string)minute;
} else {
date += (string)minute;
}
date += suffix;
return date;
}
}
default
{
touch_start(integer total_number)
{
llSay(0, "Touched: "+timestamp2JST(llGetTimestamp()));
}
}
Posted by YooYoo at 2009年05月30日 19:51
<ご注意>
書き込まれた内容は公開され、ブログの持ち主だけが削除できます。