本文整理汇总了Golang中Time.Time.AddDate方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.AddDate方法的具体用法?Golang Time.AddDate怎么用?Golang Time.AddDate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Time.Time
的用法示例。
在下文中一共展示了Time.AddDate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NextAfter
func (self Weekday) NextAfter(t time.Time) (time.Time, error) {
diff := int(self) - int(t.Weekday())
if diff <= 0 {
diff += 7
}
return t.AddDate(0, 0, diff), nil
}
示例2: 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
}
示例3: creatTbl
// 根据模板文件,json配置生成建表语言,并执行
func creatTbl(ctx *cli.Context) {
sqlTemp := readSqlTemplate(ctx.String("temp"))
jsonconf := prod.NewJsonConf(ctx.String("json"))
for _, conf := range jsonconf.Log_list {
table := conf.Table
// 新加的表的建表语句,直接由配置文件生成
if _, ok := sqlTemp[table]; !ok {
sqlTemp[table] = createSql(&conf)
log.Printf("INF: table=%s", table)
}
}
var start time.Time
var err error
if ctx.String("milestone") == "" {
start = time.Now().AddDate(0, 0, 1)
} else {
start, err = time.Parse("20060102", ctx.String("milestone"))
if err != nil {
log.Panicf("ERR: %s", err)
}
}
days := ctx.Int("days")
if days < 1 {
log.Panicf("ERR: arg 'days' invalid, %d", days)
}
end := start.AddDate(0, 0, days)
dates := dateSlice(start, end)
log.Printf("INF: dates, %s", dates)
DbExecute(jsonconf, dates, sqlTemp)
}
示例4: LoadDescriptorFromDigest
// LoadDescriptorFromDigest takes as input the descriptor directory, a
// descriptor's digest, and the date the digest was created. It then attempts
// to parse and return the descriptor referenced by the digest. The descriptor
// directory expects to contain CollecTor server descriptor archives such as:
// server-descriptors-2015-03/
// server-descriptors-2015-04/
// ...
func LoadDescriptorFromDigest(descriptorDir, digest string, date time.Time) (*RouterDescriptor, error) {
topDir := fmt.Sprintf("server-descriptors-%s", date.Format("2006-01"))
prevTopDir := fmt.Sprintf("server-descriptors-%s", date.AddDate(0, -1, 0).Format("2006-01"))
fileName := filepath.Join(descriptorDir, topDir, digest[0:1], digest[1:2], digest)
// If we cannot find the descriptor file, go one month back in time.
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fileName = filepath.Join(descriptorDir, prevTopDir, digest[0:1], digest[1:2], digest)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return nil, fmt.Errorf("Could not find digest file %s in %s", digest, descriptorDir)
}
}
descs, err := ParseDescriptorFile(fileName)
if err != nil {
return nil, err
}
if descs.Length() != 1 {
return nil, fmt.Errorf("More than one descriptor in digest file %s. Bug?", fileName)
}
var d *RouterDescriptor
for _, getDesc := range descs.RouterDescriptors {
d = getDesc()
break
}
return d, nil
}
示例5: dateArgs
func (rp *rangeParams) dateArgs() (bool, time.Time, time.Time) {
var truthy bool = true
var falsey bool = false
var t1, t2 time.Time
var prec string
if rp.min != nil {
t1, prec = parseTime(*rp.min, rp.loc)
switch prec {
case "day":
if !*rp.minInclusive {
// add 1 day and make inclusive
t1 = t1.AddDate(0, 0, 1)
rp.minInclusive = &truthy
}
default:
return false, t1, t2
}
}
if rp.max != nil {
t2, prec = parseTime(*rp.max, rp.loc)
switch prec {
case "day":
if *rp.maxInclusive {
// add 1 day and change to exclusive
t2 = t2.AddDate(0, 0, 1)
rp.maxInclusive = &falsey
}
default:
return false, t1, t2
}
}
return true, t1, t2
}
示例6: createDate
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())
}
示例7: EndOf
// EndOf returns the end of the passed unit for the given time.
func EndOf(t time.Time, unit UnitOfTime) time.Time {
// Retrieve the individual parts of the given time.
year := t.Year()
month := t.Month()
day := t.Day()
hour := t.Hour()
minute := t.Minute()
second := t.Second()
loc := t.Location()
// Build new time.
switch unit {
case Second:
return time.Date(year, month, day, hour, minute, second, 999999999, loc)
case Minute:
return time.Date(year, month, day, hour, minute, 59, 999999999, loc)
case Hour:
return time.Date(year, month, day, hour, 59, 59, 999999999, loc)
case Day:
return time.Date(year, month, day, 23, 59, 59, 999999999, loc)
case Month:
// Catching leap years makes the month a bit more complex.
_, nextMonth, _ := t.AddDate(0, 1, 0).Date()
return time.Date(year, nextMonth, 1, 23, 59, 59, 999999999, loc).AddDate(0, 0, -1)
case Year:
return time.Date(year, time.December, 31, 23, 59, 59, 999999999, loc)
default:
return t
}
}
示例8: addSpecial
// 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]
}
示例9: startOfNextMonth
func startOfNextMonth(original time.Time) time.Time {
next := original.AddDate(0, 1, -original.Day()+1) // the first of the next month
clockTimeToRemove := time.Duration(original.Hour()) * time.Hour
clockTimeToRemove += time.Duration(original.Minute()) * time.Minute
clockTimeToRemove += time.Duration(original.Second()) * time.Second
return next.Add(-clockTimeToRemove)
}
示例10: offset
func (oc OffsetContext) offset(origin time.Time) (time.Time, error) {
var days, months, years int
switch oc.interval {
case INTERVAL_WEEK:
days = oc.count * 7
case INTERVAL_DAY:
days = oc.count
case INTERVAL_MONTH:
months = oc.count
case INTERVAL_YEAR:
years = oc.count
}
var direction int
switch oc.direction {
case DIRECTION_LEFT:
direction = -1
case DIRECTION_RIGHT:
direction = 1
case DIRECTION_CURRENT:
direction = 0
}
d := origin.AddDate(direction*years, direction*months, direction*days)
return d, nil
}
示例11: WorkdayN
// 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
}
示例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: makeISOTimeArray
func makeISOTimeArray(startDate time.Time, endDate time.Time) []stock.ISOTime {
var timeArray []stock.ISOTime
// Make sure startDate and endDate are in 2011/2012, those are the only years
// that we have holiday information for. If not, return empy.
if !(startDate.Year() == 2011 || startDate.Year() == 2012) && (endDate.Year() == 2011 || endDate.Year() == 2012) {
return timeArray
}
var day stock.ISOTime = stock.ISOTime{startDate.AddDate(0, 0, -1)}
for day.Before(endDate) {
day = stock.ISOTime{day.AddDate(0, 0, 1)}
// market is not open on Saturday or Sunday
if day.Weekday() == time.Weekday(0) ||
day.Weekday() == time.Weekday(6) {
continue
}
// market is not open on certain holidays
isHoliday := false
for _, holiday := range Holidays20112012 {
if day.Year() == holiday.Year() && day.YearDay() == holiday.YearDay() {
isHoliday = true
break
}
}
if !isHoliday {
timeArray = append(timeArray, day)
}
}
return timeArray
}
示例14: CleanData
func (b *BaseData) CleanData(t time.Time) {
daysAgo20 := t.AddDate(0, 0, -20)
err := db.Remove(DB, C_REQUEST, bson.M{"time": bson.M{"$lt": daysAgo20}})
if err != nil && !strings.Contains(err.Error(), "not found") {
log.Error(err)
}
}
示例15: startOfNextDay
func startOfNextDay(original time.Time) time.Time {
next := original.AddDate(0, 0, 1)
clockTimeToRemove := time.Duration(original.Hour()) * time.Hour
clockTimeToRemove += time.Duration(original.Minute()) * time.Minute
clockTimeToRemove += time.Duration(original.Second()) * time.Second
return next.Add(-clockTimeToRemove)
}