123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package main
- import (
- "encoding/binary"
- "regexp"
- "time"
- )
- func Int32toByteArray(i int32) (arr [4]byte) {
- binary.BigEndian.PutUint32(arr[0:4], uint32(i))
- return
- }
- func Int64toByteArray(i int64) (arr [8]byte) {
- binary.BigEndian.PutUint64(arr[0:8], uint64(i))
- return
- }
- // Find all the words in a string
- // return [][2]int of index,length
- func find_words(text string) [][]int {
- word, _ := regexp.Compile("([A-Za-z]+)")
- return word.FindAllStringIndex(text, -1)
- }
- // int version of Abs
- func Abs(x int) int {
- if x < 0 {
- return -x
- }
- return x
- }
- // Adjust date to 2:00 AM
- func NormalizeDate(date *time.Time) {
- offset := time.Duration(date.Second())*time.Second +
- time.Duration(date.Minute())*time.Minute +
- time.Duration(date.Hour()-2)*time.Hour +
- time.Duration(date.Nanosecond())*time.Nanosecond
- *date = date.Add(-offset)
- }
- // Find the 1st of the Month (Normalized) 2:00 AM
- func FirstOfMonthDate(date *time.Time) {
- if date.Day() != 1 {
- *date = date.AddDate(0, 0, 1-date.Day())
- }
- NormalizeDate(date)
- }
- // Find 1st of the Month (Normalize) Unix
- func FirstOfMonthDateUnix(dateUnix *int64) {
- var date time.Time = time.Unix(*dateUnix, 0)
- FirstOfMonthDate(&date)
- *dateUnix = date.Unix()
- }
- // See:
- // https://yourbasic.org/golang/format-parse-string-time-date-example/
- // for all your time.Time.Format needs.
- func FormatDate(dateUnix int64, format string) string {
- var date time.Time = time.Unix(dateUnix, 0)
- return date.Format(format)
- }
- func CurrentMonth(date time.Time) string {
- return date.Format("January")
- }
|