Skip to content
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

Windows timestamp in converter panel #114

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ui/Components/ConverterPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
<tr><td>float</td> <td colspan="2">{{model.float }}</td></tr>
<tr><td>double</td> <td colspan="2">{{model.double}}</td></tr>
<tr><td>unixts</td> <td colspan="2">{{model.unixts}}</td></tr>
<tr><td>filetime</td> <td colspan="2">{{model.filetime}}</td></tr>
<tr><td>ascii</td> <td colspan="2"><div class="str">{{model.ascii }}</div></td></tr>
<tr><td>utf8</td> <td colspan="2"><div class="str">{{model.utf8 }}</div></td></tr>
<tr><td>utf16le</td><td colspan="2"><div class="str">{{model.utf16le}}</div></td></tr>
<tr><td>utf16be</td><td colspan="2"><div class="str">{{model.utf16be}}</div></td></tr>
</table>
</div>
</template>
</template>
12 changes: 12 additions & 0 deletions src/ui/Components/ConverterPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export class Converter {
return str.substring(0, i);
return str + "...";
}

static fileToUnixTime(filetime: number): number {
var unixEpoch = new Date(1970, 1, 1).getTime()
var filetimeEpoch = new Date(1601, 1, 1).getTime()
var epochDeltaInSec = (unixEpoch - filetimeEpoch) / 1_000
var filetimeInSec = filetime / 10_000_000
return filetimeInSec - epochDeltaInSec
}
}

export class ConverterPanelModel {
Expand All @@ -48,6 +56,7 @@ export class ConverterPanelModel {
float: string = "";
double: string = "";
unixts: string = "";
filetime: string = "";
ascii: string = "";
utf8: string = "";
utf16le: string = "";
Expand Down Expand Up @@ -75,6 +84,9 @@ export class ConverterPanelModel {
var u32le = Converter.numConv(data, 4, false, false);
this.unixts = u32le ? dateFormat(new Date(parseInt(u32le) * 1000), "yyyy-mm-dd HH:MM:ss") : "";

var u64le = Converter.numConv(data, 8, false, false);
this.filetime = u64le ? dateFormat(new Date(Converter.fileToUnixTime(parseInt(u64le)) * 1000), "yyyy-mm-dd HH:MM:ss") : "";
Comment on lines +87 to +88
Copy link
Member

@generalmimon generalmimon Jul 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sealmove Unfonutately, as all numbers in JavaScript are doubles with just 53 bits of mantissa, a 64-bit integer will lose precision during the conversion to it (using the parseInt method). It means that not all integers larger than 253 - 1 (available in the Number.MAX_SAFE_INTEGER constant) can't be represented accurately in the native JavaScript number type: e.g. Number.MAX_SAFE_INTEGER + 1 == Number.MAX_SAFE_INTEGER + 2, see MDN.

But it doesn't have to happen here, because the Converter.numConv method works internally with a bigInt, though it returns a string (don't know why). Anyway, I would suggest to convert the string to the bigInt type, and do the arithmetics you need to do in the fileToUnixTime method using its methods instead of the native arithmetic operators.

And I just checked that Date object has more than sufficient range for not losing precision here, it's even far larger than FILETIME has (maximum year in FILETIME is ~30828, maximum year of Date is 275760), so there shouldn't be a problem, if you choose a safe method of initializing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there shouldn't be a problem, if you choose a safe method of initializing it.

@sealmove Actually, you can safely ignore that "safe method". I've just tested on RunKit the maximum year 30828 of FILETIME, and the number that represents that Date (number of milliseconds that have elapsed since midnight on January 1, 1970, UTC) is still ~10 times less than the Number.MAX_SAFE_INTEGER, so it will be fine to just stuff that Unix timestamp * 1000 number into the new Date() constructor, after you do the arithmetic with bigInt.


try {
this.ascii = Converter.strDecode(data, "ascii");
this.utf8 = Converter.strDecode(data, "utf-8");
Expand Down