[Golang] 關於Golang Time 的處理

[Golang] 關於Golang Time 的處理 花了一點時間在處理timestamp.工作不外乎是儲存timestamp,解析timestamp並且比對timestamp. 找了很久的相關資料,卻發現處理上並沒有那麼直覺主要是在於以下幾個部分. ####格式: 永遠會困在格式之中 首先最重要的就是處理time的格式,根據Golang Time Pkg,裡面的格式可以有ANSI, RFC822, RFC1123…. 許多種,這裡面大家所熟知的ISO 8601其實沒有名列在其中.但是你可以使用它其中一個profile 那麼就是RFC 3339 儲存格式決定你要如何存取時間的資料. 當然…. 如果大家都是丟time.Time 的格式.. 那麼就世界太平…. 如果不行.. 大家談一下如何parse.. ####時區: 時間比對的重要參數 大家都知道,時區是一個濫觴.但是只要能夠正確的處理其實Golang裡面內建已經把時區處理好了.主要用來解析的是Parse與ParseInLocation 會需要使用到時區的時候,通常是在於接收資料從client或是其他的程式. 這時候比較建議的是: 統一使用UTC 時區,作為字串解析會統一點 LoadLocation 是個好東西,可以幫助你轉換時區.前提是你已經拿到time.Time 以下為一個簡單範例去取得UTC的時間 func GetUTCTime() time.Time { t := time.Now() local_location, err := time.LoadLocation("UTC") if err != nil { fmt.Println(err) } time_UTC := t.In(local_location) return time_UTC } 如果你必須要parse一個time.Time不支援的格式,來轉換成time.Time格式.以下是一個簡單的範例.(當然前提資料是 UTC :) ) func ParseCustomTimeString(time_string string) time.Time { //"2015-03-10 23:58:23 UTC" str_array := strings.Split(time_string, "UTC") var year, mon, day, hh, mm, ss int fmt.Sscanf(str_array[0], "%d-%d-%d %d:%d:%d ", &year, &mon, &day, &hh, &mm, &ss) time_string_to_parse := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d+00:00", year, mon, day, hh, mm, ss) url_create_time, _ := time.Parse(time.RFC3339, time_string_to_parse) return url_create_time } ####時間的比對,善用Before/After 針對於時間的操作上面,其實Golang Time Pkg 提供了很方便的比較時間先後的方式. Before/After 其實只要透過以下的方式,就可以比較不同時區,不同時間格式的先後順序. func IsWarrantyExpired(time_purchase time.Time) bool { //time of product purchase store in DB which is time.time with ETC time_Warranty := time_purchase.AddDate(1, 0, 0) //假設是一年保固 return time_Warranty.After(time.Now()) //如果保固期比現在還後面,代表還是保固期內. } 範例程式中, time.Time.AddDate() 是一個可以取出變更過的時間.比如說你需要取出現在這個時間一年後的timestamp,就可以透過這個.目前支援年,月,日. 如果要小時的話,可能就得要手動去改. ####在資料庫裡處理time.Time 其實不論是 MySQL或是...
繼續閱讀

[XMPP][Ejabberd] Some XMPP spec survey

Preface: This article is a note about I study XMPP spec recent days. To maniplate a XMPP client, it might be easy to deal it with IP*WORKs 3rd party XMPP client. But it could not fulfil some custom request such as: Not response friend request immediatelly, once we got it. Handle roster (friend list) programmatically Maniplate VCard as custom information storage. In those case, we might need to handle XMPP XML commands to Ejabberd Ejabberd is a XMPP server(which twitter use it at first). It written by Erlang, it is powerful to handle multiple user connectivity. XMPP Command Both we could have two ways to manipulate XMPP, one by 3rd party XMPP framework. We will use IP*Work for a example which you can find sample code from web here. Another one is using basic XML send to XMPP server directly. Actually we still use IP*Works. sendCommand to send our XML...
繼續閱讀

[Golang][程式設計週記].. 2015第九週

##雜七雜八感言: 開始把一些內容分成別篇文章,最近多花了很多時間讀XMPP Spec挺有趣的. ##筆記: [Golang] 關於Golang 有趣的網頁與小工具 GRPC: Go Project 支援HTTP/2之外,還支援九種以上的語言.並且可以擺脫net/rpc原本需要管理gob的痛苦. Gore: Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. 另外一個Go REPL的工具,支援許多shortcut跟即時顯示… Viper: A configuration tool of Go 用來讀取與使用Config的工具,開發者是spf13 Hugo的開發者.當然,Hugo也有使用Viper Wide: A Web-based IDE for Teams using Golang. https://wide.b3log.org 相當酷的網站,可以online 寫go而且有資料夾的概念.並且有syntax highlighter的. [Golang] Meetup Youtube Playlist: London Go Gathering February 2015 FOSDEM 2015 Video list Go at CoreOS by +Kelsey Hightower​​ This session will discuss using Go to build products that make distributed computing as stress-free as installing a Linux distribution. Finding Bad Needles in Worldwide Haystacks by Dmitry Savintsev Experience of using Go for a large-scale web security scanner Moving MongoDB components to Go by +Norberto Leite​​ We love Go and this train is unstoppable! CockroachDB by +Tobias Schottdorf​​ Towards an Open-Source Spanner HTTP/2 for Go by +Brad Fitzpatrick​​ Overview of HTTP/2 and the design of Go’s support for it Go and...
繼續閱讀

[Golang]關於Go的Constant的型別轉換(Type Cast)

