时区转换器
注意
此页为于 SCP 维基内部使用的“组件”页。用于在其他页面中引用。
未经组件作者或工作人员允许,请勿修改此页的内容。
这是什么
一个可以显示读者所在时区的给定时间和/或日期的组件。
读者的时区将被自动计算。你所需要作的就是将你所需时间的时区输入到页面之中。
如果你的时区能够匹配读者的时区,那么该组件将不会出现。这是故意的!如果读者需要的话它还是会出现的。
请注意以下夏令时制为例外:当“EST”1与“EDT”2被输入到该组件中时,其将会转换为美国/纽约时区,然后再根据一年中的时间转换为 EST 或者 EDT。“BST”3将会转换为欧洲/伦敦时区,然后再根据一年中的时间转换为 GMT4 或者 BST。为避免此种混乱,请避免使用非常依赖夏令时的时区!使用如 UTC5 这样的静态时区。
用法
将以下代码复制到你的页面中:
[[include :scp-wiki-cn:component:tz
| time=18:00
| tz=UTC
]]
以斜体所示的设置为可选项。其它所有设置均为必填。如果你省略了一个可选设置,那么它将会使用其默认值。如果你省略了一个非可选的设置,那么就不要期望组件能够正常运作了。
若需省略设置,仅需将其完全留空即可。
代码库
转换器的 HTML 结构
很不幸它被掩盖在源代码之中——你得自己去挖掘了!
用以运作转换器的 JS
document.addEventListener("DOMContentLoaded", function() { console.log(date); date = date === "{$date}" ? moment().format("Y-MM-DD") : date; console.log(date); tz = tz === "{$tz}" ? "UTC" : tz; tz = tz === "EST" ? "America/New_York" : tz; tz = tz === "EDT" ? "America/New_York" : tz; tz = tz === "BST" ? "Europe/London" : tz; given_time = moment.tz(date + " " + time, tz); tz = given_time.format("z"); text_before = text_before === "{$text_before}" ? "${time} ${tz} 即 " : text_before; text_after = text_after === "{$text_after}" ? "。" : text_after; // eval is used here to convert text strings to template literals // variables of the form ${var} will be parsed text_before = eval('`' + text_before + '`'); text_after = eval('`' + text_after + '`'); user_tz = Intl.DateTimeFormat().resolvedOptions().timeZone; converted_time = given_time.tz(user_tz); converted_time_tz = converted_time.format("HH:mm z"); console.log(given_time); console.log(converted_time); document.getElementById("text_before").textContent = text_before; document.getElementById("text_converted").textContent = converted_time_tz; document.getElementById("text_after").textContent = text_after; if (converted_time.format("z") === tz) { // e.g. displaying an EST time to an EST user - that's pointless! document.documentElement.style.display = 'none'; } });
用以修改输出外部样式的 CSS
.timezone-thingy { margin-top: -0.5rem; } .timezone-thingy iframe { border: none; max-height: 3.5rem; }
用以修改输出内部样式的 CSS
#wrapper { padding: 0.5rem !important; border: 1px solid #660000; border-radius: 10px; box-shadow: 0 2px 6px rgba(102,0,0,.5); background: white; margin: 0.5rem 0; display: inline-block; position: relative; left: 50%; transform: translateX(-50%); } #wrapper p { margin: 0; } #wrapper img { height: 1rem; vertical-align: text-top; }
页面版本: 3, 最后编辑于: 26 Nov 2021 18:27