1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00

Timezone awareness for datetime input

This commit is contained in:
Matej Bačo 2022-09-06 09:07:29 +00:00
parent 04b9311190
commit d5d5c95507
5 changed files with 33 additions and 7 deletions

View file

@ -719,10 +719,10 @@ $permissions = $this->getParam('permissions', null);
<label for="xdefault">Default Value</label>
<template x-if="!(array || required)">
<input name="xdefault" type="datetime-local" class="margin-bottom-large">
<input name="xdefault" type="datetime-local" step=".001" data-cast-to="string-datetime" class="margin-bottom-large">
</template>
<template x-if="(array || required)">
<input name="xdefault" type="datetime-local" class="margin-bottom-large" disabled value="">
<input name="xdefault" type="datetime-local" step=".001" data-cast-to="string-datetime" class="margin-bottom-large" disabled value="">
</template>
<footer>

View file

@ -102,7 +102,7 @@ $permissions = $this->getParam('permissions', null);
<?php endif; ?>
<fieldset name="data" data-cast-to="object" data-ls-attrs="x-init=doc = {{project-document}}" x-data="{doc: {}}">
<ul data-ls-attrs="x-init=attributes = {{project-collection.attributes}}" x-data="{attributes: []}">
<ul data-ls-attrs="x-init=attributes = {{project-collection.attributes}}.map(function(attr) { if(attr.type === 'datetime' && doc[attr.key]) { doc[attr.key] = isoToLocal(doc[attr.key]) } return attr; })" x-data="{attributes: [], prefixWithZero(value, minLength = 2) { value = value + ''; while (value.length < minLength) { value = '0' + value; } return value; }, isoToLocal(isoTime) { const date = new Date(isoTime); const localTime = `${date.getFullYear()}-${this.prefixWithZero(date.getMonth() + 1)}-${this.prefixWithZero(date.getDate())}T${this.prefixWithZero(date.getHours())}:${this.prefixWithZero(date.getMinutes())}:${this.prefixWithZero(date.getSeconds())}.${this.prefixWithZero(date.getMilliseconds(), 3)}`; return localTime; } }">
<template x-for="attr in attributes.filter(a => a.status === 'available')">
<li>
<label>
@ -151,7 +151,7 @@ $permissions = $this->getParam('permissions', null);
:name="attr.key"
:required="attr.required"
x-model="doc[attr.key]"
data-cast-to="string" />
data-cast-to="string-datetime" />
</template>
<template x-if="attr.type === 'string' && !attr.format">
<textarea
@ -261,7 +261,7 @@ $permissions = $this->getParam('permissions', null);
:name="attr.key"
:required="attr.required"
x-model="doc[attr.key][index]"
data-cast-to="string" />
data-cast-to="string-datetime" />
</template>
<template x-if="attr.type === 'string' && !attr.format">
<textarea

View file