前言: 原本的問題是出現在Stackoverflow,有人發問template.HTML(“test”) == “test” 可以過,但是template.HTML(“test”) == some_str 卻不能成 功compiler. 原因: 主要的原因是由於Go 本身是static typing的程式語言,而constant本身的型別是未定,並且對於constant在compiler的狀態下是會有一些型別轉換的. // 型別尚未確定 const hello_string = "Hello, 世界" // 型別確定為字串 fmt.Printf(" type is %T \n", hello_string) //type is string 範例程式碼: 根據The Go Blog: Constants 裡面有詳細提到關於Go 是 statical typing的一些問題. 接下來用幾個範例解釋: package main import ( "fmt" ) func main() { // hello Type is not define for now. const hello = "Hello, 世界" // Type is String const typedHello string = "Hello, 世界" // hello type is cast to string fmt.Printf("Type of hello is=%T, typedHello is=%T \n", hello, typedHello) // hello is non-type it convert to string and compare fmt.Println(hello == typedHello) //Another sample. Define MyString as a string type type MyString string var m MyString // m is non-type for now. m = "Hello, 世界" // It will introduce error, because m type is MyString and typeHello is string. //fmt.Println(m == typedHello) // hello is non-type until compare with...
繼續閱讀

[Git]使用git subtree與git submodule來拆解與管理專案

前言: 我想這樣的需求應該也不少,原本在專案的開發過程中,可能在一個respository會變得相當的大而且相當的肥.這時候可能要把一些專案拆解出去. 但是又希望有一個overview的專案在外面可以查看,研究了一下利用sourcetree想要達到這樣的方式,但是看起來還是得用git cli來使用. 範例: 以下的解釋都會透過這個範例,假設我們現在一有一個project_sample裡面有feature_a與feature_b.但是feature_a由於太受歡迎(真有這種feature???)所以很多產品都要使用,必須要獨立出來開發. 資料結構如下: project_sample feature_a feature_b 這時候,我們要把feature_a獨立出來成為另外一個repository並且希望開發project_sample的人可以一樣看得到feature_a. SourceTree沒有你想像的好用 Sourcetree是一套提供GUI的git tool,我個人不論是在Mac/Windows都有使用這套軟體.雖然bug不少,倒也算是涵蓋了我常用的幾個功能.不過要使用接下來的 git subtree split 就無法使用 sourcetree的UI,得用command line. 詳細步驟:切割repo 接下來的詳細步驟可以參考這個StackOverflow,以下以我提出的範例來做示範 : 在project_foo 下面,使用command line //這裏要注意的是: 如果是在Windows 路徑要使用 A/B/C 而不是 A\B\C git subtree split -P feature_a -b "branchA" 建立出新的branch之後,你會發現所有change list被切成兩個branch.一個是原本project_sample全部,另一個只有feature_a 接下來要把branchA submit到新的repository,建議路徑先不要在相同目錄下 mkdir <new-repo> cd <new-repo> git init git pull ../projhect_sample branchA git remote add origin <git@some-new-repro> git push origin -u master 如此一來會把剛剛另外一個branchA的所有內容submit到新的repo去.回來到就的repo,如果就的repo不需要留著原來的部分(我們接下來會用submodule import),就必須依照以下方式: // 如此會將所有與feature_a 目錄有關的檔案移除 git rm -rf feature_a 關於Subtree與Submodule取捨 如此一來,光是切割的部分已經算是完成了. 這個部分無法使用Sourcetree來完成. 接下來要把原來的Project_Sample來把feature_a作為一個submodule來輸入. 這裏我曾經有試過透過git subtree add的方式,把feature_a加回原來的project_sample.不過這樣sumit之後會把所有的code都submit而sub repo “feature_a”修改後無法正確的對應到 project_sample. (只有第一個人可以,其他clone下來的人會看成一個完整的repo 而喪失了subtree的link) 或許這個部分可能是操作上的錯誤. 持續研究中,我使用的是 submodule 來管理project_sample中的 feature_a. 詳細步驟:管理sub repo 接下來,可以使用Sourcetree來管理feature_a. [Repository]->[Add Submodule..] 選擇好你的feature_a,直接就可以Add Submodule進來. 由於是透過Sourcetree所以會直接clone下來並且在左邊會出現一個Submodules 更新Submodule: 接下來,如果你要更新Submodule裡面的程式碼,需要把滑鼠移到Submodules,並且選擇[Open Submodule] [Fetch] -> [Pull] //In Submodule 如此一來,你僅僅是你local把submodule的部份更新了.如果要所有的人都更新最新版本的submodule 還是需要submit submodule的tag. [Commit]->[Push] // submodule, in project_sample 修改Submodule: 如果要修改,一樣選擇[Open Submodule] [Commit]->[Push] //In Submodule 除了,local Push之外.你還需要sync submodule的tag. 回到原來的 project_sample commit “submodule”. [Commit]->[Push] // submodule, in project_sample ###參考文章: Stackoverflow: Detach subdirectory into separate Git repository GitSCM: 6.6 Git 工具 - 子模組 (Submodules) 第十三號艦隊: git subtree WMの物語:...
繼續閱讀

[MongoDB]More study about mongodb and mgo

Preface After some study about mongodb in some web server usage, I occur some question and need to resolve it. Here is three major questions I found here. Question 1: Auto increase number Question 2: Check field(column Auto increase number This question comes from the usage of MySQL auto increase sequence number. I am wondering if there is similiar mechanism in MongoDB. There is a official article about MongoDB implement auto-increasementing Sequence Field. The simple idea, is to add extra record in your database to storage this field. It might impact as follow: Your index might need to refere this field. Your query all need filter(ignore) the sequence record. (refer to “check field if exist”) For the FindAndModify mechanism in MongoDB, the mgo has similar implement about “Apply”. type DBSequence struct { ID string `json:"id"` // ID is a string ID, in this case I will use collection.Name as it...
繼續閱讀