func parseDateTime(str string, loc *time.Location) (time.Time, error) { var t time.Time var err error base := "0000-00-00 00:00:00.0000000" switch len(str) { case 10, 19, 21, 22, 23, 24, 25, 26: if str == base[:len(str)] { return t, err } t, err = time.Parse(timeFormat[:len(str)], str) default: err = ErrInvalidTimestring return t, err } // Adjust location if err == nil && loc != time.UTC { y, mo, d := t.Date() h, mi, s := t.Clock() t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil } return t, err }
func (w *Writer) writeTime(v interface{}, t time.Time) (err error) { w.setRef(v) s := w.Stream year, month, day := t.Date() hour, min, sec := t.Clock() nsec := t.Nanosecond() tag := TagSemicolon if t.Location() == time.UTC { tag = TagUTC } if hour == 0 && min == 0 && sec == 0 && nsec == 0 { if _, err = s.Write(formatDate(year, int(month), day)); err == nil { err = s.WriteByte(tag) } } else if year == 1970 && month == 1 && day == 1 { if _, err = s.Write(formatTime(hour, min, sec, nsec)); err == nil { err = s.WriteByte(tag) } } else if _, err = s.Write(formatDate(year, int(month), day)); err == nil { if _, err = s.Write(formatTime(hour, min, sec, nsec)); err == nil { err = s.WriteByte(tag) } } return err }
func getEpisodes() { var episodes []entity.Episode var t time.Time t = time.Now() h, _, _ := t.Clock() resp, _ := http.Get("http://api.tvmaze.com/schedule?country=US&date=2014-12-01") defer resp.Body.Close() json.NewDecoder(resp.Body).Decode(&episodes) for _, episode := range episodes { timeSlice := strings.Split(episode.Airtime, ":") episodeHour, _ := strconv.Atoi(timeSlice[0]) if episodeHour < (h-1) || episodeHour > (h) { continue } if episode.Show != nil { fmt.Println(episode.Show.Name) if episode.Show.Network != nil { fmt.Println(episode.Show.Network.Name) } } fmt.Println(episode.Name) fmt.Println("Airtime: ", episode.Airtime) fmt.Println("Episode: ", episode.Number) fmt.Println("Season: ", episode.Season) fmt.Println("") } }
func getTimeByRangeType(myTime *time.Time, rangeType dataRangeType) (beginTime time.Time, endTime time.Time) { h, m, s := myTime.Clock() y, M, d := myTime.Date() wd := myTime.Weekday() l := myTime.Location() var bh, bm, bs, eh, em, es = h, m, s, h, m, s var by, bM, bd, ey, eM, ed = y, M, d, y, M, d switch rangeType { case PerMinute: bh, bm, bs = h, m, 0 eh, em, es = h, m, 59 by, bM, bd = y, M, d ey, eM, ed = y, M, d case PerHour: bh, bm, bs = h, 0, 0 eh, em, es = h, 59, 59 by, bM, bd = y, M, d ey, eM, ed = y, M, d case PerDay: bh, bm, bs = 0, 0, 0 eh, em, es = 23, 59, 59 by, bM, bd = y, M, d ey, eM, ed = y, M, d case PerWeek: bh, bm, bs = 0, 0, 0 eh, em, es = 23, 59, 59 by, bM, bd = y, M, d-int(time.Sunday+wd) ey, eM, ed = y, M, d+int(time.Saturday-wd) case PerMonth: bh, bm, bs = 0, 0, 0 eh, em, es = 23, 59, 59 by, bM, bd = y, M, 1 ey, eM, ed = y, M+1, 0 case PerSeason: bh, bm, bs = 0, 0, 0 eh, em, es = 23, 59, 59 switch { case M >= time.January && M <= time.March: by, bM, bd = y, 1, 1 case M >= time.April && M <= time.June: by, bM, bd = y, 4, 1 case M >= time.July && M <= time.September: by, bM, bd = y, 7, 1 case M >= time.October && M <= time.December: by, bM, bd = y, 10, 1 } ey, eM, ed = y, bM+3, 0 case PerYear: bh, bm, bs = 0, 0, 0 eh, em, es = 23, 59, 59 by, bM, bd = y, 1, 1 ey, eM, ed = y, 12, 31 } bt := time.Date(by, bM, bd, bh, bm, bs, 0, l) et := time.Date(ey, eM, ed, eh, em, es, 999999999, l) return bt, et }
func appendTimeCommon(dst []byte, t time.Time) []byte { _, month, day := t.Date() dst = appendTwoDigits(dst, int(month)) dst = appendTwoDigits(dst, day) hour, min, sec := t.Clock() dst = appendTwoDigits(dst, hour) dst = appendTwoDigits(dst, min) dst = appendTwoDigits(dst, sec) _, offset := t.Zone() switch { case offset/60 == 0: return append(dst, 'Z') case offset > 0: dst = append(dst, '+') case offset < 0: dst = append(dst, '-') } offsetMinutes := offset / 60 if offsetMinutes < 0 { offsetMinutes = -offsetMinutes } dst = appendTwoDigits(dst, offsetMinutes/60) dst = appendTwoDigits(dst, offsetMinutes%60) return dst }
func ParseTime(str string, rel time.Time) time.Time { if t, ok := tryAll(str, rel, datetimeFormats); ok { return t } var dt, tt time.Time var dok, tok bool if strings.Index(str, " ") > -1 { parts := strings.SplitN(str, " ", 2) dt, dok = tryAll(parts[0], rel, dateFormats) tt, tok = tryAll(parts[1], rel, timeFormats) } if !dok || !tok { dt, dok = tryAll(str, rel, dateFormats) tt, tok = tryAll(str, rel, timeFormats) } if !dok && !tok { return time.Time{} } y, mo, d := dt.Date() if y == 0 { y, _, _ = rel.Date() } h, m, s := tt.Clock() return time.Date(y, mo, d, h, m, s, 0, rel.Location()) }
func (fs *fileStorage) doLog(t time.Time, str string) { if fs.logSize > logSizeThreshold { // Rotate log file. fs.logw.Close() fs.logw = nil fs.logSize = 0 rename(filepath.Join(fs.path, "LOG"), filepath.Join(fs.path, "LOG.old")) } if fs.logw == nil { var err error fs.logw, err = os.OpenFile(filepath.Join(fs.path, "LOG"), os.O_WRONLY|os.O_CREATE, 0644) if err != nil { return } // Force printDay on new log file. fs.day = 0 } fs.printDay(t) hour, min, sec := t.Clock() msec := t.Nanosecond() / 1e3 // time fs.buf = itoa(fs.buf[:0], hour, 2) fs.buf = append(fs.buf, ':') fs.buf = itoa(fs.buf, min, 2) fs.buf = append(fs.buf, ':') fs.buf = itoa(fs.buf, sec, 2) fs.buf = append(fs.buf, '.') fs.buf = itoa(fs.buf, msec, 6) fs.buf = append(fs.buf, ' ') // write fs.buf = append(fs.buf, []byte(str)...) fs.buf = append(fs.buf, '\n') fs.logw.Write(fs.buf) }
func (l *Logger) formatHeader(t time.Time) string { var s string //*buf = append(*buf, l.prefix...) if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flag&Ldate != 0 { year, month, day := t.Date() s += itoa(year, 4) s += "/" s += itoa(int(month), 2) s += "/" s += itoa(day, 2) } if l.flag&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() s += " " s += itoa(hour, 2) s += ":" s += itoa(min, 2) s += ":" s += itoa(sec, 2) if l.flag&Lmicroseconds != 0 { s += "." s += itoa(t.Nanosecond()/1e3, 6) } } s += " " } return s }
func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int, lvl int, reqId string, Func string) { if l.prefix != "" { buf.WriteString(l.prefix) } if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) buf.WriteByte('/') itoa(buf, int(month), 2) buf.WriteByte('/') itoa(buf, day, 2) buf.WriteByte(' ') } if l.flag&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() itoa(buf, hour, 2) buf.WriteByte(':') itoa(buf, min, 2) buf.WriteByte(':') itoa(buf, sec, 2) if l.flag&Lmicroseconds != 0 { buf.WriteByte('.') itoa(buf, t.Nanosecond()/1e3, 6) } buf.WriteByte(' ') } } if reqId != "" { buf.WriteByte('[') buf.WriteString(reqId) buf.WriteByte(']') } if l.flag&Llevel != 0 { buf.WriteString(levels[lvl]) } if l.flag&Lmodule != 0 { buf.WriteByte('[') buf.WriteString(Func) buf.WriteByte(']') buf.WriteByte(' ') } if l.flag&(Lshortfile|Llongfile) != 0 { if l.flag&Lshortfile != 0 { short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } file = short } buf.WriteString(file) buf.WriteByte(':') itoa(buf, line, -1) buf.WriteString(": ") } }
func (w *Logger) logOneLine(asof time.Time, text, port string) { // figure out name of logfile based on UTC date, with daily rotation year, month, day := asof.Date() path := fmt.Sprintf("%s/%d", w.dir, year) err := os.MkdirAll(path, os.ModePerm) flow.Check(err) // e.g. "./logger/2014/20140122.txt" datePath := fmt.Sprintf("%s/%d.txt", path, (year*100+int(month))*100+day) if w.fd == nil || datePath != w.fd.Name() { if w.fd != nil { name := w.fd.Name() w.fd.Close() w.Out.Send(name) // report the closed file } mode := os.O_WRONLY | os.O_APPEND | os.O_CREATE fd, err := os.OpenFile(datePath, mode, os.ModePerm) flow.Check(err) w.fd = fd } // append a new log entry, here is an example of the format used: // L 01:02:03.537 usb-A40117UK OK 9 25 54 66 235 61 210 226 33 19 hour, min, sec := asof.Clock() line := fmt.Sprintf("L %02d:%02d:%02d.%03d %s %s\n", hour, min, sec, jeebus.TimeToMs(asof)%1000, port, text) w.fd.WriteString(line) }
// createFile creates log file with specified timestamp. // l.mu held func (l *Logger) createFile(t time.Time) error { year, month, day := t.Date() hour, min, sec := t.Clock() var file string switch l.options.Mode { case R_Size: file = fmt.Sprintf("%s-%04d%02d%02d-%02d%02d%02d", l.options.File, year, month, day, hour, min, sec) case R_Hour: file = fmt.Sprintf("%s-%04d%02d%02d-%02d", l.options.File, year, month, day, hour) case R_Day: file = fmt.Sprintf("%s-%04d%02d%02d", l.options.File, year, month, day) default: // R_None file = l.options.File } if l.file != nil { l.out.Flush() l.file.Close() } f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664) if err != nil { return err } l.file = f l.out = bufio.NewWriterSize(f, bufferSize) l.nbytes = 0 l.hour = hour l.day = day return nil }
func (log *Logger) date(now time.Time) { hour, minute, second := now.Clock() year, month, day := now.Date() //nsec := now.Nanosecond() itoa(&log.buf, year, 4) log.buf = append(log.buf, '-') itoa(&log.buf, int(month), 2) log.buf = append(log.buf, '-') itoa(&log.buf, day, 2) log.buf = append(log.buf, 'T') itoa(&log.buf, hour, 2) log.buf = append(log.buf, ':') itoa(&log.buf, minute, 2) log.buf = append(log.buf, ':') itoa(&log.buf, second, 2) //log.buf = append(log.buf, '.') //itoa(&log.buf, nsec, 9) _, off := now.Zone() if off == 0 { log.buf = append(log.buf, 'Z') } else { zone := off / 60 absoff := off if zone < 0 { log.buf = append(log.buf, '-') absoff = -absoff zone = -zone } else { log.buf = append(log.buf, '+') } itoa(&log.buf, zone/60, 2) log.buf = append(log.buf, ':') itoa(&log.buf, zone%60, 2) } }
// Trasforma la variabile time passata in ingresso in una stringa ordinata e leggibile func getTimeString(t time.Time) (date, clock string) { hour, min, sec := t.Clock() year, month, day := t.Date() clock = strings.Join([]string{strconv.Itoa(hour), strconv.Itoa(min), strconv.Itoa(sec)}, ".") date = strings.Join([]string{strconv.Itoa(day), month.String(), strconv.Itoa(year)}, "-") return }
func (l *Logger) formatPrefix(level int, t time.Time, file string, line int) { // prefix: "D0806 10:45:19.598 bvc.go:100] " l.buf = append(l.buf, levelStr[level]) _, month, day := t.Date() l.itoa(int(month), 2) l.itoa(day, 2) l.buf = append(l.buf, ' ') hour, min, sec := t.Clock() l.itoa(hour, 2) l.buf = append(l.buf, ':') l.itoa(min, 2) l.buf = append(l.buf, ':') l.itoa(sec, 2) l.buf = append(l.buf, '.') l.itoa(t.Nanosecond()/1e6, 3) l.buf = append(l.buf, ' ') short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } l.buf = append(l.buf, short...) l.buf = append(l.buf, ':') l.itoa(line, -1) l.buf = append(l.buf, "] "...) }
func formatHeader(flag int, t time.Time, file string, line int, funcname string) string { var buf bytes.Buffer buf.WriteByte('[') needspace := false if flag&(DateFlag|TimeFlag|MsecFlag) != 0 { if flag&DateFlag != 0 { year, month, day := t.Date() writespace(&buf, needspace) fmt.Fprintf(&buf, "%04d/%02d/%02d", year, month, day) needspace = true } if flag&(TimeFlag|MsecFlag) != 0 { hour, min, sec := t.Clock() writespace(&buf, needspace) fmt.Fprintf(&buf, "%02d:%02d:%02d", hour, min, sec) if flag&MsecFlag != 0 { fmt.Fprintf(&buf, ".%06d", t.Nanosecond()/1e3) } needspace = true } } if flag&(LongFileFlag|ShortFileFlag|FuncNameFlag) != 0 { if flag&ShortFileFlag != 0 { file = shortfilename(file) } writespace(&buf, needspace) fmt.Fprintf(&buf, "%s:%d", file, line) if flag&FuncNameFlag != 0 { fmt.Fprintf(&buf, " (%s)", shortfuncname(funcname)) } needspace = true } buf.WriteByte(']') return buf.String() }
func formatTimeHeader(when time.Time) ([]byte, int) { y, mo, d := when.Date() h, mi, s := when.Clock() //len("2006/01/02 15:04:05 ")==20 var buf [20]byte buf[0] = y1[y/1000%10] buf[1] = y2[y/100] buf[2] = y3[y-y/100*100] buf[3] = y4[y-y/100*100] buf[4] = '/' buf[5] = mo1[mo-1] buf[6] = mo2[mo-1] buf[7] = '/' buf[8] = d1[d-1] buf[9] = d2[d-1] buf[10] = ' ' buf[11] = h1[h] buf[12] = h2[h] buf[13] = ':' buf[14] = mi1[mi] buf[15] = mi2[mi] buf[16] = ':' buf[17] = s1[s] buf[18] = s2[s] buf[19] = ' ' return buf[0:], d }
// remove the time component of a datetime to get just a date at 00:00:00 func TruncDate(t time.Time) time.Time { hour, min, sec := t.Clock() nano := t.Nanosecond() d := time.Duration(0) - (time.Duration(nano) + time.Duration(sec)*time.Second + time.Duration(min)*time.Minute + time.Duration(hour)*time.Hour) return t.Add(d) }
func marshalTimeCommon(out *forkableWriter, t time.Time) (err error) { _, month, day := t.Date() err = marshalTwoDigits(out, int(month)) if err != nil { return } err = marshalTwoDigits(out, day) if err != nil { return } hour, min, sec := t.Clock() err = marshalTwoDigits(out, hour) if err != nil { return } err = marshalTwoDigits(out, min) if err != nil { return } err = marshalTwoDigits(out, sec) if err != nil { return } _, offset := t.Zone() switch { case offset/60 == 0: err = out.WriteByte('Z') return case offset > 0: err = out.WriteByte('+') case offset < 0: err = out.WriteByte('-') } if err != nil { return } offsetMinutes := offset / 60 if offsetMinutes < 0 { offsetMinutes = -offsetMinutes } err = marshalTwoDigits(out, offsetMinutes/60) if err != nil { return } err = marshalTwoDigits(out, offsetMinutes%60) return }
// NewAt returns a new Clock with specified hour, minute, second and millisecond. func NewAt(t time.Time) Clock { hour, minute, second := t.Clock() hx := Clock(hour) * Hour mx := Clock(minute) * Minute sx := Clock(second) * Second ms := Clock(t.Nanosecond() / int(time.Millisecond)) return Clock(hx + mx + sx + ms) }
func (d *testingStorage) doPrint(str string, t time.Time) { if d.log == nil { return } hour, min, sec := t.Clock() msec := t.Nanosecond() / 1e3 d.log.Logf("<%02d:%02d:%02d.%06d> %s\n", hour, min, sec, msec, str) }
func (l *dateLexer) resolveDay(rel time.Time) time.Time { y, m, _ := rel.Date() h, n, s := rel.Clock() rel = time.Date(y, m, l.day, h, n, s, 0, rel.Location()) if DEBUG { fmt.Printf("Parsed day as %s %s\n", rel.Weekday(), rel) } return rel }
func (r *TimeRange) isInTimeRange(t time.Time) bool { t = t.In(r.loc) ts := NewTimeOfDay(t.Clock()).d if r.startTime.d > r.endTime.d { return !(r.endTime.d < ts && ts < r.startTime.d) } return r.startTime.d <= ts && ts <= r.endTime.d }
// The Flarm timestamp uses a Hours/Minutes/Seconds format. The date is not passed explicitely. // libfap-go messes up when converting this to a time, resulting in the correct time for different dates. func packetTime(t time.Time) time.Time { now := time.Now() hour, min, sec := t.Clock() day := now.Day() month := now.Month() year := now.Year() return time.Date(year, month, day, hour, min, sec, 0, time.Local) }
func (l *Logger) formatHeader(buf *[]byte, s Level, calldepth int, t time.Time) { flag := l.options.Flag if flag&LUTC != 0 { t = t.UTC() } if flag&(Ldate|Ltime|Lmicroseconds) != 0 { if flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) *buf = append(*buf, '/') itoa(buf, int(month), 2) *buf = append(*buf, '/') itoa(buf, day, 2) *buf = append(*buf, ' ') } if flag&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() itoa(buf, hour, 2) *buf = append(*buf, ':') itoa(buf, min, 2) *buf = append(*buf, ':') itoa(buf, sec, 2) if flag&Lmicroseconds != 0 { *buf = append(*buf, '.') itoa(buf, t.Nanosecond()/1e3, 6) } *buf = append(*buf, ' ') } } if flag&(Lshortfile|Llongfile) != 0 { _, file, line, ok := runtime.Caller(calldepth) if !ok { file = "???" line = 0 } if flag&Lshortfile != 0 { short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } file = short } *buf = append(*buf, file...) *buf = append(*buf, ':') itoa(buf, line, -1) *buf = append(*buf, ' ') } if flag&Llevel != 0 { *buf = append(*buf, levelName[s]...) *buf = append(*buf, ' ') } }
func StripDayGoTime(unano time.Time) Ntm { hrs, minutes, sec := unano.Clock() nsec := unano.Nanosecond() ntm := Ntm(time.Date(1970, time.Month(1), 1, hrs, minutes, sec, nsec, time.UTC).UnixNano()) //fmt.Printf("\n ntm = %v", ntm) return ntm }
func getLogFileName(t time.Time) string { var buf []byte year, month, day := t.Date() itoa(&buf, year, 4) itoa(&buf, int(month), 2) itoa(&buf, day, 2) hour, _, _ := t.Clock() itoa(&buf, hour, 2) return string(buf) + ".log" }
func (l *Logger) formatHeader(buf *[]byte, t time.Time, lvl int, file string, line int) { *buf = append(*buf, l.prefix...) if l.flag&Lmodule != 0 { *buf = append(*buf, moduleOf(file)...) *buf = append(*buf, ' ') } if l.flag&(Lshortfile|Llongfile) != 0 { if l.flag&Lshortfile != 0 { short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } file = short } *buf = append(*buf, file...) *buf = append(*buf, ':') itoa(buf, line, -1) *buf = append(*buf, ' ') } if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) *buf = append(*buf, '-') itoa(buf, int(month), 2) *buf = append(*buf, '-') itoa(buf, day, 2) *buf = append(*buf, ' ') } if l.flag&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() itoa(buf, hour, 2) *buf = append(*buf, ':') itoa(buf, min, 2) *buf = append(*buf, ':') itoa(buf, sec, 2) if l.flag&Lmicroseconds != 0 { *buf = append(*buf, '.') itoa(buf, t.Nanosecond()/1e3, 6) } *buf = append(*buf, ' ') } } if l.flag&Llevel != 0 { *buf = append(*buf, levels[lvl%Lmax]...) *buf = append(*buf, ' ') } }
func EncodeTime(t time.Time) []byte { if t.IsZero() { return []byte{0} // MySQL zero } y, mon, d := t.Date() h, m, s := t.Clock() n := t.Nanosecond() return encodeNonzeroTime( int16(y), byte(mon), byte(d), byte(h), byte(m), byte(s), uint32(n), ) }
// Replaces rel's year, month and day with the lexer's date func (l *dateLexer) resolveDate(rel time.Time) time.Time { y, m, d := l.date.Date() if y == 0 { y = rel.Year() } h, n, s := rel.Clock() rel = time.Date(y, m, d, h, n, s, 0, rel.Location()) if DEBUG { fmt.Printf("Parsed date as %s %s\n", rel.Weekday(), rel) } return rel }
func (l *Logger) formatHeader(level LLevel, buf *[]byte, t time.Time, file string, line int) { if l.flags&(Lshortlevel|Llevel) != 0 { var lev string if l.flags&Lshortlevel != 0 { lev = level.Initial() } else { lev = level.String() } *buf = append(*buf, ("[" + lev + "] ")...) } if l.flags&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flags&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) *buf = append(*buf, '/') itoa(buf, int(month), 2) *buf = append(*buf, '/') itoa(buf, day, 2) *buf = append(*buf, ' ') } if l.flags&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() itoa(buf, hour, 2) *buf = append(*buf, ':') itoa(buf, min, 2) *buf = append(*buf, ':') itoa(buf, sec, 2) if l.flags&Lmicroseconds != 0 { *buf = append(*buf, '.') itoa(buf, t.Nanosecond()/1e3, 6) } *buf = append(*buf, ' ') } } if l.flags&(Lshortfile|Llongfile) != 0 { if l.flags&Lshortfile != 0 { short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } file = short } *buf = append(*buf, file...) *buf = append(*buf, ':') itoa(buf, line, -1) *buf = append(*buf, ": "...) } }