本文整理汇总了Golang中Time.Time.Weekday方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.Weekday方法的具体用法?Golang Time.Weekday怎么用?Golang Time.Weekday使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Time.Time
的用法示例。
在下文中一共展示了Time.Weekday方法的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: main
func main() {
log.Print("Started running timer")
var numberOfHours int
var message string
isWeekend := false
var currentTime time.Time
for {
numberOfHours += 1
currentTime = time.Now()
today := currentTime.Weekday()
hour := currentTime.Hour()
isWeekend = today == time.Sunday || today == time.Friday || today == time.Saturday
switch {
case (hour >= 22 || hour <= 3) && !isWeekend:
message = "Late night on a weekday. Sleep!."
case numberOfHours == 1:
message = "One hour! Take a break"
case numberOfHours == 2:
message = "Two hours! Take a break now."
case numberOfHours > 2: // or default:
message = "Take a break now. Seriously."
}
log.Print("Sleeping program for one hour")
time.Sleep(1 * time.Hour)
showNotification(message)
}
}
示例3: SubmitScheduledJob
// Just like SUBMIT_JOB_BG, but run job at given time instead of
// immediately. This is not currently used and may be removed.
//
// Arguments:
// - NULL byte terminated function name.
// - NULL byte terminated unique ID.
// - NULL byte terminated minute (0-59).
// - NULL byte terminated hour (0-23).
// - NULL byte terminated day of month (1-31).
// - NULL byte terminated month (1-12).
// - NULL byte terminated day of week (0-6, 0 = Monday).
// - Opaque data that is given to the function as an argument.
func (c *Client) SubmitScheduledJob(func_name, uniq string, t time.Time, data []byte) error {
weekday := int(t.Weekday())
// go week day number starts at 0 on Sunday,
// gearman starts at 0 on Monday
if weekday == 0 {
weekday = 7
}
weekday -= 1
buf := new(bytes.Buffer)
buf.WriteString(func_name)
buf.WriteByte(0)
buf.WriteString(uniq)
buf.WriteByte(0)
buf.WriteString(strconv.Itoa(t.Minute()))
buf.WriteByte(0)
buf.WriteString(strconv.Itoa(t.Hour()))
buf.WriteByte(0)
buf.WriteString(strconv.Itoa(t.Day()))
buf.WriteByte(0)
buf.WriteString(strconv.Itoa(int(t.Month())))
buf.WriteByte(0)
buf.WriteString(strconv.Itoa(weekday))
buf.WriteByte(0)
buf.Write(data)
return c.server.Send(CS_SUBMIT_JOB_EPOCH, buf.Bytes())
}
示例4: 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
}
示例5: ValidOn
// func (c *Calendar) ValidOn(date string) bool {
// if stringDayDateComp(c.StartDate, date) <= 0 && stringDayDateComp(c.EndDate, date) >= 0 {
func (c *Calendar) ValidOn(intday int, t *time.Time) bool {
if c.StartDate <= intday && c.EndDate >= intday {
switch t.Weekday() {
case time.Monday:
return c.Monday
case time.Tuesday:
return c.Tuesday
case time.Wednesday:
return c.Wednesday
case time.Thursday:
return c.Thursday
case time.Friday:
return c.Friday
case time.Saturday:
return c.Saturday
case time.Sunday:
return c.Sunday
default:
return false
}
}
return false
}
示例6: GetBetween
func (source mondayAfternoons) GetBetween(start, end time.Time) ([]book.Booking, error) {
// find next monday
y, m, d := start.Date()
daysUntilMonday := int((7 + time.Monday - start.Weekday()) % 7)
nextmonday := time.Date(y, m, d+daysUntilMonday, 0, 0, 0, 0, start.Location())
// build a map of booked dates
bookedSet := make(map[time.Time]bool)
if booked, err := source.session.List(); err != nil {
return nil, err
} else {
for _, appointment := range booked {
y, m, d = appointment.Timestamp.Date()
date := time.Date(y, m, d, 0, 0, 0, 0, start.Location())
bookedSet[date] = true
}
}
// add a week at a time
var bookings []book.Booking
for !nextmonday.After(end) {
// take days where nothing has been booked yet
if _, exists := bookedSet[nextmonday]; !exists {
bookings = append(bookings, lizafternoon{nextmonday})
}
nextmonday = nextmonday.AddDate(0, 0, 7)
}
return bookings, nil
}
示例7: repl
// repl replaces % directives with right time, will panic on unknown directive
func repl(match string, t time.Time) string {
if match == "%%" {
return "%"
}
format, ok := conv[match]
if ok {
return t.Format(format)
}
switch match {
case "%j":
start := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.UTC)
day := int(t.Sub(start).Hours()/24) + 1
return fmt.Sprintf("%03d", day)
case "%w":
return fmt.Sprintf("%d", t.Weekday())
case "%W", "%U":
start := time.Date(t.Year(), time.January, 1, 23, 0, 0, 0, time.UTC)
week := 0
for start.Before(t) {
week += 1
start = start.Add(WEEK)
}
return fmt.Sprintf("%02d", week)
}
panic(fmt.Errorf("unknown directive - %s", match))
return "" // Make compiler happy
}
示例8: Times
// Times returns the start and stop time for
// the closest Day translation to now.
func (d *Day) Times(now time.Time) (closestStart time.Time, closestStop time.Time) {
if now.IsZero() {
Log.Error("Cannot find times without reference")
return
}
if loc, err := d.GetLocation(); loc != nil && err == nil {
now = now.In(loc)
}
// find nearest start time of this slot
if now.Weekday() == d.Day && todayAt(now, d.Start).Before(now) {
// if the schedule is for today
// and we are after that scheduled time,
// then we have found the closest start
closestStart = todayAt(now, d.Start)
} else {
// Step back a day at a time until we find
// the most recent day that matches the day
// of the week of our Day.
for i := 1; i < 8; i++ {
sTime := now.Add(-time.Duration(i) * 24 * time.Hour)
if sTime.Weekday() == d.Day {
closestStart = todayAt(sTime, d.Start)
break
}
}
}
// closestStop is just the closests start plus
// the duration
closestStop = closestStart.Add(d.Duration)
return
}
示例9: calculateNextRollTime
// given a time, calculate the instant that the log should next roll
func calculateNextRollTime(t time.Time, freq rollFrequency) time.Time {
if freq == RollMinutely {
t = t.Truncate(time.Minute)
t = t.Add(time.Minute)
} else if freq == RollHourly {
t = t.Truncate(time.Hour)
t = t.Add(time.Hour)
} else {
t = t.Truncate(time.Hour)
// easiest way to beat DST bugs is to just iterate
for t.Hour() > 0 {
t = t.Add(-time.Hour)
}
if freq == RollDaily {
t = t.AddDate(0, 0, 1)
} else {
if t.Weekday() == time.Sunday {
t = t.AddDate(0, 0, 7)
} else {
for t.Weekday() != time.Sunday {
t = t.AddDate(0, 0, 1)
}
}
}
}
return t
}
示例10: isGametime
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))
}
示例11: getDateString
func getDateString(d time.Time) string {
if d.IsZero() {
return "Date TBC"
}
year, month, day := d.Date()
suffix := "th"
switch day % 10 {
case 1:
if day%100 != 11 {
suffix = "st"
}
case 2:
if day%100 != 12 {
suffix = "nd"
}
case 3:
if day%100 != 13 {
suffix = "rd"
}
}
return d.Weekday().String() + " " + strconv.Itoa(day) + suffix + " " +
month.String() + " " + strconv.Itoa(year)
}
示例12: FmtDateFull
// 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)
}
示例13: DateFilepath
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
}
示例14: 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()
}
}
示例15: IsWorkday
// IsWorkday reports whether a given date is a work day (business day).
func (c *Calendar) IsWorkday(date time.Time) bool {
if IsWeekend(date) || c.IsHoliday(date) {
return false
}
if c.Observed == ObservedExact {
return true
}
day := date.Weekday()
if c.Observed == ObservedMonday && day == time.Monday {
sun := date.AddDate(0, 0, -1)
sat := date.AddDate(0, 0, -2)
return !c.IsHoliday(sat) && !c.IsHoliday(sun)
} else if c.Observed == ObservedNearest {
if day == time.Friday {
sat := date.AddDate(0, 0, 1)
return !c.IsHoliday(sat)
} else if day == time.Monday {
sun := date.AddDate(0, 0, -1)
return !c.IsHoliday(sun)
}
}
return true
}