-
-
Notifications
You must be signed in to change notification settings - Fork 524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support date/time on @colyseus/schema #297
Comments
Hi @dengzhaofun, |
Why not support the Date type, even if it helps to convert the number to display on the client. Forcing developers to change the data types of existing code is not a good solution @endel |
Supporting |
@endel I use import { DateTime, Duration } from "luxon";
DateTime.now().toString(); // 2021-03-05T19:26:46.628+02:00
Duration.fromMillis(30000).toString(); // PT30S In my opinion (new Date()).toString(); // Fri Mar 05 2021 19:31:51 GMT+0200 (Eastern European Standard Time) |
I'd prefer sending dates as number/unix timestamp rather than strings @vitalyrotari: class State extends Schema {
@type("number") timestamp: number;
}
const state = new State();
state.timestamp = Date.now();
// => 1614970340065
const dateInstance = new Date(state.timestamp);
// => Fri Mar 05 2021 15:52:20 GMT-0300 (Brasilia Standard Time)
dateInstance.getTime() === state.timestamp
// => true Eventually would be nice to convert seamlessly from unix timestamp to |
@endel unix timestamp doesn't preserve time zone, this is why I use |
@vitalyrotari I think it does! check out this answer on Stack Overflow: https://stackoverflow.com/questions/23062515/do-unix-timestamps-change-across-timezones/23062640#23062640
|
Oh I understand what you mean now, it is the same time but it does not specify which timezone that time is, that's right! |
@endel yep :) So the bulletproof option is |
But, |
@endel perhaps what is needed is a more generic solution, which is custom serializer/deserializer types. Would need to be registered on client to deserialize, matching the provided decorator. Might let you do:
of course, this would not serialize internal changes to the object and would only support updates by replacing the full object using the setter. And requires sending full data, unless you want to scan the byte array for diffs and only send the parts that change. |
Error: a 'string' was expected, but '"2020-01-28T15:33:25.708Z"' (Date) was provided in User#loginAt
@type('string')
@column({comment: '最后登录时间'})
loginAt: Date; // not work
@type('number')
@column({comment: '最后登录时间'})
loginAt: Date; // not work
The text was updated successfully, but these errors were encountered: