as-date

Staging AssemblyScript Date implementation by @LiaoPeng , as an installable AssemblyScript library.
This library builds upon @LiaoPeng 's work in AssemblyScript/assemblyscript#357. By taking their changes, and moving into an installble AssemblyScript library. This is a rough implementation of Date
for AssemblyScript, and a more mature Date
implementation will eventually be written and merged into the AssemblyScript Standard Library.
Installation
as-date
is available as a npm package. You can install as-date
in your AssemblyScript project by running:
npm install --save as-date
Quick Start
import { Date } from "as-date";
let year = 2021;
let month = 2;
let dayOfMonth = 10;
let dayOfWeek = 3;
let hours = 18;
let minutes = 6;
let seconds = 24;
let milliseconds = 242;
let date = new Date(
Date.UTC(year, month, dayOfMonth, hours, minutes, seconds, milliseconds),
);
let timeZoneOffsetHours = -7;
date.setTimezoneOffset(timeZoneOffsetHours * 60);
expect(date.getUTCFullYear()).toBe(year);
expect(date.getUTCMonth()).toBe(month);
expect(date.getUTCDate()).toBe(dayOfMonth);
expect(date.getUTCDay()).toBe(dayOfWeek);
expect(date.getUTCHours()).toBe(hours);
expect(date.getUTCMinutes()).toBe(minutes);
expect(date.getUTCSeconds()).toBe(seconds);
expect(date.getUTCMilliseconds()).toBe(milliseconds);
expect(date.getFullYear()).toBe(year);
expect(date.getMonth()).toBe(month);
expect(date.getDate()).toBe(dayOfMonth);
expect(date.getDay()).toBe(dayOfWeek);
expect(date.getHours()).toBe(hours - 7);
expect(date.getMinutes()).toBe(minutes);
expect(date.getSeconds()).toBe(seconds);
expect(date.getMilliseconds()).toBe(milliseconds);
date.setHours(13);
expect(date.getUTCHours()).toBe(20);
expect(date.getHours()).toBe(13);
date.setUTCHours(21);
expect(date.getUTCHours()).toBe(21);
expect(date.getHours()).toBe(14);
Reference API
The Reference API documentation can be found in the docs/
directory.
Similar Libraries
License
TL;DR This project is licensed under GPLv2 with a linking exception.
This library builds on @LiaoPeng 's work, which they mentioned is based on the Java Standard Library Date. @DcodeIO and I discussed that we should look into the License in Java to ensure that we can use the code in AssemblyScript (which is licensed under MIT).
Looking at the AssemblyScript/assemblyscript#357, you will notice it does refer to Java's util/Calendar.java, and states that the file is licensed under GPLv2 with a linking exception.
Looking through various discussions of porting, or creating derivative works, of other implementations (1, 2, 3, 4, 5), it seems like we should be preserving the license that this code was stated to have reffered to. There could be an argument that this could have been a "clean room" implementation, but it would probably be best to just make this library have the same License! Which means that this library can't be merged into the main AssemblyScript standard library, but can act as a Date implementation in the meantime :)
Lastly, I am not a lawyer or software license expert, but when we take a look at the license exception meaning, or compare to a less restrictive but similar license like LGPL, it is possible that this license allows this library to be used in any other project freely without the parent project having to modify their license or distribution.