Пример #1
1
// FormatTimestamp formats t into Postgres' text format for timestamps.
func FormatTimestamp(t time.Time) []byte {
	// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
	// minus sign preferred by Go.
	// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
	bc := false
	if t.Year() <= 0 {
		// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
		t = t.AddDate((-t.Year())*2+1, 0, 0)
		bc = true
	}
	b := []byte(t.Format(time.RFC3339Nano))

	_, offset := t.Zone()
	offset = offset % 60
	if offset != 0 {
		// RFC3339Nano already printed the minus sign
		if offset < 0 {
			offset = -offset
		}

		b = append(b, ':')
		if offset < 10 {
			b = append(b, '0')
		}
		b = strconv.AppendInt(b, int64(offset), 10)
	}

	if bc {
		b = append(b, " BC"...)
	}
	return b
}
Пример #2
0
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
}
Пример #3
0
// 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
}
Пример #4
0
// 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
}
Пример #5
0
// 将时间转换为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
}
Пример #6
0
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
}
Пример #7
0
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)
}
Пример #8
0
// 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()
	}
}
Пример #9
0
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
}
Пример #10
0
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
}
Пример #11
0
func (expr *Expression) nextYear(t time.Time) time.Time {
	// Find index at which item in list is greater or equal to
	// candidate year
	i := sort.SearchInts(expr.yearList, t.Year()+1)
	if i == len(expr.yearList) {
		return time.Time{}
	}
	// Year changed, need to recalculate actual days of month
	expr.actualDaysOfMonthList = expr.calculateActualDaysOfMonth(expr.yearList[i], expr.monthList[0])
	if len(expr.actualDaysOfMonthList) == 0 {
		return expr.nextMonth(time.Date(
			expr.yearList[i],
			time.Month(expr.monthList[0]),
			1,
			expr.hourList[0],
			expr.minuteList[0],
			expr.secondList[0],
			0,
			t.Location()))
	}
	return time.Date(
		expr.yearList[i],
		time.Month(expr.monthList[0]),
		expr.actualDaysOfMonthList[0],
		expr.hourList[0],
		expr.minuteList[0],
		expr.secondList[0],
		0,
		t.Location())
}
Пример #12
0
// WriteFormattedTime formats t into a format postgres understands.
// Taken with gratitude from pq: https://github.com/lib/pq/blob/b269bd035a727d6c1081f76e7a239a1b00674c40/encode.go#L403
func (pd *Postgres) WriteFormattedTime(buf common.BufferWriter, t time.Time) {
	buf.WriteRune('\'')
	defer buf.WriteRune('\'')
	// XXX: This doesn't currently deal with infinity values

	// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
	// minus sign preferred by Go.
	// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
	bc := false
	if t.Year() <= 0 {
		// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
		t = t.AddDate((-t.Year())*2+1, 0, 0)
		bc = true
	}
	buf.WriteString(t.Format(time.RFC3339Nano))

	_, offset := t.Zone()
	offset = offset % 60
	if offset != 0 {
		// RFC3339Nano already printed the minus sign
		if offset < 0 {
			offset = -offset
		}

		buf.WriteRune(':')
		if offset < 10 {
			buf.WriteRune('0')
		}
		buf.WriteString(strconv.FormatInt(int64(offset), 10))
	}

	if bc {
		buf.WriteString(" BC")
	}
}
Пример #13
0
// Checks if the date of the file is from a prior year, and if so print the year, else print
// only the hour and minute.
func dateFormatCheck(fileModTime time.Time) string {
	if fileModTime.Year() != time.Now().Year() {
		return fileModTime.Format(DATE_YEAR_FORMAT)
	} else {
		return fileModTime.Format(DATE_FORMAT)
	}
}
Пример #14
0
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
}
Пример #15
0
// Returns human time since ( Example 3 weeks ago or 11 hours ago)
func timesince(anything interface{}) string {
	var date string
	var t time.Time
	switch reflect.TypeOf(anything).Kind() {
	case reflect.String:
		date = anything.(string)
		const longForm = "Jan 2, 2006 at 3:04pm (MST)"
		t, err = time.Parse(longForm, date)
		if err != nil {
			log.Println(err)
			return "Unknown"
		}

	default:
		t = anything.(time.Time)

	}

	since := time.Since(t)
	const year = 365 * time.Hour * 24

	// Years ago
	if t.Year() != time.Now().Year() {
		str := strconv.Itoa(time.Now().Year() - t.Year())
		if str == "1" {
			return str + " year ago"
		}
		return str + " years ago"

	}

	return humanize(since)

}
Пример #16
0
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
}
Пример #17
0
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))
}
Пример #18
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
}
Пример #19
0
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(),
	)
}
Пример #20
0
// 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
}
Пример #21
0
// 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), &quote)
	if err != nil {
		return nil, err
	}

	// 校验
	err = yahoo.valid(quote)
	if err != nil {
		return nil, err
	}

	// 解析
	return yahoo.parse(_market, company, date, quote)
}
Пример #22
0
// Checks if the given date is at Palm Sunday (the Sunday before easter)
func atPalmSunday(date time.Time) bool {
	easter := EasterDay(date.Year())
	// There will always be a sunday before easter for any given year,
	// so we don't need to check the value of err
	palmSunday, _ := searchBackwardsForSunday(easter)
	return atDate(date, palmSunday)
}
Пример #23
0
// 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)
}
Пример #24
0
// 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]
}
Пример #25
0
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)
}
Пример #26
0
// 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
}
Пример #27
0
func yearForMonth(mo time.Month, now *time.Time) int {
	year := now.Year()
	if mo+6 <= now.Month() {
		year += 1
	}
	return year
}
Пример #28
0
// formatTs formats t with an optional offset into a format lib/pq understands,
// appending to the provided tmp buffer and reallocating if needed. The function
// will then return the resulting buffer. formatTs is mostly cribbed from
// github.com/lib/pq.
func formatTs(t time.Time, offset *time.Location, tmp []byte) (b []byte) {
	// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
	// minus sign preferred by Go.
	// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
	if offset != nil {
		t = t.In(offset)
	} else {
		t = t.UTC()
	}
	bc := false
	if t.Year() <= 0 {
		// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
		t = t.AddDate((-t.Year())*2+1, 0, 0)
		bc = true
	}

	if offset != nil {
		b = t.AppendFormat(tmp, pgTimeStampFormat)
	} else {
		b = t.AppendFormat(tmp, pgTimeStampFormatNoOffset)
	}

	if bc {
		b = append(b, " BC"...)
	}
	return b
}
Пример #29
0
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)
}
Пример #30
0
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())
}