From 533681e0f83ec4f69b3b8e9f1982ed9f089285b4 Mon Sep 17 00:00:00 2001 From: Clemens Fries Date: Tue, 1 Nov 2016 17:11:58 +0100 Subject: Initial commit --- common/util.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 common/util.go (limited to 'common/util.go') diff --git a/common/util.go b/common/util.go new file mode 100644 index 0000000..a4422c7 --- /dev/null +++ b/common/util.go @@ -0,0 +1,63 @@ +/* util.go: various utility functions + * + * Copyright (C) 2016 Clemens Fries + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package common + +import ( + "strings" + "time" +) + +// Split a text in two parts on the first blank line. +func SplitMessage(text []string) ([]string, []string) { + conf := []string{} + body := []string{} + + inBody := false + + for _, line := range text { + if !inBody && strings.TrimSpace(line) == "" { + inBody = true + continue // skip the separating line + } + + if inBody { + body = append(body, line) + } else { + conf = append(conf, line) + } + } + + return conf, body +} + +// Parse a time which may either be just a date or a date with time. +func ParseTime(datetime string) (time.Time, error) { + result, err := time.ParseInLocation(DATE_FORMAT, datetime, time.Now().Location()) + + if err == nil { + return result, nil + } + + result, err = time.ParseInLocation(DATETIME_FORMAT, datetime, time.Now().Location()) + + if err == nil { + return result, nil + } + + return result, err +} -- cgit