/* Split a string into a start/end UNIX time stamp assuming the string is in one of the following formats: +nnnn start == now end == now + nnn timestamp start == now end == timestamp ts1-ts2 start == ts1 end == ts2 (start may be adjusted to now if old) If the end time value is before the start time value it is set to the start time value. */ func Str2start_end(tok string) (startt int64, endt int64) { now := time.Now().Unix() if tok[0:1] == "+" { startt = now endt = startt + clike.Atoll(tok) } else { idx := strings.Index(tok, "-") // separate start-end times if idx > 0 { startt = clike.Atoll(tok[0:idx]) endt = clike.Atoll(tok[idx+1:]) if startt < now { startt = now } } else { startt = now endt = clike.Atoll(tok) } } if endt < startt { endt = startt } return }
func TestAtoll(t *testing.T) { // must use bloody camel case to be recognised by go testing var ( iv interface{} // use interface to ensure that the type coming back is 64 bit v int64 ) iv = clike.Atoll("123456") switch iv.(type) { case int64: break default: t.Errorf("atoll() did not return int64, no other atoll() tests executed") return } if iv.(int64) != 123456 { t.Errorf("atoll( '123456' did not return 123456, got: %d", iv.(int64)) } v = clike.Atoll("0x8000") if v != 0x8000 { t.Errorf("atoll( '0x8000' ) did not return 0x8000") } v = clike.Atoll("foo") if v != 0 { t.Errorf("atoll( \"foo\" ) did not return 0 as expected.") } v = clike.Atoll("092") if v != 0 { t.Errorf("atoll( \"092\" ) did not return 0 as expected.") } v = clike.Atoll("029") if v != 2 { t.Errorf("atoll( \"029\" ) did not return 2 as expected.") } s := "1234" v = clike.Atoll(&s) if v != 1234 { t.Errorf("atoll( &\"1234\" ) did not return 1234 as expected.") } v = clike.Atoll(nil) if v != 0 { t.Errorf("atoll( nil ) did not return 0 as expected.") } }
/* Internal funciton to convert an interface into a supported desired value. */ func cvt2desired(v interface{}, desired int) interface{} { switch v.(type) { case string: switch desired { case ET_STRING: return v case ET_STRINGP: return &v case ET_INT: return v.(int) case ET_UINT: return v.(uint) case ET_INT64: return v.(int64) case ET_FLOAT: return v.(float64) case ET_BOOL: return v == "true" } case *string: switch desired { case ET_STRING: return *(v.(*string)) case ET_STRINGP: return v case ET_INT: return clike.Atoi(*(v.(*string))) case ET_UINT: return clike.Atou(*(v.(*string))) case ET_INT64: return clike.Atoll(*(v.(*string))) case ET_FLOAT: return clike.Atof(*(v.(*string))) case ET_BOOL: return *(v.(*string)) == "true" } case float64: switch desired { case ET_STRING: return fmt.Sprintf("%.2f", v) case ET_STRINGP: s := fmt.Sprintf("%.2f", v) return &s case ET_INT: return int(v.(float64)) case ET_UINT: return uint(v.(float64)) case ET_INT64: return int64(v.(float64)) case ET_FLOAT: return v case ET_BOOL: return v.(float64) != 0.0 } case int: switch desired { case ET_STRING: return fmt.Sprintf("%d", v) case ET_STRINGP: s := fmt.Sprintf("%d", v) return &s case ET_INT: return v case ET_UINT: return uint(v.(int)) case ET_INT64: return int64(v.(int)) case ET_FLOAT: return float64(v.(int)) case ET_BOOL: return v.(int) != 0 } case int64: switch desired { case ET_STRING: return fmt.Sprintf("%d", v) case ET_STRINGP: s := fmt.Sprintf("%d", v) return &s case ET_INT: return int(v.(int64)) case ET_UINT: return uint(v.(int64)) case ET_INT64: return v case ET_FLOAT: return float64(v.(int64)) case ET_BOOL: return v.(int64) != 0 } case bool: switch desired { case ET_STRING: return fmt.Sprintf("%v", v) case ET_STRINGP: s := fmt.Sprintf("%v", v) return &s case ET_INT: if v.(bool) { return int(1) } else { return int(0) } case ET_UINT: if v.(bool) { return uint(1) } else { return uint(0) } case ET_INT64: if v.(bool) { return int64(1) } else { return int64(0) } case ET_FLOAT: if v.(bool) { return 1.0 } else { return 0.0 } case ET_BOOL: return v } } return nil }