Category talk:Wren-date: Difference between revisions

→‎Source code: Changed Date.fromNum_ from a private to a public constructor.
(→‎Source code: Improved parsing, added time zone support.)
(→‎Source code: Changed Date.fromNum_ from a private to a public constructor.)
Line 469:
}
 
// Private constructorConstructor for creating a Date object directly from a number of milliseconds.
construct fromNum_fromNumber(num, tz) {
if (num < 0 || num > Date.maximum.number) Fiber.abort("Number is out of range.")
if (!(tz = Date.isValidTz_(tz))) Fiber.abort("Invalid time zone designator.")
Line 483:
static new(y, mo, d) { Date.new(y, mo, d, 0, 0, 0, 0, "UTC") }
static new(y) { Date.new(y, 1, 1, 0, 0, 0, 0, "UTC") }
 
static fromNumber(num) { Date.fromNumber(num, "UTC") }
 
// Gets the component parts of this date, as a list, from its number
Line 554 ⟶ 556:
}
 
addWeeks(w) { Date.fromNum_fromNumber(_num + w * 86400000 * 7, _tz) }
addDays(d) { Date.fromNum_fromNumber(_num + d * 86400000, _tz) }
addHours(h) { Date.fromNum_fromNumber(_num + h * 3600000, _tz) }
addMinutes(mi) { Date.fromNum_fromNumber(_num + mi * 60000, _tz) }
addSeconds(s) { Date.fromNum_fromNumber(_num + s * 1000, _tz) }
addMillisecs(ms) { Date.fromNum_fromNumber(_num + ms, _tz) }
 
// Returns the day of the year in which this date falls.
Line 581 ⟶ 583:
 
// Returns a new date object with the new time zone. Doesn't adjust the time.
changeZone(newZone) { Date.fromNum_fromNumber(_num, newZone) }
 
// Attempts to adjust the time to a new time zone. If successful, returns a
Line 594 ⟶ 596:
var newOffset
if (hasName && !(newOffset = __tzs[newZone])) return null
if (oldOffset == newOffset) return Date.fromNum_fromNumber(_num, newZone) // no time adjustment needed
var ohm = Date.parseOffset_(oldOffset)
var nhm = Date.parseOffset_(newOffset)
var d = Date.fromNum_fromNumber(_num, newZone).addHours(nhm[0]).addMinutes(nhm[1])
return d.addHours(-ohm[0]).addMinutes(-ohm[1])
}
Line 603 ⟶ 605:
// The inherited 'clone' method just returns 'this' as Date objects are immutable.
// If you need an actual copy use this method instead.
copy() { Date.fromNum_fromNumber(_num, _tz) }
// Compares this date with another one to enable comparison operators via Comparable trait.
9,482

edits