Category talk:Wren-date: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
(→‎Source code: Changed Date.fromNum_ from a private to a public constructor.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(6 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<langsyntaxhighlight ecmascriptlang="wren">/* Module "date.wren" */
 
import "./trait" for Comparable
 
/*
Line 485:
 
static fromNumber(num) { Date.fromNumber(num, "UTC") }
 
// Returns the Gregorian Proleptic date equivalent to a Julian date given in y/m/d format.
// If 'ay' is 1582, the first omitted date is 5th October 1582 or, if 'ay' is 1752,
// 3rd September 1752. Years are always assumed to begin on 1st January and leap years
// to contain 29th February. Omitted Julian dates map respectively to 15th October 1582 or
// 14th September 1752. Subsequent dates are returned unchanged as Gregorian dates.
// If 'ay' is 2200, conversions continue up to 14th February 2200 but no further.
static fromJulian(y, m, d, ay) {
var dt = Date.new(y, m, d)
if (ay != 1582 && ay != 1752 && ay != 2200) Fiber.abort("Invalid adoption year.")
if (dt <= Date.new( 1, 1, 2)) Fiber.abort("Unsupported Julian date.")
if (dt <= Date.new( 100, 3, 1)) return dt.addDays(-2)
if (dt <= Date.new( 200, 2, 29)) return dt.addDays(-1)
if (dt <= Date.new( 300, 2, 28)) return dt
if (dt <= Date.new( 500, 2, 27)) return dt.addDays(1)
if (dt <= Date.new( 600, 2, 26)) return dt.addDays(2)
if (dt <= Date.new( 700, 2, 25)) return dt.addDays(3)
if (dt <= Date.new( 900, 2, 24)) return dt.addDays(4)
if (dt <= Date.new(1000, 2, 23)) return dt.addDays(5)
if (dt <= Date.new(1100, 2, 22)) return dt.addDays(6)
if (dt <= Date.new(1300, 2, 21)) return dt.addDays(7)
if (dt <= Date.new(1400, 2, 20)) return dt.addDays(8)
if (dt <= Date.new(1500, 2, 19)) return dt.addDays(9)
if (dt <= Date.new(1582, 10, 4)) return dt.addDays(10)
if (dt <= Date.new(1582, 10, 14)) return ay == 1582 ? Date.new(1582, 10, 15) : dt.addDays(10)
if (ay == 1582) return dt
if (dt <= Date.new(1700, 2, 18)) return dt.addDays(10)
if (dt <= Date.new(1752, 9, 2)) return dt.addDays(11)
if (dt <= Date.new(1752, 9, 13)) return ay == 1752 ? Date.new(1752, 9, 14) : dt.addDays(11)
if (ay == 1752) return dt
if (dt <= Date.new(1800, 2, 17)) return dt.addDays(11)
if (dt <= Date.new(1900, 2, 16)) return dt.addDays(12)
if (dt <= Date.new(2100, 2, 15)) return dt.addDays(13)
if (dt <= Date.new(2200, 2, 14)) return dt.addDays(14)
Fiber.abort("Unsupported Julian date.")
}
 
// As above method but always uses a 1582 adoption year.
static fromJulian(y, m, d) { fromJulian(y, m, d, 1582) }
 
// Gets the component parts of this date, as a list, from its number
Line 702 ⟶ 741:
// Maximum safe integer (2^53 - 1) and hence duration
static maximum { 9007199254740991 }
 
// Blocks the current fiber for a given number of milliseconds.
static wait(ms) {
if (ms.type != Num || ms < 0) {
Fiber.abort("Argument must be a non-negative number of milliseconds.")
}
var finish = System.clock + ms/1000
while (System.clock <= finish) {}
}
 
// Constructs a new Duration object by passing it a number (positive or negative) of:
Line 738 ⟶ 786:
/* Stopwatch enables one to easily time events. */
class Stopwatch {
// Times the execution of a fumctionfunction returning the duration it took to execute.
static time(fn) {
var sw = Stopwatch.new()
Line 759 ⟶ 807:
stop() { _start = null }
}
 
// Type aliases for classes in case of any name clashes with other modules.
var Date_Date = Date
var Date_Duration = Duration
var Date_Stopwatch = Stopwatch
var Date_Comparable = Comparable // in case imported indirectly
 
// Initialize Date tables.
Date.init_()</langsyntaxhighlight>
9,476

edits