emkornfield commented on a change in pull request #11359:
URL: https://github.com/apache/arrow/pull/11359#discussion_r741393338



##########
File path: go/arrow/datatype_fixedwidth.go
##########
@@ -54,6 +56,140 @@ type (
        Duration  int64
 )
 
+// Date32FromTime returns a Date32 value from a time object
+func Date32FromTime(t time.Time) Date32 {
+       return Date32(t.Unix() / int64((time.Hour * 24).Seconds()))
+}
+
+func (d Date32) ToTime() time.Time {
+       return time.Unix(0, 0).UTC().AddDate(0, 0, int(d))
+}
+
+// Date64FromTime returns a Date64 value from a time object
+func Date64FromTime(t time.Time) Date64 {
+       return Date64(t.Unix()*1e3 + int64(t.Nanosecond())/1e6)
+}
+
+func (d Date64) ToTime() time.Time {
+       days := int(int64(d) / (time.Hour * 24).Milliseconds())
+       return time.Unix(0, 0).UTC().AddDate(0, 0, days)
+}
+
+// TimestampFromString parses a string and returns a timestamp for the given 
unit
+// level.
+//
+// The timestamp should be in one of the following forms, [T] can be either T
+// or a space, and [.zzzzzzzzz] can be either left out or up to 9 digits of
+// fractions of a second.
+//
+//      YYYY-MM-DD
+//      YYYY-MM-DD[T]HH
+//   YYYY-MM-DD[T]HH:MM
+//   YYYY-MM-DD[T]HH:MM:SS[.zzzzzzzz]
+func TimestampFromString(val string, unit TimeUnit) (Timestamp, error) {
+       format := "2006-01-02"
+       if val[len(val)-1] == 'Z' {
+               val = val[:len(val)-1]
+       }
+
+       switch {
+       case len(val) == 13:
+               format += string(val[10]) + "15"
+       case len(val) == 16:
+               format += string(val[10]) + "15:04"
+       case len(val) >= 19:
+               format += string(val[10]) + "15:04:05.999999999"
+       }
+
+       out, err := time.ParseInLocation(format, val, time.UTC)
+       if err != nil {
+               return 0, err
+       }
+
+       switch unit {
+       case Second:
+               return Timestamp(out.Unix()), nil
+       case Millisecond:
+               return Timestamp(out.Unix()*1e3 + int64(out.Nanosecond())/1e6), 
nil

Review comment:
       I think at least in C++ errors are raised when if precision is truncated 
(i.e. we'd expect everything past the millisecond precision to be 0)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to