2020-05-20 15:10:24 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-05-22 16:29:12 +02:00
|
|
|
"strings"
|
2020-05-20 15:10:24 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2,}")
|
2020-05-20 21:57:45 +02:00
|
|
|
var DateCanonicalRegexp = regexp.MustCompile("\\D\\d{8}_\\d{6}_\\w+\\.")
|
2020-05-28 15:12:18 +02:00
|
|
|
var DatePathRegexp = regexp.MustCompile("\\D\\d{4}/\\d{1,2}/?\\d*")
|
2020-05-22 16:29:12 +02:00
|
|
|
var DateTimeRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2,}")
|
2020-05-20 15:10:24 +02:00
|
|
|
var DateIntRegexp = regexp.MustCompile("\\d{1,4}")
|
2020-05-28 15:12:18 +02:00
|
|
|
var YearRegexp = regexp.MustCompile("\\d{4,5}")
|
2020-06-13 07:40:35 +02:00
|
|
|
var CountryWordsRegexp = regexp.MustCompile("[\\p{L}]{2,}")
|
2020-05-20 15:10:24 +02:00
|
|
|
|
|
|
|
var (
|
2020-05-20 19:58:59 +02:00
|
|
|
YearMin = 1990
|
2020-05-20 15:10:24 +02:00
|
|
|
YearMax = time.Now().Year() + 3
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MonthMin = 1
|
|
|
|
MonthMax = 12
|
|
|
|
DayMin = 1
|
|
|
|
DayMax = 31
|
|
|
|
HourMin = 0
|
|
|
|
HourMax = 24
|
|
|
|
MinMin = 0
|
|
|
|
MinMax = 59
|
|
|
|
SecMin = 0
|
|
|
|
SecMax = 59
|
|
|
|
)
|
|
|
|
|
|
|
|
// Time returns a string as time or the zero time instant in case it can not be converted.
|
|
|
|
func Time(s string) (result time.Time) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
result = time.Time{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
if len(s) < 6 {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
if !strings.HasPrefix(s, "/") {
|
2020-05-22 16:29:12 +02:00
|
|
|
s = "/" + s
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:10:24 +02:00
|
|
|
b := []byte(s)
|
|
|
|
|
2020-05-20 21:57:45 +02:00
|
|
|
if found := DateCanonicalRegexp.Find(b); len(found) > 0 { // Is it a canonical name like "20120727_093920_97425909.jpg"?
|
|
|
|
if date, err := time.Parse("20060102_150405", string(found[1:16])); err == nil {
|
2020-05-20 15:10:24 +02:00
|
|
|
result = date.Round(time.Second).UTC()
|
|
|
|
}
|
|
|
|
} else if found := DateTimeRegexp.Find(b); len(found) > 0 { // Is it a date with time like "2020-01-30_09-57-18"?
|
|
|
|
n := DateIntRegexp.FindAll(found, -1)
|
|
|
|
|
|
|
|
if len(n) != 6 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
year := Int(string(n[0]))
|
|
|
|
month := Int(string(n[1]))
|
|
|
|
day := Int(string(n[2]))
|
|
|
|
hour := Int(string(n[3]))
|
|
|
|
min := Int(string(n[4]))
|
|
|
|
sec := Int(string(n[5]))
|
|
|
|
|
|
|
|
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax || day < DayMin || day > DayMax {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if hour < HourMin || hour > HourMax || min < MinMin || min > MinMax || sec < SecMin || sec > SecMax {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
result = time.Date(
|
|
|
|
year,
|
|
|
|
time.Month(month),
|
|
|
|
day,
|
|
|
|
hour,
|
|
|
|
min,
|
|
|
|
sec,
|
|
|
|
0,
|
|
|
|
time.UTC)
|
|
|
|
|
|
|
|
} else if found := DateRegexp.Find(b); len(found) > 0 { // Is it a date only like "2020-01-30"?
|
|
|
|
n := DateIntRegexp.FindAll(found, -1)
|
|
|
|
|
|
|
|
if len(n) != 3 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
year := Int(string(n[0]))
|
|
|
|
month := Int(string(n[1]))
|
|
|
|
day := Int(string(n[2]))
|
|
|
|
|
|
|
|
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax || day < DayMin || day > DayMax {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
result = time.Date(
|
|
|
|
year,
|
|
|
|
time.Month(month),
|
|
|
|
day,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
time.UTC)
|
|
|
|
} else if found := DatePathRegexp.Find(b); len(found) > 0 { // Is it a date path like "2020/01/03"?
|
|
|
|
n := DateIntRegexp.FindAll(found, -1)
|
|
|
|
|
|
|
|
if len(n) < 2 || len(n) > 3 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
year := Int(string(n[0]))
|
|
|
|
month := Int(string(n[1]))
|
|
|
|
|
|
|
|
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(n) == 2 {
|
|
|
|
result = time.Date(
|
|
|
|
year,
|
|
|
|
time.Month(month),
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
time.UTC)
|
|
|
|
} else if day := Int(string(n[2])); day >= DayMin && day <= DayMax {
|
|
|
|
result = time.Date(
|
|
|
|
year,
|
|
|
|
time.Month(month),
|
|
|
|
day,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
time.UTC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int returns a string as int or 0 if it can not be converted.
|
|
|
|
func Int(s string) int {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(result)
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
|
|
|
|
// IsUInt returns true if a string only contains an unsigned integer.
|
|
|
|
func IsUInt(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if r < 48 || r > 57 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
|
|
|
|
// CountryCode tries to find a matching country code for a given string e.g. from a file oder directory name.
|
|
|
|
func CountryCode(s string) string {
|
|
|
|
if s == "zz" {
|
|
|
|
return "zz"
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
words := CountryWordsRegexp.FindAllString(s, -1)
|
2020-05-28 15:12:18 +02:00
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
for i, w := range words {
|
|
|
|
if i < len(words)-1 {
|
|
|
|
search := strings.ToLower(w + " " + words[i+1])
|
|
|
|
|
|
|
|
if code, ok := Countries[search]; ok {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search := strings.ToLower(w)
|
2020-06-12 18:01:22 +02:00
|
|
|
|
|
|
|
if code, ok := Countries[search]; ok {
|
2020-05-28 15:12:18 +02:00
|
|
|
return code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "zz"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Year tries to find a matching year for a given string e.g. from a file oder directory name.
|
|
|
|
func Year(s string) int {
|
|
|
|
b := []byte(s)
|
|
|
|
|
|
|
|
found := YearRegexp.FindAll(b, -1)
|
|
|
|
|
|
|
|
for _, match := range found {
|
|
|
|
year := Int(string(match))
|
|
|
|
|
|
|
|
if year > YearMin && year < YearMax {
|
|
|
|
return year
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|