You are not logged in.
Pages: 1
Hi,
i'm writing a javascript client that get data with a query.
now i should write a condition on a TTimeLog field but i don't know how to convert a Date to TTimeLog in javascript.
Any idea?
thanks,
Emanuele.
Offline
Just encode a JavaScript date with the bit-shifted values from the TTimeLog integer content.
function TTimeLogBits.Year: Integer;
begin
result := (Value shr (6+6+5+5+4)) and 4095;
end;
function TTimeLogBits.Month: Integer;
begin
result := 1+(Int64Rec(Value).Lo shr (6+6+5+5)) and 15;
end;
function TTimeLogBits.Day: Integer;
begin
result := 1+(Int64Rec(Value).Lo shr (6+6+5)) and 31;
end;
function TTimeLogBits.Hour: Integer;
begin
result := (Int64Rec(Value).Lo shr (6+6)) and 31;
end;
function TTimeLogBits.Minute: Integer;
begin
result := (Int64Rec(Value).Lo shr 6) and 63;
end;
function TTimeLogBits.Second: Integer;
begin
result := Int64Rec(Value).Lo and 63;
end;
Online
hi ab,
excuse me but maybe i explained wrong or i don't understand what do you mean.
i retrieve data from a javascript client page (that use the mormotClient.js written from Esmond, thanks!).
I need to query on TTimeLog field and so, i think, i need to encode a DateTime javascript variable to TTimeLog.
how to do?
thanks
Offline
Take a look to the mORMot sources (SynCommons.pas):
// bits: S=0..5 M=6..11 H=12..16 D=17..21 M=22..25 Y=26..38
// size: S=6 M=6 H=5 D=5 M=4 Y=12
// i.e. S<64 M<64 H<32 D<32 M<16 Y<4096: power of 2 -> use fast shl/shr
procedure TTimeLogBits.From(Y, M, D, HH, MM, SS: cardinal);
begin
inc(HH,D shl 5+M shl 10+Y shl 14-(1 shl 5+1 shl 10));
Value := SS+MM shl 6+Int64(HH) shl 12;
end;
Offline
i looked and i write this javascript code but the number dont seems a ttimelog...
var timeLog = 0 + (day << 5) + (month << 10) + (year << 14) - ((1 << 5) + (1 << 10));
timeLog = (timeLog << 12);
NB i need date only
where i mistake?
Offline
I just googled that JavaScript bit-level operations are limited to 32 bit. You can use some library to work with 64-bit integers in JS, for example: http://google.github.io/closure-library … _Long.html
Offline
var timeLog = 0 + (day * 5) + (month * 10) + (year * 14) - ((1 * 5) + (1 * 10));
timeLog = (timeLog * 12);
like this?
Offline
ooops sorry, now i understand!
var temp = 0 + (day * Math.pow(2,5)) + (month * Math.pow(2,10)) + (year * Math.pow(2,14)) - ((1 * Math.pow(2,5)) + (1 * Math.pow(2,10)));
var timeLog = temp * Math.pow(2,12);
this javascript code seems to work!
Offline
any idea to write a javascript TimeLogToDateTime function?
Offline
for other user, i hope can help.
excuse my poor javascript.
function TimeLogFromDateTime(dataText) {
var giorno = new Date(dataText).getDate();
var mese = new Date(dataText).getMonth();
mese++;
var anno = new Date(dataText).getFullYear();
var temp = 0 + (giorno * Math.pow(2,5)) + (mese * Math.pow(2,10)) + (anno * Math.pow(2,14)) - ((1 * Math.pow(2,5)) + (1 * Math.pow(2,10)));
var result = temp * Math.pow(2,12);
return result;
}
function TimeLogToDateTime(Value) {
var result = new Date();
//window.alert(Value);
var Y = Math.round(Value / Math.pow(2,(6+6+5+5+4)) & 4095);
var M = 1+((Value) / Math.pow(2,(6+6+5+5))) & 15;
var D = 1+((Value) / Math.pow(2,(6+6+5))) & 31;
result.setFullYear(Y);
result.setMonth(M-1);
result.setDate(D);
result.setHours(0);
result.setMinutes(0);
result.setSeconds(0);
result.setMilliseconds(0);
if (Value & (1 * Math.pow(2,(6+6+5))-1) != 0) {
var HH = ((Value) / Math.pow(2,(6+6))) & 31;
var MM = ((Value) / Math.pow(2,6)) & 63;
var SS = (Value) & 63;
result.setHours(HH);
result.setMinutes(MM);
result.setSeconds(SS);
result.setMilliseconds(0);
}
return result;
}
Offline
Thanks lele9. I was looking for this too...
Offline
Pages: 1