本文整理汇总了Golang中Time.Time.Year方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.Year方法的具体用法?Golang Time.Year怎么用?Golang Time.Year使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Time.Time
的用法示例。
在下文中一共展示了Time.Year方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FormatTimestamp
// 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: ParseTime
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: IsActiveAt
// 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: TimeToAnneesMoisJour
// 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: ConvertToInt
// 将时间转换为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: getFetchLink
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: substractMonth
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: IsWeekdayN
// 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: yqlHist
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: NextAfter
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: nextYear
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: WriteFormattedTime
// 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: dateFormatCheck
// 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: Strftime
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: timesince
// 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)
}