goscript 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/compiler/analysis.go +41 -2
  2. package/compiler/expr-call.go +87 -15
  3. package/compiler/expr-selector.go +100 -0
  4. package/compiler/spec-value.go +79 -8
  5. package/compiler/spec.go +236 -0
  6. package/dist/gs/builtin/builtin.d.ts +1 -0
  7. package/dist/gs/builtin/builtin.js +11 -0
  8. package/dist/gs/builtin/builtin.js.map +1 -1
  9. package/dist/gs/internal/oserror/errors.d.ts +6 -0
  10. package/dist/gs/internal/oserror/errors.js +7 -0
  11. package/dist/gs/internal/oserror/errors.js.map +1 -0
  12. package/dist/gs/internal/oserror/index.d.ts +1 -0
  13. package/dist/gs/internal/oserror/index.js +2 -0
  14. package/dist/gs/internal/oserror/index.js.map +1 -0
  15. package/dist/gs/io/fs/format.d.ts +3 -0
  16. package/dist/gs/io/fs/format.js +56 -0
  17. package/dist/gs/io/fs/format.js.map +1 -0
  18. package/dist/gs/io/fs/fs.d.ts +79 -0
  19. package/dist/gs/io/fs/fs.js +200 -0
  20. package/dist/gs/io/fs/fs.js.map +1 -0
  21. package/dist/gs/io/fs/glob.d.ts +10 -0
  22. package/dist/gs/io/fs/glob.js +141 -0
  23. package/dist/gs/io/fs/glob.js.map +1 -0
  24. package/dist/gs/io/fs/index.d.ts +8 -0
  25. package/dist/gs/io/fs/index.js +9 -0
  26. package/dist/gs/io/fs/index.js.map +1 -0
  27. package/dist/gs/io/fs/readdir.d.ts +7 -0
  28. package/dist/gs/io/fs/readdir.js +152 -0
  29. package/dist/gs/io/fs/readdir.js.map +1 -0
  30. package/dist/gs/io/fs/readfile.d.ts +6 -0
  31. package/dist/gs/io/fs/readfile.js +118 -0
  32. package/dist/gs/io/fs/readfile.js.map +1 -0
  33. package/dist/gs/io/fs/stat.d.ts +6 -0
  34. package/dist/gs/io/fs/stat.js +87 -0
  35. package/dist/gs/io/fs/stat.js.map +1 -0
  36. package/dist/gs/io/fs/sub.d.ts +6 -0
  37. package/dist/gs/io/fs/sub.js +172 -0
  38. package/dist/gs/io/fs/sub.js.map +1 -0
  39. package/dist/gs/io/fs/walk.d.ts +7 -0
  40. package/dist/gs/io/fs/walk.js +76 -0
  41. package/dist/gs/io/fs/walk.js.map +1 -0
  42. package/dist/gs/path/index.d.ts +2 -0
  43. package/dist/gs/path/index.js +3 -0
  44. package/dist/gs/path/index.js.map +1 -0
  45. package/dist/gs/path/match.d.ts +6 -0
  46. package/dist/gs/path/match.js +281 -0
  47. package/dist/gs/path/match.js.map +1 -0
  48. package/dist/gs/path/path.d.ts +7 -0
  49. package/dist/gs/path/path.js +256 -0
  50. package/dist/gs/path/path.js.map +1 -0
  51. package/dist/gs/time/time.d.ts +11 -2
  52. package/dist/gs/time/time.js +337 -12
  53. package/dist/gs/time/time.js.map +1 -1
  54. package/gs/builtin/builtin.ts +13 -0
  55. package/gs/internal/oserror/errors.ts +14 -0
  56. package/gs/internal/oserror/index.ts +1 -0
  57. package/gs/io/fs/format.ts +65 -0
  58. package/gs/io/fs/fs.ts +359 -0
  59. package/gs/io/fs/glob.ts +167 -0
  60. package/gs/io/fs/godoc.txt +35 -0
  61. package/gs/io/fs/index.ts +8 -0
  62. package/gs/io/fs/readdir.ts +126 -0
  63. package/gs/io/fs/readfile.ts +77 -0
  64. package/gs/io/fs/stat.ts +38 -0
  65. package/gs/io/fs/sub.ts +208 -0
  66. package/gs/io/fs/walk.ts +89 -0
  67. package/gs/path/index.ts +2 -0
  68. package/gs/path/match.ts +307 -0
  69. package/gs/path/path.ts +301 -0
  70. package/gs/strings/reader.test.ts +0 -1
  71. package/gs/time/time.ts +325 -12
  72. package/package.json +1 -1
  73. package/gs/time/godoc.md +0 -116
