How can we convert a datetime into a string that represents a Unix timestamp in nanoseconds?
I am trying to use Matlab to generate a string which contains a Unix timestamp in nanoseconds (i.e number of nanoseconds since 01-Jan-1970 00:00:00, the Unix epoch) from an input date string.
For example, if my input is only 1 ns after the start of the epoch, then the following code works:
t0 = datetime(’01-Jan-1970 00:00:00.000000000′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
t1 = datetime(’01-Jan-1970 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
dt_ns = seconds(t1 – t0)*1e9
dt_ns_string = sprintf(‘%.0f’,dt_ns)
% Output:
dt_ns_string =
‘1’
and I have the nanosecond precision that I need.
However, for later dates this does not work. For example, if I instead for t1 use a date around today:
t1 = datetime(’16-Jun-2020 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
then the output is the following:
dt_ns_string =
‘1592265600000000000’
and I have lost the final nanosecond precision on the end of the string (final character should be a "1").
I believe this may be due to working with double types, and I might need to use uint64, but I can’t figure out how to make the change.
How can I solve this?I am trying to use Matlab to generate a string which contains a Unix timestamp in nanoseconds (i.e number of nanoseconds since 01-Jan-1970 00:00:00, the Unix epoch) from an input date string.
For example, if my input is only 1 ns after the start of the epoch, then the following code works:
t0 = datetime(’01-Jan-1970 00:00:00.000000000′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
t1 = datetime(’01-Jan-1970 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
dt_ns = seconds(t1 – t0)*1e9
dt_ns_string = sprintf(‘%.0f’,dt_ns)
% Output:
dt_ns_string =
‘1’
and I have the nanosecond precision that I need.
However, for later dates this does not work. For example, if I instead for t1 use a date around today:
t1 = datetime(’16-Jun-2020 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
then the output is the following:
dt_ns_string =
‘1592265600000000000’
and I have lost the final nanosecond precision on the end of the string (final character should be a "1").
I believe this may be due to working with double types, and I might need to use uint64, but I can’t figure out how to make the change.
How can I solve this? I am trying to use Matlab to generate a string which contains a Unix timestamp in nanoseconds (i.e number of nanoseconds since 01-Jan-1970 00:00:00, the Unix epoch) from an input date string.
For example, if my input is only 1 ns after the start of the epoch, then the following code works:
t0 = datetime(’01-Jan-1970 00:00:00.000000000′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
t1 = datetime(’01-Jan-1970 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
dt_ns = seconds(t1 – t0)*1e9
dt_ns_string = sprintf(‘%.0f’,dt_ns)
% Output:
dt_ns_string =
‘1’
and I have the nanosecond precision that I need.
However, for later dates this does not work. For example, if I instead for t1 use a date around today:
t1 = datetime(’16-Jun-2020 00:00:00.000000001′,’Format’,’dd-MMM-yyyy HH:mm:ss.SSSSSSSSS’);
then the output is the following:
dt_ns_string =
‘1592265600000000000’
and I have lost the final nanosecond precision on the end of the string (final character should be a "1").
I believe this may be due to working with double types, and I might need to use uint64, but I can’t figure out how to make the change.
How can I solve this? datetime, unix time MATLAB Answers — New Questions