Octave database package determines that PostgreSQL timestamp is returned in Octave as 8-byte-time-value positive or negative difference to 2000-01-01 00:00 (int64 scalar, representing microseconds)
e.g. timestamp 2015-12-04 11:22:08 is returned as 502543328000000 in Octave.
How do I convert this to more readable and usable form if I intend to plot time series in Octave?
Use datestr. Note that datestr expects the serial date number in units of days and since 1 Jan 0000 as seen from the datenum docs
Return the date/time input as a serial day number, with Jan 1, 0000
defined as day 1.
So first we would have to convert your PostgreSQL serial number to (a) units of days instead of microseconds and (b) to start from the year 0000 instead of 2000.
timestamp = 502543328000000; % in microsecond since 2000
timestamp = timestamp / (1000000*60*60*24); % Days since 2000
timestamp = timestamp + datenum([2000,1,1]); % Days since 0000
datestr(timestamp, "yyyy-MM-dd HH:mm:ss") % Formatted string
There is a problem with datestr format in previous answer, i.e. it returns months in place of minutes and minutes in place of months. Proper format is probably
timestamp = input_time; % in microsecond since 2000
timestamp = timestamp ./ (1000000*60*60*24); % Days since 2000
timestamp = timestamp + datenum([2000,1,1]); % Days since 0000
datestr(timestamp(i), "yyyy-mm-dd HH:MM:SS.FFF") % Formatted string
where FFF stands for miliseconds if needed.
Related
I am working in Google Sheets. How can I convert a timestamp in seconds (as type of 1634978274) to the format of 2021-10-23 08:23, e.g. Date, hours; Minutes and how can I store this result in a new column? The original timestamp as such should not be changed, though.
Unfortunately, I am not sure by which number to divide the timestamp as such to receive the correct date.
I strive to filter for the dates and hours, which is why I need to transform the timestamp in the first place.
if your unix / epoch time is in seconds use:
=TEXT(A2/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss")
without seconds:
=TEXT(A2/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm")
if your unix / epoch time is in milliseconds use:
=TEXT(A2/86400000+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss.000")
for array use:
=INDEX(IF(A1:A="",,TEXT(A1:A/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss")))
Unix time counts the number of elapsed seconds from an origin of January 1, 1970. So to convert to Sheets time:
=DATE(1970,1,1) + 1634978274/(60*60*24)
... where 60*60*24 = "60 seconds per minute x 60 minutes per hour x 24 hours per day."
Then you can format the formula cell (or range) with the Date/Time format of your choice.
If your Unix time will be entered into a cell, of course you can substitute 1634978274 in the formula with that cell reference, e.g.:
=DATE(1970,1,1) + A1/(24*60*60)
I have column with cell format date or time (DD.MM.YYYY HH:MM:SS) and values like
03.12.2013 14:01:49
04.12.2013 10:19:27
04.12.2013 12:44:56
04.12.2013 14:20:12
04.12.2013 18:30:21
I need those values converted to unix epoch (seconds since 1970). Somehow it feels like the values are not recognized as dates, but rather as strings. I tried different formats, had little luck with dates without time.
Operations performed on date data should be automatic provided that the cells are formatted as as a user defined DD.MM.YYYY HH:MM:SS in the 'Format' > 'Cells' > 'Numbers' tab.
If you're using the standard settings, LibreOffice Calc uses 12/30/1899 as it's default date. So the first step is getting the number of days between 12/30/1899 and 1/1/1970:
=(DATE(1970,1,1) - DATE(1899,12,30)) = 25569
Number of seconds in a day:
=(60 * 60 * 24) = 86400
If, for example, in cell A2 you have the date 03.12.2013 14:01:49. I subtract the difference between Calc's default date and the Unix Epoch we just calculated, and multiply it by the number of seconds in a day:
=(A2 - 25569) * 86400
The result is a value of 1363096909 which is the Epoch time in seconds. If you need it in milliseconds, multiply the equation by 1000.
If it's something you use a lot, you can create a custom function that does this. Go to Tools > Macros > Edit Macros, and type out the following into whichever module comes up:
REM ***** BASIC *****
Function EPOCH(date_cell)
EPOCH = (date_cell - 25569)*86400
End Function
Close the macro IDE, and now you can use your EPOCH() like any other function!
This formula worked for me, where the others above did not:
= DATE( 1970, 1, 1 ) + ( A1 / 86400 )
I have date & time in intervals of one hour(3600 seconds) in number format, e.g 0,3600, 7200, 10800, 14400, 18000 etc.
I have starting date and time , e.g 0 corresponds to 2005/06/01 01:00 in 'yyyy/mm/dd HH:MM' format.
I am writing this data to Excel file, so I am looking for way where I can convert time given in Hour (in seconds) to Date Time (2005/06/01 01:00, 2005/06/01 02:00 etc) before writing to excel file.
I have explored 'datenum' and 'datestr' functions but they are not useful since I can not give them customised start time i.e (0 corresponds to 2005/06/01 01:00).
May be if some one can help me to point me in right direction.
tempMatrix = [NrID time_inSec ff X Y];
tempMatrix_dataCell=num2cell(tempMatrix);
col_header={'NrID','Time','ff','X','Y'};
data_for_xls_file=[col_header; tempMatrix_dataCell];
xlswrite('My_file.xls',data_for_xls_file);
time_inSec is column with values 0, 3600, 7200, 10800 etc which need to be converted.
When I use datenum it returns 7.3246e+05 so when I add 3600 to get 2005/06/01 02:00 and pass it to datestr it returns 2015/04/10 01:00.
temp_time = datenum('2005/06/01 01:00','yyyy/mm/dd HH:MM')
This works with a given start time:
startTime = datenum('2005/06/01 01:00', 'yyyy/mm/dd HH:MM'); % Define start time.
currentTime = datenum('2005/06/01 02:00', 'yyyy/mm/dd HH:MM'); % Current time.
timePassedHours = (currentTime - startTime) * 24; % Time that has passed in hours.
display(timePassedHours); % Print the output.
I have some data files with Unix timestamps (in this case, number of milliseconds since Jan 1, 1970 00:00 UTC). I would like to convert these to human-friendly date/time strings (e.g. 31-Aug-2012 11:36:24) in Matlab. Is there an easy way to do this in Matlab, or am I better off using an external library (e.g. java.text.SimpleDateFormat)?
How about
date = datestr(unix_time/86400 + datenum(1970,1,1))
if unix_time is given in seconds, unix_time/86400 will give the number of days since Jan. 1st 1970. Add to that the offset used by Matlab's datenum (datenum(0000,1,1) == 1), and you have the amount of days since Jan. 1st, 0000. This can be easily converted to human-readable form by Matlab's datestr.
If you have milliseconds, just use
date = datestr(unix_time/86400/1000 + datenum(1970,1,1))
Wrapped in functions, these would be
function dn = unixtime_to_datenum( unix_time )
dn = unix_time/86400 + 719529; %# == datenum(1970,1,1)
end
function dn = unixtime_in_ms_to_datenum( unix_time_ms )
dn = unix_time_ms/86400000 + 719529; %# == datenum(1970,1,1)
end
datestr( unixtime_to_datenum( unix_time ) )
Newer versions of MATLAB (verified in R2015a) have a datetime type that is useful for working with and formatting dates and times. You can convert UNIX timestamps into a MATLAB datetime with
dt = datetime( unix_time, 'ConvertFrom', 'posixtime' );
and then use datestr as before for formatting as a string
datestr( dt )
I am looking for helping doing time conversions from UTC time to string using MATLAB.
I am trying to extract time from a data file collected at the end of October 2010.
The data file says it is reporting in UTC time and the field is an integer string value in milliseconds that is around 3.02e11. I would like to convert this to a string but am have some trouble.
I figured out that the units are most definitely in milliseconds so I convert this to fractions of days to be compatible with datenum format.
If the data was collected at the end of October (say, October 31, 2010) then I can guess what kind of number I might get. I thought that January 1, 2001 would be a good epoch and calculated what sort of number (in days) I might get:
suspectedDate = datenum('October 31, 2010')
suspectedEpoch = datenum('January 1, 2001')
suspectedTimeInDays = suspectedDate - suspectedEpoch
Which comes out as 3590.
However, my actual time, in days, comes out with the following code
actualTime = 3.02e11
actualTimeInDays = 3.02e11/1000/24/3600
as 3495.4.
This is troubling as the difference is only 94.6 -- not a full year. This would mean either the documentation for the file is wrong or the epoch is close to April 1-5, 2001:
calculatedEpoch = suspectedDate - actualTimeInDays
calculatedEpochStr = datestr(calculatedEpoch)
Alternately, if the epoch is January 1, 2001 then the actual date in the file is from the end of July.
ifEpochIsJanuaryDate = suspectedEpoch + actualTimeInDays
ifEpochIsJanuaryDateStr = datestr(ifEpochIsJanuaryDate)
Is this a known UTC format and can anyone give suggestions on how to get an October date from 3.02e11 magnitude number?
Unix time today is about 13e11, and is measured in ms since 1970.
If your time is ~3e11, then it's probably since year 2000.
>> time_unix = 1339116554872; % example time
>> time_reference = datenum('1970', 'yyyy');
>> time_matlab = time_reference + time_unix / 8.64e7;
>> time_matlab_string = datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF')
time_matlab_string =
20120608 00:49:14.872
Notes:
1) change 1970 into 2000 if your time is since 2000;
2) See the definition of matlab's time.
3) 8.64e7 is number of milliseconds in a day.
4) Matlab does not apply any time-zone shifts, so the result is the same UTC time.
5) Example for backward transformation:
>> matlab_time = now;
>> unix_time = round(8.64e7 * (matlab_time - datenum('1970', 'yyyy')))
unix_time =
1339118367664
You can't just make up your own epoch. Also datenum returns things in days. So the closeness you got with doing your math was just a coincidence.
Turns out that
>> datenum('Jan-1-0000')
ans =
1
and
>> datenum('Jan-1-0001')
ans =
367
So Matlab should be returning things in days since Jan. 1, 0000. (Not a typo)
However, I'd look carefully at this 3.02e11 number and find out exactly what it means. I'm pretty sure it's not standard Unix UTC, which should be seconds since January 1, 1970. It's way too big. It's close to GMT: Mon, 1 Jan 11540 08:53:20 UTC.