久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從代碼中檢查時區標識符是否有效?

How to check is timezone identifier valid from code?(如何從代碼中檢查時區標識符是否有效?)
本文介紹了如何從代碼中檢查時區標識符是否有效?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我會試著解釋這里的問題.

I'll try to explain what's the problem here.

根據 PHP 手冊中的支持的時區列表,我可以在 PHP 中看到所有有效的 TZ 標識符.

According to list of supported timezones from PHP manual, I can see all valid TZ identifiers in PHP.

我的第一個問題是如何從代碼中獲取該列表,但這并不是我真正需要的.

My first question is how to get that list from code but that's not what I really need.

我的最終目標是編寫函數 isValidTimezoneId() 如果時區有效則返回 TRUE,否則返回 FALSE.>

My final goal is to write function isValidTimezoneId() that returns TRUE if timezone is valid, otherwise it should return FALSE.

function isValidTimezoneId($timezoneId) {
  # ...function body...
  return ?; # TRUE or FALSE
  }

因此,當我在函數中使用 $timezoneId(字符串)傳遞 TZ 標識符時,我需要布爾結果.

So, when I pass TZ identifier using $timezoneId (string) in function I need boolean result.

嗯,到目前為止我所擁有的......

Well, what I have so far...

我得到的第一個解決方案是這樣的:

First solution I've got is something like this:

function isValidTimezoneId($timezoneId) {
  $savedZone = date_default_timezone_get(); # save current zone
  $res = $savedZone == $timezoneId; # it's TRUE if param matches current zone
  if (!$res) { # 0r...
    @date_default_timezone_set($timezoneId); # try to set new timezone
    $res = date_default_timezone_get() == $timezoneId; # it's true if new timezone set matches param string.
    }
  date_default_timezone_set($savedZone); # restore back old timezone
  return $res; # set result
  }

效果很好,但我想要另一種解決方案(以避免嘗試設置錯誤的時區)

That works perfectly, but I want another solution (to avoid trying to set wrong timezone)

然后,我試圖獲取有效時區標識符的列表,并使用 in_array() 對照參數檢查它 函數.所以我嘗試使用 timezone_identifiers_list(),但事實并非如此很好,因為此函數返回的數組中缺少很多時區(DateTimeZone::listIdentifiers()).乍一看,這正是我要找的.

Then, I was trying to get list of valid timezone identifiers and check it against parameter using in_array() function. So I've tried to use timezone_identifiers_list(), but that was not so good because a lot of timezones was missing in array returned by this function (alias of DateTimeZone::listIdentifiers()). At first sight that was exactly what I was looking for.

function isValidTimezoneId($timezoneId) {
  $zoneList = timezone_identifiers_list(); # list of (all) valid timezones
  return in_array($timezoneId, $zoneList); # set result
  }

這段代碼看起來不錯而且簡單,但我發現 $zoneList 數組包含約 400 個元素.根據我的計算,它應該返回 550+ 個元素.缺少 150 多個元素......所以這還不足以解決我的問題.

This code looks nice and easy but than I've found that $zoneList array contains ~400 elements. According to my calculations it should return 550+ elements. 150+ elements are missing... So that's not good enough as solution for my problem.

這是我試圖找到完美解決方案的最后一步.使用此方法返回的數組,我可以提取所有 PHP 支持的時區標識符.

This is last step on my way trying to find perfect solution. Using array returned by this method I can extract all timezone identifiers supported by PHP.

function createTZlist() {
  $tza = DateTimeZone::listAbbreviations();
  $tzlist = array();
  foreach ($tza as $zone)
    foreach ($zone as $item) 
      if (is_string($item['timezone_id']) && $item['timezone_id'] != '')
        $tzlist[] = $item['timezone_id'];
  $tzlist = array_unique($tzlist);
  asort($tzlist);
  return array_values($tzlist);
  }

