Category talk:Wren-date: Difference between revisions

Content added Content deleted
(→‎Source code: Improved parsing, added time zone support.)
(→‎Source code: Changed Date.fromNum_ from a private to a public constructor.)
Line 469: Line 469:
}
}


// Private constructor for creating a Date object directly from a number of milliseconds.
// Constructor for creating a Date object directly from a number of milliseconds.
construct fromNum_(num, tz) {
construct fromNumber(num, tz) {
if (num < 0 || num > Date.maximum.number) Fiber.abort("Number is out of range.")
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.")
if (!(tz = Date.isValidTz_(tz))) Fiber.abort("Invalid time zone designator.")
Line 483: Line 483:
static new(y, mo, d) { Date.new(y, mo, d, 0, 0, 0, 0, "UTC") }
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 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
// Gets the component parts of this date, as a list, from its number
Line 554: Line 556:
}
}


addWeeks(w) { Date.fromNum_(_num + w * 86400000 * 7, _tz) }
addWeeks(w) { Date.fromNumber(_num + w * 86400000 * 7, _tz) }
addDays(d) { Date.fromNum_(_num + d * 86400000, _tz) }
addDays(d) { Date.fromNumber(_num + d * 86400000, _tz) }
addHours(h) { Date.fromNum_(_num + h * 3600000, _tz) }
addHours(h) { Date.fromNumber(_num + h * 3600000, _tz) }
addMinutes(mi) { Date.fromNum_(_num + mi * 60000, _tz) }
addMinutes(mi) { Date.fromNumber(_num + mi * 60000, _tz) }
addSeconds(s) { Date.fromNum_(_num + s * 1000, _tz) }
addSeconds(s) { Date.fromNumber(_num + s * 1000, _tz) }
addMillisecs(ms) { Date.fromNum_(_num + ms, _tz) }
addMillisecs(ms) { Date.fromNumber(_num + ms, _tz) }


// Returns the day of the year in which this date falls.
// Returns the day of the year in which this date falls.
Line 581: Line 583:


// Returns a new date object with the new time zone. Doesn't adjust the time.
// Returns a new date object with the new time zone. Doesn't adjust the time.
changeZone(newZone) { Date.fromNum_(_num, newZone) }
changeZone(newZone) { Date.fromNumber(_num, newZone) }


// Attempts to adjust the time to a new time zone. If successful, returns a
// Attempts to adjust the time to a new time zone. If successful, returns a
Line 594: Line 596:
var newOffset
var newOffset
if (hasName && !(newOffset = __tzs[newZone])) return null
if (hasName && !(newOffset = __tzs[newZone])) return null
if (oldOffset == newOffset) return Date.fromNum_(_num, newZone) // no time adjustment needed
if (oldOffset == newOffset) return Date.fromNumber(_num, newZone) // no time adjustment needed
var ohm = Date.parseOffset_(oldOffset)
var ohm = Date.parseOffset_(oldOffset)
var nhm = Date.parseOffset_(newOffset)
var nhm = Date.parseOffset_(newOffset)
var d = Date.fromNum_(_num, newZone).addHours(nhm[0]).addMinutes(nhm[1])
var d = Date.fromNumber(_num, newZone).addHours(nhm[0]).addMinutes(nhm[1])
return d.addHours(-ohm[0]).addMinutes(-ohm[1])
return d.addHours(-ohm[0]).addMinutes(-ohm[1])
}
}
Line 603: Line 605:
// The inherited 'clone' method just returns 'this' as Date objects are immutable.
// The inherited 'clone' method just returns 'this' as Date objects are immutable.
// If you need an actual copy use this method instead.
// If you need an actual copy use this method instead.
copy() { Date.fromNum_(_num, _tz) }
copy() { Date.fromNumber(_num, _tz) }
// Compares this date with another one to enable comparison operators via Comparable trait.
// Compares this date with another one to enable comparison operators via Comparable trait.