gizgame/calendar
the time of a game's world. A game whose world has a date (a voyage, a campaign, a season) holds one Calendar: play passes its days at a pace, and a jump (a turn, a journey skipped) passes them at once. Whoever passes time gets what passed, so a day's and a month's happenings run the same way however the time went.
import * as Calendar from "gizgame/calendar";Types
Passed
type Passed = { days: i64; months: i64; years: i64; }What a stretch of time held: the days that ended in it, and the months and the years among them.
days:i64months:i64years:i64
Calendar
class Calendar { private at: f64; pace: f64; constructor(year: i64, month: i64 = 1, day: i64 = 1, pace: f64 = 0.0) { assert(month> = 1 && month <= YEAR_MONTHS && day> = 1 && day <= MONTH_DAYS && pace> = 0.0); this.at = f64(year*YEAR_DAYS+(month-1)*MONTH_DAYS+day-1); this.pace = pace; } private whole(): i64 { return i64(Math.floor(this.at)); } year(): i64 { return i64(Math.floor(this.at/f64(YEAR_DAYS))); } month(): i64 { return (this.whole()-this.year()*YEAR_DAYS)/MONTH_DAYS+1; } day(): i64 { return (this.whole()-this.year()*YEAR_DAYS)%MONTH_DAYS+1; } hour(): f64 { return this.at-Math.floor(this.at); } pass(seconds: f64): Passed { return this.skip(seconds*this.pace); } skip(days: f64): Passed { assert(days> = 0.0); const was = this.whole(); const year = this.year(); const months = year*YEAR_MONTHS+this.month(); this.at + = days; return { days: this.whole()-was, months: this.year()*YEAR_MONTHS+this.month()-months, years: this.year()-year }; } text(): string { const m = this.month(); const d = this.day(); return `${this.year() }-${ m <10 ? "0": "" }${ m }-${ d <10 ? "0": "" }${ d }`; } }The time of a world: its date, the part of its day, and the PACE its days pass at in play (days a second; 0 for a world whose time only jumps, as by turns). A year before the year 1 is 0 or under (the year -4000).
at:f64— The days from the first day of the year 0, with the part of the day.pace:f64
Functions
constructor
constructor(year: i64, month: i64 = 1, day: i64 = 1, pace: f64 = 0.0)A calendar at the start of DAY of MONTH of YEAR.
Effects: fail, read, write, alloc.
Methods
Calendar.year
year(): i64The year; 0 or under before the year 1.
Effects: fail, read.
Calendar.month
month(): i64The month of the year, from 1.
Effects: fail, read.
Calendar.day
day(): i64The day of the month, from 1.
Effects: fail, read.
Calendar.hour
hour(): f64The part of the day that is gone, 0 to 1.
Effects: fail, read.
Calendar.pass
pass(seconds: f64): PassedSECONDS of play pass, at the calendar's pace.
Effects: fail, read, write.
Calendar.skip
skip(days: f64): PassedDAYS pass at once (a part of a day too): a turn, a journey not played.
Effects: fail, read, write.
Calendar.text
text(): stringThe date as YEAR-MM-DD.
Effects: fail, read, alloc.
Constants
MONTH_DAYS
const MONTH_DAYS: i64 = 30The days of a month and the months of a year of every game's calendar: a year is 360 days.
YEAR_MONTHS
const YEAR_MONTHS: i64 = 12The months of a year.