github.com/viant/toolbox@v0.34.5/time_format.go (about) 1 package toolbox 2 3 import ( 4 "strings" 5 "time" 6 ) 7 8 //DateFormatKeyword constant 'dateFormat' key 9 var DateFormatKeyword = "dateFormat" 10 11 //DateLayoutKeyword constant 'dateLayout' key 12 var DateLayoutKeyword = "dateLayout" 13 14 //DateFormatToLayout converts java date format https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone into go date layout 15 func DateFormatToLayout(dateFormat string) string { 16 17 dateFormat = strings.Replace(dateFormat, "ddd", "_2", 1) 18 dateFormat = strings.Replace(dateFormat, "dd", "02", 1) 19 dateFormat = strings.Replace(dateFormat, "d", "2", 1) 20 21 dateFormat = strings.Replace(dateFormat, "HH", "15", 1) 22 23 dateFormat = strings.Replace(dateFormat, "hh", "03", 1) 24 dateFormat = strings.Replace(dateFormat, "h", "3", 1) 25 26 dateFormat = strings.Replace(dateFormat, "mm", "04", 1) 27 dateFormat = strings.Replace(dateFormat, "m", "4", 1) 28 29 dateFormat = strings.Replace(dateFormat, "ss", "05", 1) 30 dateFormat = strings.Replace(dateFormat, "s", "5", 1) 31 32 dateFormat = strings.Replace(dateFormat, "yyyy", "2006", 1) 33 dateFormat = strings.Replace(dateFormat, "yy", "06", 1) 34 dateFormat = strings.Replace(dateFormat, "y", "06", 1) 35 36 dateFormat = strings.Replace(dateFormat, "SSS", "000", 1) 37 38 dateFormat = strings.Replace(dateFormat, "a", "pm", 1) 39 dateFormat = strings.Replace(dateFormat, "aa", "PM", 1) 40 41 dateFormat = strings.Replace(dateFormat, "MMMM", "January", 1) 42 dateFormat = strings.Replace(dateFormat, "MMM", "Jan", 1) 43 dateFormat = strings.Replace(dateFormat, "MM", "01", 1) 44 dateFormat = strings.Replace(dateFormat, "M", "1", 1) 45 46 dateFormat = strings.Replace(dateFormat, "ZZ", "-0700", 1) 47 48 dateFormat = strings.Replace(dateFormat, "Z", "-07", 1) 49 50 dateFormat = strings.Replace(dateFormat, "zz:zz", "Z07:00", 1) 51 dateFormat = strings.Replace(dateFormat, "zzzz", "Z0700", 1) 52 dateFormat = strings.Replace(dateFormat, "z", "MST", 1) 53 54 dateFormat = strings.Replace(dateFormat, "EEEE", "Monday", 1) 55 dateFormat = strings.Replace(dateFormat, "E", "Mon", 1) 56 57 return dateFormat 58 } 59 60 //GetTimeLayout returns time laout from passed in map, first it check if DateLayoutKeyword is defined is so it returns it, otherwise it check DateFormatKeyword and if exists converts it to dateLayout 61 //If neithers keys exists it panics, please use HasTimeLayout to avoid panic 62 func GetTimeLayout(input interface{}) string { 63 switch settings := input.(type) { 64 case map[string]string: 65 if value, found := settings[DateLayoutKeyword]; found { 66 return value 67 } 68 69 if value, found := settings[DateFormatKeyword]; found { 70 return DateFormatToLayout(value) 71 } 72 73 case map[string]interface{}: 74 if value, found := settings[DateLayoutKeyword]; found { 75 return AsString(value) 76 } 77 78 if value, found := settings[DateFormatKeyword]; found { 79 return DateFormatToLayout(AsString(value)) 80 } 81 82 } 83 return "" 84 } 85 86 //HasTimeLayout checks if dateLayout can be taken from the passed in setting map 87 func HasTimeLayout(input interface{}) bool { 88 switch settings := input.(type) { 89 case map[string]string: 90 if _, found := settings[DateLayoutKeyword]; found { 91 return true 92 } 93 if _, found := settings[DateFormatKeyword]; found { 94 return true 95 } 96 97 case map[string]interface{}: 98 if _, found := settings[DateLayoutKeyword]; found { 99 return true 100 } 101 if _, found := settings[DateFormatKeyword]; found { 102 return true 103 } 104 105 } 106 return false 107 } 108 109 //TimestampToString formats timestamp to passed in java style date format 110 func TimestampToString(dateFormat string, unixTimestamp, unixNanoTimestamp int64) string { 111 t := time.Unix(unixTimestamp, unixNanoTimestamp) 112 dateLayout := DateFormatToLayout(dateFormat) 113 return t.Format(dateLayout) 114 }