Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday, March 3, 2012

Mysterious Timestamp

時間轉換大概是最惱人的「小」問題之一,每個語言的標準處理方式不盡相同。Perl 的 localtime() 是月份從 0 開始到 11 結束,Ruby 的 DateTime 是月份是從 1 開始的,不過 Perl 的 DateTime 月份也從 1 開始 (LOL)。背誦這些東西大概會發瘋。

不過 Epoch 沒有這個問題,任何語言上的 Epoch 應該都是一致的,有的只是時差問題,使用 Epoch 來初始時間物件大概是避開這問題的好方法之一,特別在不同語言間交換時間資料時,用 Epoch 交換後怎麼 format 都不會出問題。所以有些時間模組大概會像 Perl 的 DateTime 一樣實作 from_epoch() 這種 constructor。

就這點設計而言,Badger::Timestamp 大概是最好用的時間模組,其 constructor 支援 DateTime 模組的方式初始化外,還可以餵食 Epoch 和 ISO 格式的時間字串。一個簡單的 Epoch/ISO converter 只要八行。

什麼,你問說這八行程式有什麼用,有些跑了好幾年的 unix 系統上有著 crontab 執行著需要 timestamp 來分辨執行區段的分析器,有些要當天的零分零秒,有些要隔天的零分零秒。壞掉時有人會要你一定要「人工」處理,這八行就就是這樣來的。

附帶一提, Javascript 的 Date.UTC 和 Perl 的 localtime() 一樣,都是月份從 0 開始。


和 Badger::Timestamp 很像的 moment.js這時候就很好用了,其 constructor 用陣列初始化時雖然還是沒有改用 1 作為月份起始著實也讓我驚訝了一下。好險,這 constructor 支援用 Epoch 初始化(不過承襲 javascript 的慣例,是 millisecond),還可以用 Date.parse() 支援的字串初始化。這兩招可以避開月份的問題。

Saturday, December 13, 2008

simple clone of GitHub Badge

Thanks to jQuery, I just spend three hours to create GitHub Widget, a clone of GitHub Badge. The widget is put in the sidebar of this page.



Compare with GitHub Badge, GitHub Widget is full written in jQuery, so that it is also compatible with IE. And the usage and html structure is full compatible with GitHub Badge.

I host it here temporarily. Please feel free to use it.

 

github api callback

Not documented on Official GitHub API Guide, GitHub provide "callback" parameter to allow corss-site json accessing.

Example in jQuery:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type="text/javascript"></script>

<script>

function myFunction(data) {
// data is the json we would get
}

$.getScript("http://github.com/api/v1/json/username?callback=myFunction")

</script>