此函數返回 563 個元素(在 Example #2 中,我有 只有 407 個).

This function returns 563 elements (in Example #2 I've got just 407).

我試圖找出這兩個數組之間的差異:

I've tried to find differences between those two arrays:

$a1 = timezone_identifiers_list();
$a2 = createTZlist();

print_r(array_values(array_diff($a2, $a1)));

結果是:

Array
(
    [0] => Africa/Asmera
    [1] => Africa/Timbuktu
    [2] => America/Argentina/ComodRivadavia
    [3] => America/Atka
    [4] => America/Buenos_Aires
    [5] => America/Catamarca
    [6] => America/Coral_Harbour
    [7] => America/Cordoba
    [8] => America/Ensenada
    [9] => America/Fort_Wayne
    [10] => America/Indianapolis
    [11] => America/Jujuy
    [12] => America/Knox_IN
    [13] => America/Louisville
    [14] => America/Mendoza
    [15] => America/Porto_Acre
    [16] => America/Rosario
    [17] => America/Virgin
    [18] => Asia/Ashkhabad
    [19] => Asia/Calcutta
    [20] => Asia/Chungking
    [21] => Asia/Dacca
    [22] => Asia/Istanbul
    [23] => Asia/Katmandu
    [24] => Asia/Macao
    [25] => Asia/Saigon
    [26] => Asia/Tel_Aviv
    [27] => Asia/Thimbu
    [28] => Asia/Ujung_Pandang
    [29] => Asia/Ulan_Bator
    [30] => Atlantic/Faeroe
    [31] => Atlantic/Jan_Mayen
    [32] => Australia/ACT
    [33] => Australia/Canberra
    [34] => Australia/LHI
    [35] => Australia/NSW
    [36] => Australia/North
    [37] => Australia/Queensland
    [38] => Australia/South
    [39] => Australia/Tasmania
    [40] => Australia/Victoria
    [41] => Australia/West
    [42] => Australia/Yancowinna
    [43] => Brazil/Acre
    [44] => Brazil/DeNoronha
    [45] => Brazil/East
    [46] => Brazil/West
    [47] => CET
    [48] => CST6CDT
    [49] => Canada/Atlantic
    [50] => Canada/Central
    [51] => Canada/East-Saskatchewan
    [52] => Canada/Eastern
    [53] => Canada/Mountain
    [54] => Canada/Newfoundland
    [55] => Canada/Pacific
    [56] => Canada/Saskatchewan
    [57] => Canada/Yukon
    [58] => Chile/Continental
    [59] => Chile/EasterIsland
    [60] => Cuba
    [61] => EET
    [62] => EST
    [63] => EST5EDT
    [64] => Egypt
    [65] => Eire
    [66] => Etc/GMT
    [67] => Etc/GMT+0
    [68] => Etc/GMT+1
    [69] => Etc/GMT+10
    [70] => Etc/GMT+11
    [71] => Etc/GMT+12
    [72] => Etc/GMT+2
    [73] => Etc/GMT+3
    [74] => Etc/GMT+4
    [75] => Etc/GMT+5
    [76] => Etc/GMT+6
    [77] => Etc/GMT+7
    [78] => Etc/GMT+8
    [79] => Etc/GMT+9
    [80] => Etc/GMT-0
    [81] => Etc/GMT-1
    [82] => Etc/GMT-10
    [83] => Etc/GMT-11
    [84] => Etc/GMT-12
    [85] => Etc/GMT-13
    [86] => Etc/GMT-14
    [87] => Etc/GMT-2
    [88] => Etc/GMT-3
    [89] => Etc/GMT-4
    [90] => Etc/GMT-5
    [91] => Etc/GMT-6
    [92] => Etc/GMT-7
    [93] => Etc/GMT-8
    [94] => Etc/GMT-9
    [95] => Etc/GMT0
    [96] => Etc/Greenwich
    [97] => Etc/UCT
    [98] => Etc/UTC
    [99] => Etc/Universal
    [100] => Etc/Zulu
    [101] => Europe/Belfast
    [102] => Europe/Nicosia
    [103] => Europe/Tiraspol
    [104] => Factory
    [105] => GB
    [106] => GB-Eire
    [107] => GMT
    [108] => GMT+0
    [109] => GMT-0
    [110] => GMT0
    [111] => Greenwich
    [112] => HST
    [113] => Hongkong
    [114] => Iceland
    [115] => Iran
    [116] => Israel
    [117] => Jamaica
    [118] => Japan
    [119] => Kwajalein
    [120] => Libya
    [121] => MET
    [122] => MST
    [123] => MST7MDT
    [124] => Mexico/BajaNorte
    [125] => Mexico/BajaSur
    [126] => Mexico/General
    [127] => NZ
    [128] => NZ-CHAT
    [129] => Navajo
    [130] => PRC
    [131] => PST8PDT
    [132] => Pacific/Ponape
    [133] => Pacific/Samoa
    [134] => Pacific/Truk
    [135] => Pacific/Yap
    [136] => Poland
    [137] => Portugal
    [138] => ROC
    [139] => ROK
    [140] => Singapore
    [141] => Turkey
    [142] => UCT
    [143] => US/Alaska
    [144] => US/Aleutian
    [145] => US/Arizona
    [146] => US/Central
    [147] => US/East-Indiana
    [148] => US/Eastern
    [149] => US/Hawaii
    [150] => US/Indiana-Starke
    [151] => US/Michigan
    [152] => US/Mountain
    [153] => US/Pacific
    [154] => US/Pacific-New
    [155] => US/Samoa
    [156] => Universal
    [157] => W-SU
    [158] => WET
    [159] => Zulu
)

此列表包含 Example #2 未能匹配的所有有效 TZ 標識符.

This list contains all valid TZ identifiers that Example #2 failed to match.

還有四個 TZ 標識符($a1 的一部分):

There's four TZ identifiers more (part of $a1):

print_r(array_values(array_diff($a1, $a2)));

輸出

Array
(
    [0] => America/Bahia_Banderas
    [1] => Antarctica/Macquarie
    [2] => Pacific/Chuuk
    [3] => Pacific/Pohnpei
)

現在,我有了近乎完美的解決方案......

So now, I have almost perfect solution...

function isValidTimezoneId($timezoneId) {
  $zoneList = createTZlist(); # list of all valid timezones (last 4 are not included)
  return in_array($timezoneId, $zoneList); # set result
  }

這是我的解決方案,我可以使用它.當然,我使用這個函數作為類的一部分,所以我不需要在每次方法調用時生成 $zoneList.

That's my solution and I can use it. Of course, I use this function as part of class so I don't need to generate $zoneList on every methods call.

我想知道,是否有任何更簡單(更快)的解決方案來獲取所有有效時區標識符的列表作為數組(我想避免從 DateTimeZone::listAbbreviations() 中提取該列表,如果是這樣的話可能的)?或者,如果您知道如何檢查時區參數是否有效的另一種方法,請告訴我(我再說一遍,@ 運算符不能成為解決方案的一部分).

I'm wondering, is there any easier (quicker) solution to get list of all valid timezone identifiers as array (I want to avoid extracting that list from DateTimeZone::listAbbreviations() if that's possible)? Or if you know another way how to check is timezone parameter valid, please let me know (I repeat, @ operator can't be part of solution).

<小時>附言如果您需要更多詳細信息和示例,請告訴我.我猜你沒有.


P.S. If you need more details and examples, let me know. I guess you don't.

我使用的是 PHP 5.3.5(認為這不重要).

I'm using PHP 5.3.5 (think that's not important).

任何在無效時區字符串上拋出異常的代碼部分(使用 @ 隱藏或使用 try..catch 塊捕獲)都不是我正在尋找的解決方案.

Any part of code that throws exception on invalid timezone string (hidden using @ or caught using try..catch block) is not solution I'm looking for.


我已經為這個問題付出了小代價!

現在我正在尋找如何在 PHP 數組中提取所有時區標識符列表的最簡單方法.

Now I'm looking for the easiest way how to extract list of all timezone identifiers in PHP array.

推薦答案

您的解決方案運行良好,因此如果您正在尋找速度,我會更仔細地查看您對數組所做的工作.我已經對幾千次試驗進行計時以獲得合理的平均時間,結果如下:

You solution works fine, so if it's speed you're looking for I would look more closely at what you're doing with your arrays. I've timed a few thousand trials to get reasonable average times, and these are the results:

createTZlist  : 20,713 microseconds per run
createTZlist2 : 13,848 microseconds per run

這是更快的函數:

function createTZList2()
{
 $out = array();
 $tza = timezone_abbreviations_list();
 foreach ($tza as $zone)
 {
  foreach ($zone as $item)
  {
   $out[$item['timezone_id']] = 1;
  }
 }
 unset($out['']);
 ksort($out);
 return array_keys($out);
}

如果將 if 測試簡化為 if ($item['timezone_id']),它會更快,而不是運行 489 次來捕獲單個情況下,之后取消設置空鍵會更快.

The if test is faster if you reduce it to just if ($item['timezone_id']), but rather than running it 489 times to catch a single case, it's quicker to unset the empty key afterwards.

設置哈希鍵允許我們跳過更昂貴的 array_unique() 調用.對鍵進行排序然后提取它們比提取它們然后對提取的列表進行排序要快一點.

Setting hash keys allows us the skip the array_unique() call which is more expensive. Sorting the keys and then extracting them is a tiny bit faster than extracting them and then sorting the extracted list.

如果您放棄排序(除非您比較列表,否則不需要排序),它會減少到 12,339 微秒.

If you drop the sorting (which is not needed unless you're comparing the list), it gets down to 12,339 microseconds.

但實際上,您無論如何都不需要歸還密鑰.查看整體 isValidTimezoneId(),您最好這樣做:

But really, you don't need to return the keys anyway. Looking at the holistic isValidTimezoneId(), you'd be better off doing this:

function isValidTimezoneId2($tzid)
{
 $valid = array();
 $tza = timezone_abbreviations_list();
 foreach ($tza as $zone)
 {
  foreach ($zone as $item)
  {
   $valid[$item['timezone_id']] = true;
  }
 }
 unset($valid['']);
 return !!$valid[$tzid];
}

也就是說,假設您每次執行只需要測試一次,否則您希望在第一次運行后保存 $valid.這種方法避免了必須進行排序或將鍵轉換為值,鍵查找比 in_array() 搜索更快,并且沒有額外的函數調用.將數組值設置為 true 而不是 1 也會在結果為真時刪除單個強制轉換.

That is, assuming you only need to test once per execution, otherwise you'd want to save $valid after the first run. This approach avoids having to do a sort or converting the keys to values, key lookups are faster than in_array() searches and there's no extra function call. Setting the array values to true instead of 1 also removes a single cast when the result is true.

這使它在我的測試機器上可靠地降低到 12 毫秒以下,幾乎是您示例時間的一半.一個有趣的微優化實驗!

This brings it reliably down to under 12ms on my test machine, almost half the time of your example. A fun experiment in micro-optimizations!

這篇關于如何從代碼中檢查時區標識符是否有效?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 成人午夜在线 | 色呦呦在线| 欧美美女二区 | 爱综合 | 91久久久久久久久久久久久 | 成人区精品一区二区婷婷 | 欧美极品视频在线观看 | 91大神新作在线观看 | 999国产精品视频 | 在线观看成人免费视频 | www国产成人免费观看视频 | 久久精品国产99国产精品 | yiren22综合网成人 | 国产精品国产成人国产三级 | 精品国产免费一区二区三区演员表 | 日韩精品久久久久 | 亚洲一区在线免费观看 | 久久国产亚洲 | 国产精品一区二区三区在线 | 一级做a爰片性色毛片16美国 | 91影视 | 97人人干 | 免费一级片 | 国产区高清 | 99re6热在线精品视频播放 | 亚洲国产精品久久久 | 高清av一区| 成人h免费观看视频 | 日韩欧美视频 | 99欧美精品 | 色网站在线 | 中文字幕一区在线 | 91在线精品秘密一区二区 | 国产免费一区二区 | 精品国产一区二区在线 | 999热精品| 亚洲va在线va天堂va狼色在线 | 久久com| 免费一级欧美在线观看视频 | 日韩精品人成在线播放 | 亚洲不卡一 |