@ -3935,8 +3935,11 @@ request.onload=function(){if(4===request.readyState&&399>=request.status){let da
resolve(data);}else{reject(new Error(request.statusText));}};if(progress){request.addEventListener('progress',progress);request.upload.addEventListener('progress',progress,false);}
request.onerror=function(){reject(new Error("Network Error"));};request.send(params);})};return{'get':function(path,headers={},params={}){return call('GET',path+((params.length>0)?'?'+buildQuery(params):''),headers,{});},'post':function(path,headers={},params={},progress=null){return call('POST',path,headers,params,progress);},'put':function(path,headers={},params={},progress=null){return call('PUT',headers,params,progress);},'patch':function(path,headers={},params={},progress=null){return call('PATCH',path,headers,params,progress);},'delete':function(path,headers={},params={},progress=null){return call('DELETE',path,headers,params,progress);},'addGlobalParam':addGlobalParam,'addGlobalHeader':addGlobalHeader}}(window.document);let analytics={create:function(id,source,activity,url){return http.post('/analytics',{'content-type':'application/json'},{id:id,source:source,activity:activity,url:url,version:env.VERSION,setup:env.SETUP});},};return{analytics:analytics,};},true);})(window);(function(window){"use strict";window.ls.container.set('console',function(window){var client=new Appwrite.Client();var endpoint=window.location.origin+'/v1';client.setEndpoint(endpoint).setProject('console').setLocale(APP_ENV.LOCALE);return{client:client,account:new Appwrite.Account(client),avatars:new Appwrite.Avatars(client),databases:new Appwrite.Databases(client),functions:new Appwrite.Functions(client),health:new Appwrite.Health(client),locale:new Appwrite.Locale(client),projects:new Appwrite.Projects(client),storage:new Appwrite.Storage(client),teams:new Appwrite.Teams(client),users:new Appwrite.Users(client)}},true);})(window);(function(window){"use strict";window.ls.container.set('date',function(){function format(format,datetime){if(!datetime){return null;}
return new Intl.DateTimeFormat(navigator.languages,{hourCycle:'h24',...format}).format(new Date(datetime));}
return{format:format,}}(),true);})(window);(function(window){"use strict";window.ls.container.set('env',function(){return APP_ENV;},true);})(window);(function(window){"use strict";window.ls.container.set('form',function(){function cast(value,from,to,){if(value&&Array.isArray(value)&&to!=='array'){value=value.map(element=>cast(element,from,to));return value;}
return{format:format,}}(),true);})(window);(function(window){"use strict";window.ls.container.set('env',function(){return APP_ENV;},true);})(window);(function(window){"use strict";function prefixWithZero(value,minLength=2){value=value+"";while(value.length<minLength){value=`0${value}`;}
return value;}
window.ls.container.set('form',function(){function cast(value,from,to,){if(value&&Array.isArray(value)&&to!=='array'){value=value.map(element=>cast(element,from,to));return value;}
switch(to){case'int':case'integer':value=parseInt(value);break;case'numeric':value=Number(value);break;case'float':value=parseFloat(value);break;case'string':value=value.toString();if(value.length===0){value=null;}
break;case'string-datetime':value=value.toString();if(value.length===0){value=null;}else{const date=new Date(value);const isoTime=`${date.getUTCFullYear()}-${prefixWithZero(date.getUTCMonth() + 1)}-${prefixWithZero(date.getUTCDate())}T${prefixWithZero(date.getUTCHours())}:${prefixWithZero(date.getUTCMinutes())}:${prefixWithZero(date.getUTCSeconds())}.${prefixWithZero(date.getUTCMilliseconds(), 3)}+00:00`;value=isoTime;}
break;case'json':value=(value)?JSON.parse(value):[];break;case'array':if(value&&value.constructor&&value.constructor===Array){break;}
if(from==='csv'){if(value.length===0){value=[];}else{value=value.split(',');}}else{value=[value];}
break;case'array-empty':value=[];break;case'bool':case'boolean':value=(value==='false')?false:value;value=!!value;break;}

View file

@ -523,8 +523,11 @@ request.onload=function(){if(4===request.readyState&&399>=request.status){let da
resolve(data);}else{reject(new Error(request.statusText));}};if(progress){request.addEventListener('progress',progress);request.upload.addEventListener('progress',progress,false);}
request.onerror=function(){reject(new Error("Network Error"));};request.send(params);})};return{'get':function(path,headers={},params={}){return call('GET',path+((params.length>0)?'?'+buildQuery(params):''),headers,{});},'post':function(path,headers={},params={},progress=null){return call('POST',path,headers,params,progress);},'put':function(path,headers={},params={},progress=null){return call('PUT',headers,params,progress);},'patch':function(path,headers={},params={},progress=null){return call('PATCH',path,headers,params,progress);},'delete':function(path,headers={},params={},progress=null){return call('DELETE',path,headers,params,progress);},'addGlobalParam':addGlobalParam,'addGlobalHeader':addGlobalHeader}}(window.document);let analytics={create:function(id,source,activity,url){return http.post('/analytics',{'content-type':'application/json'},{id:id,source:source,activity:activity,url:url,version:env.VERSION,setup:env.SETUP});},};return{analytics:analytics,};},true);})(window);(function(window){"use strict";window.ls.container.set('console',function(window){var client=new Appwrite.Client();var endpoint=window.location.origin+'/v1';client.setEndpoint(endpoint).setProject('console').setLocale(APP_ENV.LOCALE);return{client:client,account:new Appwrite.Account(client),avatars:new Appwrite.Avatars(client),databases:new Appwrite.Databases(client),functions:new Appwrite.Functions(client),health:new Appwrite.Health(client),locale:new Appwrite.Locale(client),projects:new Appwrite.Projects(client),storage:new Appwrite.Storage(client),teams:new Appwrite.Teams(client),users:new Appwrite.Users(client)}},true);})(window);(function(window){"use strict";window.ls.container.set('date',function(){function format(format,datetime){if(!datetime){return null;}
return new Intl.DateTimeFormat(navigator.languages,{hourCycle:'h24',...format}).format(new Date(datetime));}
return{format:format,}}(),true);})(window);(function(window){"use strict";window.ls.container.set('env',function(){return APP_ENV;},true);})(window);(function(window){"use strict";window.ls.container.set('form',function(){function cast(value,from,to,){if(value&&Array.isArray(value)&&to!=='array'){value=value.map(element=>cast(element,from,to));return value;}
return{format:format,}}(),true);})(window);(function(window){"use strict";window.ls.container.set('env',function(){return APP_ENV;},true);})(window);(function(window){"use strict";function prefixWithZero(value,minLength=2){value=value+"";while(value.length<minLength){value=`0${value}`;}
return value;}
window.ls.container.set('form',function(){function cast(value,from,to,){if(value&&Array.isArray(value)&&to!=='array'){value=value.map(element=>cast(element,from,to));return value;}
switch(to){case'int':case'integer':value=parseInt(value);break;case'numeric':value=Number(value);break;case'float':value=parseFloat(value);break;case'string':value=value.toString();if(value.length===0){value=null;}
break;case'string-datetime':value=value.toString();if(value.length===0){value=null;}else{const date=new Date(value);const isoTime=`${date.getUTCFullYear()}-${prefixWithZero(date.getUTCMonth() + 1)}-${prefixWithZero(date.getUTCDate())}T${prefixWithZero(date.getUTCHours())}:${prefixWithZero(date.getUTCMinutes())}:${prefixWithZero(date.getUTCSeconds())}.${prefixWithZero(date.getUTCMilliseconds(), 3)}+00:00`;value=isoTime;}
break;case'json':value=(value)?JSON.parse(value):[];break;case'array':if(value&&value.constructor&&value.constructor===Array){break;}
if(from==='csv'){if(value.length===0){value=[];}else{value=value.split(',');}}else{value=[value];}
break;case'array-empty':value=[];break;case'bool':case'boolean':value=(value==='false')?false:value;value=!!value;break;}

View file

@ -25,6 +25,26 @@
value = null;
}
break;
case 'string-datetime':
value = value.toString();
if (value.length === 0) {
value = null;
} else {
function prefixWithZero(value, minLength = 2) {
value = value + "";
while (value.length < minLength) {
value = `0${value}`;
}
return value;
}
const date = new Date(value);
const isoTime = `${date.getUTCFullYear()}-${prefixWithZero(date.getUTCMonth() + 1)}-${prefixWithZero(date.getUTCDate())}T${prefixWithZero(date.getUTCHours())}:${prefixWithZero(date.getUTCMinutes())}:${prefixWithZero(date.getUTCSeconds())}.${prefixWithZero(date.getUTCMilliseconds(), 3)}+00:00`;
value = isoTime;
}
break;
case 'json':
value = (value) ? JSON.parse(value) : [];
break;