【问题标题】:Configuring Mediawiki UTCLiveClock Gadget配置 Mediawiki UTCLiveClock 小工具
【发布时间】:2016-10-25 01:37:07
【问题描述】:

我正在使用 MediaWiki 的 UTCLiveClock.js,我正在尝试将其配置为在 PST/PDT 中显示时间。我以为我解决了这个问题,但是当午夜 UTC 到达 (00:00:00) 时,它将我的时间输出更改为 0-7:00:00。我需要正确显示,以便当 UTC 00:00:00 - 07:00:00 发生时,我的时间显示看起来不会一团糟。我通过 mediawiki 使用的小工具可以在 https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js

找到

是的,我是菜鸟。我没有任何正式的编程知识。我只是想为服务器存在于 PST/PDT 中并遇到此问题的游戏构建一个 wiki。谷歌搜索过去 3 个小时的关键字搜索让我无处可去。请帮忙。

    */
/*global mw, $, UTCLiveClockConfig:true */
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.notify']).then( function () {
var $target;

function showTime( $target ) {
    var now = new Date();
    var hh = now.getUTCHours();
    var mm = now.getUTCMinutes();
    var ss = now.getUTCSeconds();
    var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss );
    $target.text( time );

    var ms = now.getUTCMilliseconds();

    setTimeout( function () {
        showTime( $target );
    }, 1100 - ms );
}

function liveClock() {
    mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );

    if ( !window.UTCLiveClockConfig ) {
        UTCLiveClockConfig = {};
    }
    var portletId = UTCLiveClockConfig.portletId || 'p-personal';
    var nextNode = UTCLiveClockConfig.nextNodeId ? document.getElementById( UTCLiveClockConfig.nextNodeId ) : undefined;
    var node = mw.util.addPortletLink(
        portletId,
        mw.util.getUrl( null, { action: 'purge' } ),
        '',
        'utcdate',
        null,
        null,
        nextNode
    );
    if ( !node ) {
        return;
    }
    $( node ).on( 'click', function ( e ) {
        new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
            location.reload();
        }, function () {
            mw.notify( 'Purge failed', { type: 'error' } );
        } );
        e.preventDefault();
    } );

    showTime( $( node ).find( 'a:first' ) );
}

$( liveClock );
} );

编辑:

我最初解决问题的方法是在小时部分加上 -7:

var hh = now.getUTCHours()-7;

【问题讨论】:

  • 这个“小工具”应该有更好的链接。你从哪里得到它?
  • 直接来自 Mediawiki mediawiki.org/wiki/Extension:Gadgets 我的问题是绝对没有关于如何编辑 .js 以使其适合另一个时区的教程。我什么都找不到。

标签: javascript mediawiki


【解决方案1】:

看起来这是一个非常简单的小工具,仅限于 UTC 时间。选项很少。

  1. 找到更好的支持时区的脚本。
  2. 在此基础上编写自己的脚本。

编写自己的脚本,有一个关键部分。

function get_PST_PDT_offset() {
    /* You have to rewrite this function from time to time. */
    var now = new Date();
    var change = new Date(2016, 11, 5, 19, 0); /* 2016-11-06 2:00 PST */
    if (now >= change) {
        return -8;
    } else {
        return -7;
    }
}

function showTime( $target ) {
    var now = new Date();
    var hh = now.getUTCHours();
    var offset = get_PST_PDT_offset();
    hh = (hh + offset + 24) % 24;
    var mm = now.getUTCMinutes();
    /* ... */

【讨论】:

  • 谢谢你,你说得对,这看起来很简单,但我通常会把我一直在使用的所有东西都放在一起......我自己想出 .js 代码并不是我想要的有能力...所以我放弃了查看手册和教程,只是决定问。
  • 这个答案也很有效,我支持你,但我的代表太低了,无法公开计算任何东西,但我仍然最深切的感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多