I store date time in google cloud mysql as CURRENT_TIMESTAMP
. I access using momentjs
with knex
typeCast: option (Nodejs server hosted in Google App Engine) and convert them to milliseconds before sending response back to client (android).
Real date in database: 2019-01-07 12:37:48
What Android gives when I convert the date to MYSQL date format: 2019-01-07 15:37:48 Note the difference of 3 hours.
How I convert in android:
public static SimpleDateFormat DB_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
public static SimpleDateFormat DB_FORMAT_UNCHANGED = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
"updated_at", > DB_FORMAT_UNCHANGED.format(milliseconds);
"updated_at", > DB_FORMAT_UNCHANGED.format(milliseconds);//giving similar inaccurate result
Question: How can I solve this in a way that wont mess the accuracy of date wherever my app users travel to across the world since subtracting the +3 GMT offset is not efficient
EDIT:
How it's converted during access in the server, insideknex config
:
const config = {
user: 'user' ,
password: 'pwpw' ,
database: 'dbdb' ,
typeCast: function (field, next) {
if (field.type === 'JSON') {
return (JSON.parse(field.string()));
}
if (field.type === 'TINY' && field.length === 1) {
return (field.string() === '1');
}
if (field.type === 'DATETIME') {
return (moment(field.string()).valueOf());
}
return next();
}
};
MYSQL returns DATETIME
column values without timezone offset. So when client interprets them they will be read as local time of client's timezone.
You could use TIMESTAMP
column type instead to get have your times interpreted always with timezone and to get always consistent value for the unix timestamp even if client's timezone differs.