Created
May 6, 2013 18:48
-
-
Save dayudodo/5527195 to your computer and use it in GitHub Desktop.
Turn C# time to ruby Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def turnCSharpTimeToRubyTime(sourceTime) | |
reg=/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{7})/ | |
temp,year,month,day,hour,minute,second,nsec=reg.match(sourceTime).to_a | |
return [year,month,day,hour,minute,second,nsec].map{|x| x.to_i} | |
end | |
# C# time String | |
timeStr="2011-08-11T11:17:55.7707133+08:00" | |
arr=turnCSharpTimeToRubyTime(timeStr) | |
# so ugly... but it works! | |
resultTime=Time.mktime(arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6].div(1000)) | |
# Is the result correct? | |
puts resultTime | |
# I need nanosecond | |
puts resultTime.nsec | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment