Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1750)

Side by Side Diff: src/pkg/time/time.go

Issue 6278047: code review 6278047: time: make Format 2.7x faster (Closed)
Patch Set: diff -r daebd1be3d41 https://go.googlecode.com/hg/ Created 11 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/time/format.go ('k') | src/pkg/time/time_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Package time provides functionality for measuring and displaying time. 5 // Package time provides functionality for measuring and displaying time.
6 // 6 //
7 // The calendrical calculations always assume a Gregorian calendar. 7 // The calendrical calculations always assume a Gregorian calendar.
8 package time 8 package time
9 9
10 import "errors" 10 import "errors"
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd { 250 if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
251 sec += int64(l.cacheZone.offset) 251 sec += int64(l.cacheZone.offset)
252 } else { 252 } else {
253 _, offset, _, _, _ := l.lookup(sec) 253 _, offset, _, _, _ := l.lookup(sec)
254 sec += int64(offset) 254 sec += int64(offset)
255 } 255 }
256 } 256 }
257 return uint64(sec + (unixToInternal + internalToAbsolute)) 257 return uint64(sec + (unixToInternal + internalToAbsolute))
258 } 258 }
259 259
260 // locabs is a combination of the Zone and abs methods,
261 // extracting both return values from a single zone lookup.
262 func (t Time) locabs() (name string, offset int, abs uint64) {
263 l := t.loc
264 if l == nil {
265 l = &utcLoc
266 }
267 // Avoid function call if we hit the local time cache.
268 sec := t.sec + internalToUnix
269 if l != &utcLoc {
270 if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
271 name = l.cacheZone.name
272 offset = l.cacheZone.offset
273 } else {
274 name, offset, _, _, _ = l.lookup(sec)
275 }
276 sec += int64(offset)
277 } else {
278 name = "UTC"
279 }
280 abs = uint64(sec + (unixToInternal + internalToAbsolute))
281 return
282 }
283
260 // Date returns the year, month, and day in which t occurs. 284 // Date returns the year, month, and day in which t occurs.
261 func (t Time) Date() (year int, month Month, day int) { 285 func (t Time) Date() (year int, month Month, day int) {
262 year, month, day, _ = t.date(true) 286 year, month, day, _ = t.date(true)
263 return 287 return
264 } 288 }
265 289
266 // Year returns the year in which t occurs. 290 // Year returns the year in which t occurs.
267 func (t Time) Year() int { 291 func (t Time) Year() int {
268 year, _, _, _ := t.date(false) 292 year, _, _, _ := t.date(false)
269 return year 293 return year
270 } 294 }
271 295
272 // Month returns the month of the year specified by t. 296 // Month returns the month of the year specified by t.
273 func (t Time) Month() Month { 297 func (t Time) Month() Month {
274 _, month, _, _ := t.date(true) 298 _, month, _, _ := t.date(true)
275 return month 299 return month
276 } 300 }
277 301
278 // Day returns the day of the month specified by t. 302 // Day returns the day of the month specified by t.
279 func (t Time) Day() int { 303 func (t Time) Day() int {
280 _, _, day, _ := t.date(true) 304 _, _, day, _ := t.date(true)
281 return day 305 return day
282 } 306 }
283 307
284 // Weekday returns the day of the week specified by t. 308 // Weekday returns the day of the week specified by t.
285 func (t Time) Weekday() Weekday { 309 func (t Time) Weekday() Weekday {
310 return absWeekday(t.abs())
311 }
312
313 // absWeekday is like Weekday but operates on an absolute time.
314 func absWeekday(abs uint64) Weekday {
286 // January 1 of the absolute year, like January 1 of 2001, was a Monday. 315 // January 1 of the absolute year, like January 1 of 2001, was a Monday.
287 » sec := (t.abs() + uint64(Monday)*secondsPerDay) % secondsPerWeek 316 » sec := (abs + uint64(Monday)*secondsPerDay) % secondsPerWeek
288 return Weekday(int(sec) / secondsPerDay) 317 return Weekday(int(sec) / secondsPerDay)
289 } 318 }
290 319
291 // ISOWeek returns the ISO 8601 year and week number in which t occurs. 320 // ISOWeek returns the ISO 8601 year and week number in which t occurs.
292 // Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to 321 // Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to
293 // week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 322 // week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1
294 // of year n+1. 323 // of year n+1.
295 func (t Time) ISOWeek() (year, week int) { 324 func (t Time) ISOWeek() (year, week int) {
296 year, month, day, yday := t.date(true) 325 year, month, day, yday := t.date(true)
297 wday := int(t.Weekday()+6) % 7 // weekday but Monday = 0. 326 wday := int(t.Weekday()+6) % 7 // weekday but Monday = 0.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 year++ 371 year++
343 week = 1 372 week = 1
344 } 373 }
345 } 374 }
346 375
347 return 376 return
348 } 377 }
349 378
350 // Clock returns the hour, minute, and second within the day specified by t. 379 // Clock returns the hour, minute, and second within the day specified by t.
351 func (t Time) Clock() (hour, min, sec int) { 380 func (t Time) Clock() (hour, min, sec int) {
352 » sec = int(t.abs() % secondsPerDay) 381 » return absClock(t.abs())
382 }
383
384 // absClock is like clock but operates on an absolute time.
385 func absClock(abs uint64) (hour, min, sec int) {
386 » sec = int(abs % secondsPerDay)
353 hour = sec / secondsPerHour 387 hour = sec / secondsPerHour
354 sec -= hour * secondsPerHour 388 sec -= hour * secondsPerHour
355 min = sec / secondsPerMinute 389 min = sec / secondsPerMinute
356 sec -= min * secondsPerMinute 390 sec -= min * secondsPerMinute
357 return 391 return
358 } 392 }
359 393
360 // Hour returns the hour within the day specified by t, in the range [0, 23]. 394 // Hour returns the hour within the day specified by t, in the range [0, 23].
361 func (t Time) Hour() int { 395 func (t Time) Hour() int {
362 return int(t.abs()%secondsPerDay) / secondsPerHour 396 return int(t.abs()%secondsPerDay) / secondsPerHour
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 secondsPerWeek = 7 * secondsPerDay 637 secondsPerWeek = 7 * secondsPerDay
604 daysPer400Years = 365*400 + 97 638 daysPer400Years = 365*400 + 97
605 daysPer100Years = 365*100 + 24 639 daysPer100Years = 365*100 + 24
606 daysPer4Years = 365*4 + 1 640 daysPer4Years = 365*4 + 1
607 days1970To2001 = 31*365 + 8 641 days1970To2001 = 31*365 + 8
608 ) 642 )
609 643
610 // date computes the year and, only when full=true, 644 // date computes the year and, only when full=true,
611 // the month and day in which t occurs. 645 // the month and day in which t occurs.
612 func (t Time) date(full bool) (year int, month Month, day int, yday int) { 646 func (t Time) date(full bool) (year int, month Month, day int, yday int) {
647 return absDate(t.abs(), full)
648 }
649
650 // absDate is like date but operates on an absolute time.
651 func absDate(abs uint64, full bool) (year int, month Month, day int, yday int) {
613 // Split into time and day. 652 // Split into time and day.
614 » d := t.abs() / secondsPerDay 653 » d := abs / secondsPerDay
615 654
616 // Account for 400 year cycles. 655 // Account for 400 year cycles.
617 n := d / daysPer400Years 656 n := d / daysPer400Years
618 y := 400 * n 657 y := 400 * n
619 d -= daysPer400Years * n 658 d -= daysPer400Years * n
620 659
621 // Cut off 100-year cycles. 660 // Cut off 100-year cycles.
622 // The last cycle has one extra leap year, so on the last day 661 // The last cycle has one extra leap year, so on the last day
623 // of that year, day / daysPer100Years will be 4 instead of 3. 662 // of that year, day / daysPer100Years will be 4 instead of 3.
624 // Cut it back down to 3 by subtracting n>>2. 663 // Cut it back down to 3 by subtracting n>>2.
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 case utc < start: 1019 case utc < start:
981 _, offset, _, _, _ = loc.lookup(start - 1) 1020 _, offset, _, _, _ = loc.lookup(start - 1)
982 case utc >= end: 1021 case utc >= end:
983 _, offset, _, _, _ = loc.lookup(end) 1022 _, offset, _, _, _ = loc.lookup(end)
984 } 1023 }
985 unix -= int64(offset) 1024 unix -= int64(offset)
986 } 1025 }
987 1026
988 return Time{unix + unixToInternal, int32(nsec), loc} 1027 return Time{unix + unixToInternal, int32(nsec), loc}
989 } 1028 }
OLDNEW
« no previous file with comments | « src/pkg/time/format.go ('k') | src/pkg/time/time_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b