// FmtDateLong returns the long date representation of 't' for 'vi_VN' func (vi *vi_VN) FmtDateLong(t time.Time) string { b := make([]byte, 0, 32) b = append(b, []byte{0x4e, 0x67, 0xc3, 0xa0, 0x79}...) b = append(b, []byte{0x20}...) if t.Day() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Day()), 10) b = append(b, []byte{0x20, 0x74, 0x68, 0xc3, 0xa1, 0x6e, 0x67}...) b = append(b, []byte{0x20}...) if t.Month() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Month()), 10) b = append(b, []byte{0x20, 0x6e, 0xc4, 0x83, 0x6d}...) b = append(b, []byte{0x20}...) b = strconv.AppendInt(b, int64(t.Year()), 10) return string(b) }
func yearForMonth(mo time.Month, now *time.Time) int { year := now.Year() if mo+6 <= now.Month() { year += 1 } return year }
// Returns wheter the Timing is active at the specified time func (rit *RITiming) IsActiveAt(t time.Time) bool { // check for years if len(rit.Years) > 0 && !rit.Years.Contains(t.Year()) { return false } // check for months if len(rit.Months) > 0 && !rit.Months.Contains(t.Month()) { return false } // check for month days if len(rit.MonthDays) > 0 && !rit.MonthDays.Contains(t.Day()) { return false } // check for weekdays if len(rit.WeekDays) > 0 && !rit.WeekDays.Contains(t.Weekday()) { return false } //log.Print("Time: ", t) //log.Print("Left Margin: ", rit.getLeftMargin(t)) // check for start hour if t.Before(rit.getLeftMargin(t)) { return false } //log.Print("Right Margin: ", rit.getRightMargin(t)) // check for end hour if t.After(rit.getRightMargin(t)) { return false } return true }
// FmtDateFull returns the full date representation of 't' for 'ti_ER' func (ti *ti_ER) FmtDateFull(t time.Time) string { b := make([]byte, 0, 32) b = append(b, ti.daysWide[t.Weekday()]...) b = append(b, []byte{0xe1, 0x8d, 0xa1, 0x20}...) if t.Day() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Day()), 10) b = append(b, []byte{0x20}...) b = append(b, ti.monthsWide[t.Month()]...) b = append(b, []byte{0x20, 0xe1, 0x88, 0x98, 0xe1, 0x8b, 0x93, 0xe1, 0x88, 0x8d, 0xe1, 0x89, 0xb2, 0x20}...) b = strconv.AppendInt(b, int64(t.Year()), 10) b = append(b, []byte{0x20}...) if t.Year() < 0 { b = append(b, ti.erasWide[0]...) } else { b = append(b, ti.erasWide[1]...) } return string(b) }
func isStartOfMonth(t time.Time) bool { y := t.Year() m := t.Month() utcLoc, _ := time.LoadLocation("UTC") startOfMonth := time.Date(y, m, 1, 0, 0, 0, 0, utcLoc) return t.Equal(startOfMonth) }
func (f FileSystem) DateFilepath(date time.Time) string { filename := fmt.Sprintf("%s_%s_%d_%d.md", date.Weekday(), date.Month().String(), date.Day(), date.Year()) path := path.Join(f.MonthDirectory(date), strings.ToLower(filename)) f.bootstrap(path) return path }
// Crawl 获取公司每天的报价 func (yahoo YahooFinance) Crawl(_market market.Market, company market.Company, date time.Time) (*market.CompanyDailyQuote, error) { // 起止时间 start := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location()) end := start.AddDate(0, 0, 1) pattern := "https://finance-yql.media.yahoo.com/v7/finance/chart/%s?period2=%d&period1=%d&interval=1m&indicators=quote&includeTimestamps=true&includePrePost=true&events=div%%7Csplit%%7Cearn&corsDomain=finance.yahoo.com" url := fmt.Sprintf(pattern, _market.YahooQueryCode(company), end.Unix(), start.Unix()) // 查询Yahoo财经接口,返回股票分时数据 str, err := net.DownloadStringRetry(url, yahoo.RetryCount(), yahoo.RetryInterval()) if err != nil { return nil, err } // 解析Json quote := &YahooQuote{} err = json.Unmarshal([]byte(str), "e) if err != nil { return nil, err } // 校验 err = yahoo.valid(quote) if err != nil { return nil, err } // 解析 return yahoo.parse(_market, company, date, quote) }
func (self Day) NextAfter(t time.Time) (time.Time, error) { desiredDay := int(self) if desiredDay == Last { if isLastDayInMonth(t) { return t.AddDate(0, 0, 1).AddDate(0, 1, -1), nil } return firstDayOfMonth(t).AddDate(0, 2, -1), nil } if t.Day() > desiredDay { if isLastDayInMonth(t) && desiredDay == First { return t.AddDate(0, 0, 1), nil } return self.NextAfter(t.AddDate(0, 0, 1)) } if t.Day() < desiredDay { totalDays := lastDayOfMonth(t).Day() if totalDays < desiredDay { return self.NextAfter(t.AddDate(0, 1, 0)) } return time.Date(t.Year(), t.Month(), desiredDay, 0, 0, 0, 0, t.Location()), nil } totalDaysNextMonth := lastDayOfMonth(lastDayOfMonth(t).AddDate(0, 0, 1)).Day() if totalDaysNextMonth < desiredDay { return self.NextAfter(t.AddDate(0, 2, -1)) } return t.AddDate(0, 1, 0), nil }
func yqlHist(symbols []string, start *time.Time, end *time.Time) (map[string]fquery.Hist, error) { if start == nil { t := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) start = &t } startq := fmt.Sprintf(` AND startDate = "%v-%v-%v"`, start.Year(), int(start.Month()), start.Day()) if end == nil { t := time.Now() end = &t } endq := fmt.Sprintf(` AND endDate = "%v-%v-%v"`, end.Year(), int(end.Month()), end.Day()) queryGen := func(symbol string) string { return fmt.Sprintf( `SELECT * FROM yahoo.finance.historicaldata WHERE symbol="%s"`, symbol) + startq + endq } makeMarshal := func() interface{} { var resp YqlJsonHistResponse return &resp } res := make(map[string]fquery.Hist) parallelFetch(queryGen, makeMarshal, addHistToMap(res), symbols) return res, nil }
// FmtDateShort returns the short date representation of 't' for 'bg_BG' func (bg *bg_BG) FmtDateShort(t time.Time) string { b := make([]byte, 0, 32) b = strconv.AppendInt(b, int64(t.Day()), 10) b = append(b, []byte{0x2e}...) if t.Month() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Month()), 10) b = append(b, []byte{0x2e}...) if t.Year() > 9 { b = append(b, strconv.Itoa(t.Year())[2:]...) } else { b = append(b, strconv.Itoa(t.Year())[1:]...) } b = append(b, []byte{0x20, 0xd0, 0xb3}...) b = append(b, []byte{0x2e}...) return string(b) }
func (expr *Expression) nextMonth(t time.Time) time.Time { // Find index at which item in list is greater or equal to // candidate month i := sort.SearchInts(expr.monthList, int(t.Month())+1) if i == len(expr.monthList) { return expr.nextYear(t) } // Month changed, need to recalculate actual days of month expr.actualDaysOfMonthList = expr.calculateActualDaysOfMonth(t.Year(), expr.monthList[i]) if len(expr.actualDaysOfMonthList) == 0 { return expr.nextMonth(time.Date( t.Year(), time.Month(expr.monthList[i]), 1, expr.hourList[0], expr.minuteList[0], expr.secondList[0], 0, t.Location())) } return time.Date( t.Year(), time.Month(expr.monthList[i]), expr.actualDaysOfMonthList[0], expr.hourList[0], expr.minuteList[0], expr.secondList[0], 0, t.Location()) }
func Strftime(format string, t time.Time) (timestamp string, err error) { c_format := C.CString(format) defer func() { C.free(unsafe.Pointer(c_format)) }() tz, offset := t.Zone() c_tz := C.CString(tz) defer func() { C.free(unsafe.Pointer(c_tz)) }() c_time := C.struct_tm{ tm_year: C.int(t.Year() - 1900), tm_mon: C.int(t.Month() - 1), tm_mday: C.int(t.Day()), tm_hour: C.int(t.Hour()), tm_min: C.int(t.Minute()), tm_sec: C.int(t.Second()), tm_gmtoff: C.long(offset), tm_zone: c_tz, } c_timestamp, trr := C.ftime(c_format, &c_time) defer func() { C.free(unsafe.Pointer(c_timestamp)) }() timestamp = C.GoString(c_timestamp) if trr == nil { timestamp = C.GoString(c_timestamp) } else { err = fmt.Errorf("%s - %s", trr, t) } return }
func ParseTime(str string) (time.Time, error) { now := time.Now() t := time.Time{} var err error isShortTime := false for _, tfmt := range inputTimeFormats { t, err = time.ParseInLocation(tfmt, str, time.Local) if err == nil { if tfmt == "03:04 pm" || tfmt == "3:04 pm" || tfmt == "15:04" { isShortTime = true } break } // fmt.Printf("%s \n", tfmt) } // if no year or month or day was given fill those in with todays date if isShortTime { t = time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local) } else if t.Year() == 0 { // no year was specified t = time.Date(now.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local) } return t, err }
func createDate(str string, now time.Time) time.Time { arr := strings.Split(str, "-") year, _ := strconv.Atoi(arr[0]) month, _ := strconv.Atoi(arr[1]) day, _ := strconv.Atoi(arr[2]) return now.AddDate(year-now.Year(), month-int(now.Month()), day-now.Day()) }
// WorkdayN reports the day of the month that corresponds to the nth workday // for the given year and month. // // The value of n affects the direction of counting: // n > 0: counting begins at the first day of the month. // n == 0: the result is always 0. // n < 0: counting begins at the end of the month. func (c *Calendar) WorkdayN(year int, month time.Month, n int) int { var date time.Time var add int if n == 0 { return 0 } if n > 0 { date = time.Date(year, month, 1, 12, 0, 0, 0, time.UTC) add = 1 } else { date = time.Date(year, month+1, 1, 12, 0, 0, 0, time.UTC).AddDate(0, 0, -1) add = -1 n = -n } ndays := 0 for ; month == date.Month(); date = date.AddDate(0, 0, add) { if c.IsWorkday(date) { ndays++ if ndays == n { return date.Day() } } } return 0 }
func TimeToString(t time.Time) string { year := t.Year() month := int(t.Month()) day := t.Day() result := strconv.Itoa(year) + "-" + strconv.Itoa(month) + "-" + strconv.Itoa(day) return result }
func buildTimeElement(t time.Time) string { return fmt.Sprintf( "<dateTime.iso8601>%d%d%dT%d:%d:%d</dateTime.iso8601>", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), ) }
// Check returns whether or not the provided time.Time is bank holiday. It ignores the time portion, checking only the date. func Check(date time.Time) bool { holsMap := getHols(date.Year()) // strip time d := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local) _, ok := holsMap[d] return ok }
func substractMonth(now time.Time) time.Time { var previousMonth time.Month year := now.Year() switch now.Month() { case time.January: previousMonth = time.December year = year - 1 case time.February: previousMonth = time.January case time.March: previousMonth = time.February case time.April: previousMonth = time.March case time.May: previousMonth = time.April case time.June: previousMonth = time.May case time.July: previousMonth = time.June case time.August: previousMonth = time.July case time.September: previousMonth = time.August case time.October: previousMonth = time.September case time.November: previousMonth = time.October case time.December: previousMonth = time.November } return time.Date(year, previousMonth, now.Day(), 12, 0, 0, 0, time.UTC) }
// Add involving the special "|" syntax. func addSpecial(f fields, date time.Time) time.Time { // move to the 1st of the appropriate month date = time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.UTC) if f.repeatUnit == "m" { date = date.AddDate(0, 1, 0) } else { date = date.AddDate(1, 0, 0) } // build up a list of candidate days in that month days := []time.Time{} for d := date; d.Month() == date.Month(); { if d.Weekday() == time.Weekday(strings.Index(weekdays, f.specialWeekday)) { days = append(days, d) } d = d.AddDate(0, 0, 1) } // return the right candidate index := atoi(f.specialSign+f.repeatAmount) - 1 if index < 0 { index += len(days) + 1 } return days[index] }
func getFetchLink(symbol string, currentDate time.Time) string { // var currentDate time.Time = time.Now() var result string = fmt.Sprintf( "http://real-chart.finance.yahoo.com/table.csv?s=%s&a=00&b=1&c=1900&d=%02d&e=%02d&f=%d&g=d&ignore=.csv", symbol, (currentDate.Month() - 1), currentDate.Day(), currentDate.Year()) return result }
// dateSize returns the size needed to store a given time.Time. func dateSize(v time.Time) (length uint8) { var ( month, day, hour, min, sec uint8 year uint16 msec uint32 ) year = uint16(v.Year()) month = uint8(v.Month()) day = uint8(v.Day()) hour = uint8(v.Hour()) min = uint8(v.Minute()) sec = uint8(v.Second()) msec = uint32(v.Nanosecond() / 1000) if hour == 0 && min == 0 && sec == 0 && msec == 0 { if year == 0 && month == 0 && day == 0 { return 0 } else { length = 4 } } else if msec == 0 { length = 7 } else { length = 11 } length++ // 1 extra byte needed to store the length itself return }
// 将时间转换为int类型(20160120,共8位) // t:时间 // 返回值: // int类型的数字 func ConvertToInt(t time.Time) int { year := int(t.Year()) month := int(t.Month()) day := int(t.Day()) return year*10e3 + month*10e1 + day }
func (self *Tracker) TrackTime(accountStr, evtType string, now time.Time) { go func(key, evtType string, now time.Time) { defer func() { if err := recover(); err != nil { log.Println("Goroutine failed:", err) } }() yearAsString := strconv.Itoa(now.Year()) monthAsString := strconv.Itoa(int(now.Month())) dayAsString := strconv.Itoa(now.Day()) hourAsString := strconv.Itoa(now.Hour()) // increment current month in year yearKey := fmt.Sprintf("track:%s:%s:%s", key, evtType, yearAsString) self.Conn.HIncrBy(yearKey, monthAsString, 1) // increment current day in month monthKey := fmt.Sprintf("track:%s:%s:%s:%s", key, evtType, yearAsString, monthAsString) self.Conn.HIncrBy(monthKey, dayAsString, 1) // increment current hour in day dayKey := fmt.Sprintf("track:%s:%s:%s:%s:%s", key, evtType, yearAsString, monthAsString, dayAsString) self.Conn.HIncrBy(dayKey, hourAsString, 1) // Expire day entry after a month // self.Conn.Expire(dayKey, 2592000) }(accountStr, evtType, now) }
// Crée un nouvel objet de type AnneesMoisJours pour la date spécifiée func TimeToAnneesMoisJour(t time.Time) (AnneesMoisJours, error) { if t.IsZero() { return AnneesMoisJours{}, ErrDateFormatInvalide } return AnneesMoisJours{t.Year(), int(t.Month()), t.Day()}, nil }
func (s *Stock) getHistory(from time.Time, to time.Time, i TimeInterval) (StockHistory, error) { url := historyBaseUrl + s.id + fromMonth + strconv.Itoa(monthsInverted[from.Month()]) + fromDay + strconv.Itoa(from.Day()) + fromYear + strconv.Itoa(from.Year()) + toMonth + strconv.Itoa(monthsInverted[to.Month()]) + toDay + strconv.Itoa(to.Day()) + toYear + strconv.Itoa(to.Year()) + interval + string(i) + endUrl // HTTP GET the CSV data resp, httpErr := http.Get(url) defer resp.Body.Close() if httpErr != nil { return nil, httpErr } // Convert string CSV data to a usable data structure reader := csv.NewReader(resp.Body) records, parseErr := reader.ReadAll() if parseErr != nil { return nil, parseErr } fmt.Println(records) // TODO prices := []int{0} hist := StockHistory{from, to, i, prices} return hist, nil }
func getDateBoundaries(per Period, start, end time.Time) []time.Time { var incMonth, incYear int var periodStart time.Time switch per { case PeriodMonth: incMonth = 1 periodStart = time.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, time.UTC) case PeriodQuarter: incMonth = 3 switch start.Month() { case time.January, time.February, time.March: periodStart = time.Date(start.Year(), time.January, 1, 0, 0, 0, 0, time.UTC) case time.April, time.May, time.June: periodStart = time.Date(start.Year(), time.April, 1, 0, 0, 0, 0, time.UTC) case time.July, time.August, time.September: periodStart = time.Date(start.Year(), time.July, 1, 0, 0, 0, 0, time.UTC) default: periodStart = time.Date(start.Year(), time.October, 1, 0, 0, 0, 0, time.UTC) } case PeriodYear: incYear = 1 periodStart = time.Date(start.Year(), time.January, 1, 0, 0, 0, 0, time.UTC) } boundaries := []time.Time{periodStart} for periodStart.Before(end) { periodStart = periodStart.AddDate(incYear, incMonth, 0) boundaries = append(boundaries, periodStart) } return boundaries }
func isGametime(t time.Time) bool { var start, end time.Time year := t.Year() month := t.Month() day := t.Day() hour := t.Hour() loc := t.Location() switch t.Weekday() { case time.Monday, time.Tuesday, time.Wednesday, time.Thursday: start = time.Date(year, month, day, 20, 0, 0, 0, loc) end = time.Date(year, month, day, 23, 59, 59, 999999999, loc) case time.Friday: start = time.Date(year, month, day, 19, 0, 0, 0, loc) end = time.Date(year, month, day+1, 2, 59, 59, 999999999, loc) case time.Saturday: if hour < 3 { start = time.Date(year, month, day-1, 23, 59, 59, 999999999, loc) end = time.Date(year, month, day, 2, 59, 59, 999999999, loc) } else { start = time.Date(year, month, day, 15, 0, 0, 0, loc) end = time.Date(year, month, day+1, 2, 59, 59, 999999999, loc) } case time.Sunday: if hour < 3 { start = time.Date(year, month, day-1, 23, 59, 59, 999999999, loc) end = time.Date(year, month, day, 2, 59, 59, 999999999, loc) } else { start = time.Date(year, month, day, 17, 0, 0, 0, loc) end = time.Date(year, month, day, 23, 59, 59, 999999999, loc) } } return (t.After(start) && t.Before(end)) }
// FmtDateShort returns the short date representation of 't' for 'it_IT' func (it *it_IT) FmtDateShort(t time.Time) string { b := make([]byte, 0, 32) if t.Day() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Day()), 10) b = append(b, []byte{0x2f}...) if t.Month() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Month()), 10) b = append(b, []byte{0x2f}...) if t.Year() > 9 { b = append(b, strconv.Itoa(t.Year())[2:]...) } else { b = append(b, strconv.Itoa(t.Year())[1:]...) } return string(b) }
// FormatDateString formats time.Time to string. func FormatDateString(t time.Time) string { return fmt.Sprintf("%d/%02d/%02d", t.Year(), t.Month(), t.Day(), ) }