Date Time Conversion: Number to Date Time - matlab

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.

Related

Unix time stamp to date and hour in google sheets

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)

How to convert formatted date to unix epoch in Libreoffice calc

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 )

How can I find the difference in hours between 2 times in character arrays?

I have 2 times stored in character arrays in MATLAB.
a = '11:00 PM'
b = '07:30 AM'
I want to find the difference in hours between the 2 times, which should be 8.5 hours in this example. Is there any short method to do that? I can datenum both numbers, subtract them, datevec the difference, extract the hours and minutes from the vector, and convert them into hours, but this takes a lot of lines. Is there a more efficient way of doing this or is there an existing function?
You can do this by converting each string using datetime, taking the difference, then converting the result with hours:
numHours = hours(diff(datetime({a; b}, 'InputFormat', 'hh:mm a')));
numHours = numHours + 24.*(numHours < 0)
numHours =
8.5000
The second line accounts for the condition in your example, where the second time has to occur on the next day for the time difference to be positive, so 24 hours are added to the (negative) difference.
add a date to the time
like
a = '1/1/2000 11:00 PM'
b = '1/1/2000 07:30 AM'
the convert the string to datetime
x=str2num(strrep(a,':',''))
y=str2num(strrep(b,':',''))
then fine the difference between 2 dates
e = etime(x,y)
this will give you number of seconds between both times

Octave database package timestamp from PostgreSQL

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.

UTC Time to String Conversion

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.