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()) }
func (de *DateEdit) SetRange(min, max time.Time) error { if !min.IsZero() && !max.IsZero() { if min.Year() > max.Year() || min.Year() == max.Year() && min.Month() > max.Month() || min.Year() == max.Year() && min.Month() == max.Month() && min.Day() > max.Day() { return newError("invalid range") } } var st [2]win.SYSTEMTIME var wParam uintptr if !min.IsZero() { wParam |= win.GDTR_MIN st[0] = *de.timeToSystemTime(min) } if !max.IsZero() { wParam |= win.GDTR_MAX st[1] = *de.timeToSystemTime(max) } if 0 == de.SendMessage(win.DTM_SETRANGE, wParam, uintptr(unsafe.Pointer(&st[0]))) { return newError("SendMessage(DTM_SETRANGE)") } return nil }
// 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) }
// 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 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) }
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 }
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 }
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 }
// Implement Schedule interface. func (self Day) IsOccurring(t time.Time) bool { if self := int(self); self == Last { return isLastDayInMonth(t) } else { return self == t.Day() } }
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 }
// 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 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 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 (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 }
// 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 }
// 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 *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) }
// createLogFile 创建日志文件 func (this *FileWriter) createLogFile(date time.Time) error { var day = date.Day() if day == this.day { //文件无需更新 return nil } //关闭原来的日志文件,并创建新的日志文件 if this.file != nil { err := this.file.Close() if err != nil { return err } } //创建新的日志文件 var dir = date.Format("200601") var path = filepath.Join(this.path, dir) var err = os.MkdirAll(path, 0770) if err != nil { return err } var fileName = date.Format("20060102") + ".log" var filePath = filepath.Join(path, fileName) file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660) if err != nil && !os.IsExist(err) { return err } this.file = file this.writer = bufio.NewWriter(file) return 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 }
// 将时间转换为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 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)) }
// delta returns the elapsed time since the last event or the trace start, // and whether it spans midnight. // L >= tr.mu func (tr *trace) delta(t time.Time) (time.Duration, bool) { if len(tr.events) == 0 { return t.Sub(tr.Start), false } prev := tr.events[len(tr.events)-1].When return t.Sub(prev), prev.Day() != t.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 (fs *fileStorage) printDay(t time.Time) { if fs.day == t.Day() { return } fs.day = t.Day() fs.logw.Write([]byte("=============== " + t.Format("Jan 2, 2006 (MST)") + " ===============\n")) }
// IsWeekdayN reports whether the given date is the nth occurrence of the // day in the 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 false. // n < 0: counting begins at the end of the month. func IsWeekdayN(date time.Time, day time.Weekday, n int) bool { cday := date.Weekday() if cday != day || n == 0 { return false } if n > 0 { return (date.Day()-1)/7 == (n - 1) } else { n = -n last := time.Date(date.Year(), date.Month()+1, 1, 12, 0, 0, 0, date.Location()) lastCount := 0 for { last = last.AddDate(0, 0, -1) if last.Weekday() == day { lastCount++ } if lastCount == n || last.Month() != date.Month() { break } } return lastCount == n && last.Month() == date.Month() && last.Day() == date.Day() } }
// 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) }
// 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) }