package/gs/time/time.ts CHANGED
@@ -3,16 +3,309 @@ export class Time {
3
3
  private _date: globalThis.Date
4
4
  private _nsec: number // nanoseconds within the second
5
5
  private _monotonic?: number // high-resolution monotonic timestamp in nanoseconds
6
+ private _location: Location // timezone location
6
7
 
7
- constructor(date: globalThis.Date, nsec: number = 0, monotonic?: number) {
8
+ constructor(date: globalThis.Date, nsec: number = 0, monotonic?: number, location?: Location) {
8
9
  this._date = new globalThis.Date(date.getTime())
9
10
  this._nsec = nsec
10
11
  this._monotonic = monotonic
12
+ this._location = location || UTC
11
13
  }
12
14
 
13
15
  // clone returns a copy of this Time instance
14
16
  public clone(): Time {
15
- return new Time(this._date, this._nsec, this._monotonic)
17
+ return new Time(this._date, this._nsec, this._monotonic, this._location)
18
+ }
19
+
20
+ // Format returns a textual representation of the time value formatted according to the layout
21
+ public Format(layout: string): string {
22
+ // Implementation of Go's time formatting based on reference time:
23
+ // "Mon Jan 2 15:04:05 MST 2006" (Unix time 1136239445)
24
+
25
+ // Calculate the time in the timezone of this Time object
26
+ let year: number, month0: number, dayOfMonth: number, dayOfWeek: number
27
+ let hour24: number, minute: number, second: number
28
+
29
+ if (this._location.offsetSeconds !== undefined) {
30
+ // For fixed timezone locations, adjust the UTC time by the offset
31
+ const offsetMs = this._location.offsetSeconds * 1000
32
+ const adjustedTime = new globalThis.Date(this._date.getTime() + offsetMs)
33
+
34
+ year = adjustedTime.getUTCFullYear()
35
+ month0 = adjustedTime.getUTCMonth() // 0-11 for array indexing
36
+ dayOfMonth = adjustedTime.getUTCDate() // 1-31
37
+ dayOfWeek = adjustedTime.getUTCDay() // 0 (Sun) - 6 (Sat)
38
+ hour24 = adjustedTime.getUTCHours() // 0-23
39
+ minute = adjustedTime.getUTCMinutes() // 0-59
40
+ second = adjustedTime.getUTCSeconds() // 0-59
41
+ } else {
42
+ // For local time, use the local timezone methods
43
+ year = this._date.getFullYear()
44
+ month0 = this._date.getMonth() // 0-11 for array indexing
45
+ dayOfMonth = this._date.getDate() // 1-31
46
+ dayOfWeek = this._date.getDay() // 0 (Sun) - 6 (Sat)
47
+ hour24 = this._date.getHours() // 0-23
48
+ minute = this._date.getMinutes() // 0-59
49
+ second = this._date.getSeconds() // 0-59
50
+ }
51
+
52
+ const nsec = this._nsec // Nanoseconds (0-999,999,999)
53
+
54
+ const shortMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
55
+ const longMonthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
56
+ const shortDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
57
+ const longDayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
58
+
59
+ const hour12 = hour24 % 12 || 12 // 12 for 0h and 12h
60
+ const ampmUpper = hour24 < 12 ? 'AM' : 'PM'
61
+ const ampmLower = ampmUpper.toLowerCase()
62
+
63
+ // Timezone offset calculation - use the location's offset if available
64
+ let tzOffsetSeconds = 0
65
+ let tzName = this._location.name
66
+ let isUTC = false
67
+
68
+ if (this._location.offsetSeconds !== undefined) {
69
+ // Use the fixed offset from the location
70
+ tzOffsetSeconds = this._location.offsetSeconds
71
+ isUTC = tzOffsetSeconds === 0 && this._location.name === "UTC"
72
+ } else {
73
+ // Fall back to JavaScript's timezone offset (for local time)
74
+ const tzOffsetMinutesJS = this._date.getTimezoneOffset()
75
+ tzOffsetSeconds = -tzOffsetMinutesJS * 60 // Convert to seconds, negate because JS offset is opposite
76
+ isUTC = tzOffsetSeconds === 0
77
+ }
78
+
79
+ let tzSign = '+'
80
+ if (tzOffsetSeconds < 0) {
81
+ tzSign = '-'
82
+ }
83
+ const absTzOffsetSeconds = Math.abs(tzOffsetSeconds)
84
+ const tzOffsetHours = Math.floor(absTzOffsetSeconds / 3600)
85
+ const tzOffsetMins = Math.floor((absTzOffsetSeconds % 3600) / 60)
86
+
87
+ // Helper function to format fractional seconds
88
+ const formatFracSeconds = (n: number, trimZeros: boolean): string => {
89
+ if (n === 0 && trimZeros) return ''
90
+ let str = n.toString().padStart(9, '0')
91
+ if (trimZeros) {
92
+ str = str.replace(/0+$/, '')
93
+ }
94
+ return str.length > 0 ? '.' + str : ''
95
+ }
96
+
97
+ let result = ''
98
+ let i = 0
99
+
100
+ // Process layout character by character, matching Go's nextStdChunk logic
101
+ while (i < layout.length) {
102
+ let matched = false
103
+
104
+ // Check for multi-character patterns first (longest matches first)
105
+ const remaining = layout.slice(i)
106
+
107
+ // Fractional seconds with comma/period
108
+ if (remaining.match(/^[.,]999999999/)) {
109
+ result += formatFracSeconds(nsec, true).replace('.', remaining[0])
110
+ i += 10
111
+ matched = true
112
+ } else if (remaining.match(/^[.,]999999/)) {
113
+ const microseconds = Math.floor(nsec / 1000)
114
+ let str = microseconds.toString().padStart(6, '0')
115
+ str = str.replace(/0+$/, '') // trim trailing zeros
116
+ result += str.length > 0 ? remaining[0] + str : ''
117
+ i += 7
118
+ matched = true
119
+ } else if (remaining.match(/^[.,]999/)) {
120
+ const milliseconds = Math.floor(nsec / 1000000)
121
+ let str = milliseconds.toString().padStart(3, '0')
122
+ str = str.replace(/0+$/, '') // trim trailing zeros
123
+ result += str.length > 0 ? remaining[0] + str : ''
124
+ i += 4
125
+ matched = true
126
+ } else if (remaining.match(/^[.,]000000000/)) {
127
+ result += remaining[0] + nsec.toString().padStart(9, '0')
128
+ i += 10
129
+ matched = true
130
+ } else if (remaining.match(/^[.,]000000/)) {
131
+ result += remaining[0] + Math.floor(nsec / 1000).toString().padStart(6, '0')
132
+ i += 7
133
+ matched = true
134
+ } else if (remaining.match(/^[.,]000/)) {
135
+ result += remaining[0] + Math.floor(nsec / 1000000).toString().padStart(3, '0')
136
+ i += 4
137
+ matched = true
138
+ }
139
+ // Full month/day names
140
+ else if (remaining.startsWith('January')) {
141
+ result += longMonthNames[month0]
142
+ i += 7
143
+ matched = true
144
+ } else if (remaining.startsWith('Monday')) {
145
+ result += longDayNames[dayOfWeek]
146
+ i += 6
147
+ matched = true
148
+ }
149
+ // Year patterns
150
+ else if (remaining.startsWith('2006')) {
151
+ result += year.toString()
152
+ i += 4
153
+ matched = true
154
+ }
155
+ // Timezone patterns (order matters - longer patterns first)
156
+ else if (remaining.startsWith('Z070000')) {
157
+ if (isUTC) {
158
+ result += 'Z'
159
+ } else {
160
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}${tzOffsetMins.toString().padStart(2, '0')}00`
161
+ }
162
+ i += 7
163
+ matched = true
164
+ } else if (remaining.startsWith('Z07:00:00')) {
165
+ if (isUTC) {
166
+ result += 'Z'
167
+ } else {
168
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}:${tzOffsetMins.toString().padStart(2, '0')}:00`
169
+ }
170
+ i += 9
171
+ matched = true
172
+ } else if (remaining.startsWith('Z0700')) {
173
+ if (isUTC) {
174
+ result += 'Z'
175
+ } else {
176
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}${tzOffsetMins.toString().padStart(2, '0')}`
177
+ }
178
+ i += 5
179
+ matched = true
180
+ } else if (remaining.startsWith('Z07:00')) {
181
+ if (isUTC) {
182
+ result += 'Z'
183
+ } else {
184
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}:${tzOffsetMins.toString().padStart(2, '0')}`
185
+ }
186
+ i += 6
187
+ matched = true
188
+ } else if (remaining.startsWith('Z07')) {
189
+ if (isUTC) {
190
+ result += 'Z'
191
+ } else {
192
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}`
193
+ }
194
+ i += 3
195
+ matched = true
196
+ } else if (remaining.startsWith('-070000')) {
197
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}${tzOffsetMins.toString().padStart(2, '0')}00`
198
+ i += 7
199
+ matched = true
200
+ } else if (remaining.startsWith('-07:00:00')) {
201
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}:${tzOffsetMins.toString().padStart(2, '0')}:00`
202
+ i += 9
203
+ matched = true
204
+ } else if (remaining.startsWith('-0700')) {
205
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}${tzOffsetMins.toString().padStart(2, '0')}`
206
+ i += 5
207
+ matched = true
208
+ } else if (remaining.startsWith('-07:00')) {
209
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}:${tzOffsetMins.toString().padStart(2, '0')}`
210
+ i += 6
211
+ matched = true
212
+ } else if (remaining.startsWith('-07')) {
213
+ result += `${tzSign}${tzOffsetHours.toString().padStart(2, '0')}`
214
+ i += 3
215
+ matched = true
216
+ }
217
+ // Hour patterns
218
+ else if (remaining.startsWith('15')) {
219
+ result += hour24.toString().padStart(2, '0')
220
+ i += 2
221
+ matched = true
222
+ }
223
+ // Month patterns
224
+ else if (remaining.startsWith('Jan')) {
225
+ result += shortMonthNames[month0]
226
+ i += 3
227
+ matched = true
228
+ }
229
+ // Day patterns
230
+ else if (remaining.startsWith('Mon')) {
231
+ result += shortDayNames[dayOfWeek]
232
+ i += 3
233
+ matched = true
234
+ } else if (remaining.startsWith('MST')) {
235
+ // Use the actual timezone name instead of literal "MST"
236
+ result += tzName
237
+ i += 3
238
+ matched = true
239
+ }
240
+ // AM/PM patterns
241
+ else if (remaining.startsWith('PM')) {
242
+ result += ampmUpper
243
+ i += 2
244
+ matched = true
245
+ } else if (remaining.startsWith('pm')) {
246
+ result += ampmLower
247
+ i += 2
248
+ matched = true
249
+ }
250
+ // Two-digit patterns
251
+ else if (remaining.startsWith('06')) {
252
+ result += (year % 100).toString().padStart(2, '0')
253
+ i += 2
254
+ matched = true
255
+ } else if (remaining.startsWith('_2')) {
256
+ result += dayOfMonth < 10 ? ' ' + dayOfMonth.toString() : dayOfMonth.toString()
257
+ i += 2
258
+ matched = true
259
+ } else if (remaining.startsWith('03')) {
260
+ result += hour12.toString().padStart(2, '0')
261
+ i += 2
262
+ matched = true
263
+ } else if (remaining.startsWith('01')) {
264
+ result += (month0 + 1).toString().padStart(2, '0')
265
+ i += 2
266
+ matched = true
267
+ } else if (remaining.startsWith('02')) {
268
+ result += dayOfMonth.toString().padStart(2, '0')
269
+ i += 2
270
+ matched = true
271
+ } else if (remaining.startsWith('04')) {
272
+ result += minute.toString().padStart(2, '0')
273
+ i += 2
274
+ matched = true
275
+ } else if (remaining.startsWith('05')) {
276
+ result += second.toString().padStart(2, '0')
277
+ i += 2
278
+ matched = true
279
+ }
280
+ // Single digit patterns (must come after two-digit patterns)
281
+ else if (layout[i] === '3' && (i === 0 || !'0123456789'.includes(layout[i-1]))) {
282
+ result += hour12.toString()
283
+ i += 1
284
+ matched = true
285
+ } else if (layout[i] === '2' && (i === 0 || !'0123456789'.includes(layout[i-1]))) {
286
+ result += dayOfMonth.toString()
287
+ i += 1
288
+ matched = true
289
+ } else if (layout[i] === '1' && (i === 0 || !'0123456789'.includes(layout[i-1]))) {
290
+ result += (month0 + 1).toString()
291
+ i += 1
292
+ matched = true
293
+ }
294
+ // Special Z handling for standalone Z
295
+ else if (layout[i] === 'Z' && !remaining.startsWith('Z0')) {
296
+ result += 'Z'
297
+ i += 1
298
+ matched = true
299
+ }
300
+
301
+ // If no pattern matched, copy the character literally
302
+ if (!matched) {
303
+ result += layout[i]
304
+ i += 1
305
+ }
306
+ }
307
+
308
+ return result
16
309
  }
17
310
 
18
311
  // Sub returns the duration t-u
@@ -40,7 +333,7 @@ export class Time {
40
333
  const newNsec = this._nsec + (durationNs % 1000000)
41
334
  const newMonotonic =
42
335
  this._monotonic !== undefined ? this._monotonic + durationNs : undefined
43
- return new Time(newDate, newNsec, newMonotonic)
336
+ return new Time(newDate, newNsec, newMonotonic, this._location)
44
337
  }
45
338
 
46
339
  // Equal reports whether t and u represent the same time instant
@@ -79,7 +372,7 @@ export class Time {
79
372
  public Round(d: Duration): Time {
80
373
  // Implementation would round to nearest duration
81
374
  // For now, simplified version that strips monotonic reading
82
- return new Time(this._date, this._nsec)
375
+ return new Time(this._date, this._nsec, undefined, this._location)
83
376
  }
84
377
 
85
378
  // Truncate returns the result of rounding t down to a multiple of d
@@ -87,7 +380,7 @@ export class Time {
87
380
  public Truncate(d: Duration): Time {
88
381
  // Implementation would truncate to duration
89
382
  // For now, simplified version that strips monotonic reading
90
- return new Time(this._date, this._nsec)
383
+ return new Time(this._date, this._nsec, undefined, this._location)
91
384
  }
92
385
 
93
386
  // String returns the time formatted as a string
@@ -156,14 +449,20 @@ export function multiplyDuration(
156
449
  // Location represents a time zone
157
450
  export class Location {
158
451
  private _name: string
452
+ private _offsetSeconds?: number
159
453
 
160
- constructor(name: string) {
454
+ constructor(name: string, offsetSeconds?: number) {
161
455
  this._name = name
456
+ this._offsetSeconds = offsetSeconds
162
457
  }
163
458
 
164
459
  public get name(): string {
165
460
  return this._name
166
461
  }
462
+
463
+ public get offsetSeconds(): number | undefined {
464
+ return this._offsetSeconds
465
+ }
167
466
  }
168
467
 
169
468
  // Month represents a month of the year
@@ -212,9 +511,10 @@ export function Date(
212
511
  loc: Location,
213
512
  ): Time {
214
513
  let date: globalThis.Date
215
- if (loc.name === 'UTC') {
216
- // Use Date.UTC for proper UTC handling
217
- const utcTime = globalThis.Date.UTC(
514
+
515
+ if (loc.offsetSeconds !== undefined) {
516
+ // For fixed timezone locations, create the date in the local timezone and then convert to UTC
517
+ const localTime = globalThis.Date.UTC(
218
518
  year,
219
519
  month - 1,
220
520
  day,
@@ -223,7 +523,9 @@ export function Date(
223
523
  sec,
224
524
  Math.floor(nsec / 1000000),
225
525
  )
226
- date = new globalThis.Date(utcTime)
526
+ // Subtract the offset to convert local time to UTC
527
+ // (if offset is -7*3600 for PDT, local time - (-7*3600) = local time + 7*3600 = UTC)
528
+ date = new globalThis.Date(localTime - loc.offsetSeconds * 1000)
227
529
  } else {
228
530
  // For local time or other timezones, use regular Date constructor
229
531
  date = new globalThis.Date(
@@ -236,11 +538,16 @@ export function Date(
236
538
  Math.floor(nsec / 1000000),
237
539
  )
238
540
  }
239
- return new Time(date, nsec % 1000000) // No monotonic reading
541
+ return new Time(date, nsec % 1000000000, undefined, loc) // No monotonic reading
240
542
  }
241
543
 
242
544
  // Common locations
243
- export const UTC = new Location('UTC')
545
+ export const UTC = new Location('UTC', 0)
546
+
547
+ // FixedZone returns a Location that always uses the given zone name and offset (seconds east of UTC)
548
+ export function FixedZone(name: string, offset: number): Location {
549
+ return new Location(name, offset)
550
+ }
244
551
 
245
552
  // Common durations (matching Go's time package constants)
246
553
  export const Nanosecond = new Duration(1)
@@ -270,3 +577,9 @@ export async function Sleep(d: Duration): Promise<void> {
270
577
 
271
578
  // Export month constants
272
579
  export const May = Month.May
580
+
581
+ // Time layout constants (matching Go's time package)
582
+ export const DateTime = "2006-01-02 15:04:05"
583
+ export const Layout = "01/02 03:04:05PM '06 -0700"
584
+ export const RFC3339 = "2006-01-02T15:04:05Z07:00"
585
+ export const Kitchen = "3:04PM"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "goscript",
3
3
  "description": "Go to TypeScript transpiler",
4
- "version": "0.0.28",
4
+ "version": "0.0.29",
5
5
  "author": {
6
6
  "name": "Aperture Robotics LLC.",
7
7
  "email": "support@aperture.us",
package/gs/time/godoc.md DELETED
@@ -1,116 +0,0 @@
1
- package time // import "time"
2
-
3
- Package time provides functionality for measuring and displaying time.
4
-
5
- The calendrical calculations always assume a Gregorian calendar, with no leap
6
- seconds.
7
-
8
- # Monotonic Clocks
9
-
10
- Operating systems provide both a “wall clock,” which is subject to changes
11
- for clock synchronization, and a “monotonic clock,” which is not. The general
12
- rule is that the wall clock is for telling time and the monotonic clock is for
13
- measuring time. Rather than split the API, in this package the Time returned by
14
- time.Now contains both a wall clock reading and a monotonic clock reading; later
15
- time-telling operations use the wall clock reading, but later time-measuring
16
- operations, specifically comparisons and subtractions, use the monotonic clock
17
- reading.
18
-
19
- For example, this code always computes a positive elapsed time of approximately
20
- 20 milliseconds, even if the wall clock is changed during the operation being
21
- timed:
22
-
23
- start := time.Now()
24
- ... operation that takes 20 milliseconds ...
25
- t := time.Now()
26
- elapsed := t.Sub(start)
27
-
28
- Other idioms, such as time.Since(start), time.Until(deadline), and
29
- time.Now().Before(deadline), are similarly robust against wall clock resets.
30
-
31
- The rest of this section gives the precise details of how operations use
32
- monotonic clocks, but understanding those details is not required to use this
33
- package.
34
-
35
- The Time returned by time.Now contains a monotonic clock reading. If Time t
36
- has a monotonic clock reading, t.Add adds the same duration to both the wall
37
- clock and monotonic clock readings to compute the result. Because t.AddDate(y,
38
- m, d), t.Round(d), and t.Truncate(d) are wall time computations, they always
39
- strip any monotonic clock reading from their results. Because t.In, t.Local,
40
- and t.UTC are used for their effect on the interpretation of the wall time,
41
- they also strip any monotonic clock reading from their results. The canonical
42
- way to strip a monotonic clock reading is to use t = t.Round(0).
43
-
44
- If Times t and u both contain monotonic clock readings, the operations
45
- t.After(u), t.Before(u), t.Equal(u), t.Compare(u), and t.Sub(u) are carried out
46
- using the monotonic clock readings alone, ignoring the wall clock readings.
47
- If either t or u contains no monotonic clock reading, these operations fall back
48
- to using the wall clock readings.
49
-
50
- On some systems the monotonic clock will stop if the computer goes to sleep.
51
- On such a system, t.Sub(u) may not accurately reflect the actual time that
52
- passed between t and u. The same applies to other functions and methods that
53
- subtract times, such as Since, Until, Time.Before, Time.After, Time.Add,
54
- Time.Equal and Time.Compare. In some cases, you may need to strip the monotonic
55
- clock to get accurate results.
56
-
57
- Because the monotonic clock reading has no meaning outside the current process,
58
- the serialized forms generated by t.GobEncode, t.MarshalBinary, t.MarshalJSON,
59
- and t.MarshalText omit the monotonic clock reading, and t.Format provides
60
- no format for it. Similarly, the constructors time.Date, time.Parse,
61
- time.ParseInLocation, and time.Unix, as well as the unmarshalers t.GobDecode,
62
- t.UnmarshalBinary. t.UnmarshalJSON, and t.UnmarshalText always create times with
63
- no monotonic clock reading.
64
-
65
- The monotonic clock reading exists only in Time values. It is not a part of
66
- Duration values or the Unix times returned by t.Unix and friends.
67
-
68
- Note that the Go == operator compares not just the time instant but also the
69
- Location and the monotonic clock reading. See the documentation for the Time
70
- type for a discussion of equality testing for Time values.
71
-
72
- For debugging, the result of t.String does include the monotonic clock
73
- reading if present. If t != u because of different monotonic clock readings,
74
- that difference will be visible when printing t.String() and u.String().
75
-
76
- # Timer Resolution
77
-
78
- Timer resolution varies depending on the Go runtime, the operating system
79
- and the underlying hardware. On Unix, the resolution is ~1ms. On Windows
80
- version 1803 and newer, the resolution is ~0.5ms. On older Windows versions,
81
- the default resolution is ~16ms, but a higher resolution may be requested using
82
- golang.org/x/sys/windows.TimeBeginPeriod.
83
-
84
- const Layout = "01/02 03:04:05PM '06 -0700" ...
85
- const Nanosecond Duration = 1 ...
86
- func After(d Duration) <-chan Time
87
- func Sleep(d Duration)
88
- func Tick(d Duration) <-chan Time
89
- type Duration int64
90
- func ParseDuration(s string) (Duration, error)
91
- func Since(t Time) Duration
92
- func Until(t Time) Duration
93
- type Location struct{ ... }
94
- var Local *Location = &localLoc
95
- var UTC *Location = &utcLoc
96
- func FixedZone(name string, offset int) *Location
97
- func LoadLocation(name string) (*Location, error)
98
- func LoadLocationFromTZData(name string, data []byte) (*Location, error)
99
- type Month int
100
- const January Month = 1 + iota ...
101
- type ParseError struct{ ... }
102
- type Ticker struct{ ... }
103
- func NewTicker(d Duration) *Ticker
104
- type Time struct{ ... }
105
- func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
106
- func Now() Time
107
- func Parse(layout, value string) (Time, error)
108
- func ParseInLocation(layout, value string, loc *Location) (Time, error)
109
- func Unix(sec int64, nsec int64) Time
110
- func UnixMicro(usec int64) Time
111
- func UnixMilli(msec int64) Time
112
- type Timer struct{ ... }
113
- func AfterFunc(d Duration, f func()) *Timer
114
- func NewTimer(d Duration) *Timer
115
- type Weekday int
116
- const Sunday Weekday = iota ...