Matlab code to transfer between gregorian-hijri calendars - matlab

Is there a Matlab code that transfer the date ( day,month,year) from gregorian
to Hijri (Islamic) calendar and also from hijri to gregorian calendar,
Let's assume that we want to change the gregorian date:
Friday, 18 / 11 / 2011
to the Hijri date which is Friday 22 / 12 / 1432
Thanks

If you are on Windows, you could use the .NET Framework from inside MATLAB.
Here is a function to convert Gregorian dates to Hijri (based on an article on CodeProject):
function out = GregToHijri(str, frmtIn, frmtOut)
% English (US) and Arabic (Saudi Arabia) cultures
enCult = System.Globalization.CultureInfo('en-US',false);
enCult.DateTimeFormat.Calendar = System.Globalization.GregorianCalendar();
arCult = System.Globalization.CultureInfo('ar-SA',false);
arCult.DateTimeFormat.Calendar = System.Globalization.HijriCalendar();
% parse using supplied input format
dt = System.DateTime.ParseExact(str, frmtIn, enCult.DateTimeFormat);
% convert datetime as formatted string
out = char( dt.ToString(frmtOut, arCult.DateTimeFormat) );
end
Tested on your input:
>> GregToHijri('Friday, 18/11/2011', 'dddd, dd/MM/yyyy', 'dd/MM/yyyy')
ans =
22/12/1432

Related

how can i get the day of the week for a particular given date in Matlab

As today is Wednesday with date June 8, 2016. how can i write a code to get the day of given dates:
like what day is Nov 29
I'm trying to create a struct with
date
day
month
with month and date as input
Use the builtin weekday() function:
>> [num, name] = weekday('08-Jun-2016')
num =
4
name =
Wed
>> [num, name] = weekday('29-Nov-2016')
num =
3
name =
Tue
In addition to the weekday function, you can use the DDD or DDDD formats in the datestr function, like this:
datestr('08-Jun-2016','DDD') %Returns the string 'Wed'
datestr('08-Jun-2016','DDDD') %Returns the string 'Wednesday'
Or, to use a more practical format
datestr('08-Jun-2016','DDDD, mmmm DD, yyyy')
% Returns the string: 'Wednesday, June 08, 2016'

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

In GNU Octave, get the date and time

I want to get the date and time in GNU Octave and print it to screen. What is the best way to do this?
I've checked the Octave documentation, but I don't see a function to print out YYYY-MM-DD HH:MM:SS with a one liner simple command.
http://www.gnu.org/software/octave/doc/interpreter/Timing-Utilities.html
Get the date and time in Octave:
Number of seconds since epoch as integer:
time()
ans = 1.3482e+09
The date(), now() and datestr(...) builtin methods:
date()
ans = 20-Sep-2012
datestr(now(), 'yyyy-mm-dd');
ans = 2022-07-04
str2num(datestr(now(), 'yyyymmddHHMMSS'))
ans = 20220704165727
Add/Subtract days/months interval from now:
datestr(addtodate(now(), -20, 'days'), 'yyyy-mm-dd')
ans = 2022-06-14
datestr(addtodate(now(), -20, 'month'), 'yyyy-mm-dd')
ans = 2020-11-04
Format date/time as string with strftime
strftime ("%r (%Z) %A %e %B %Y", localtime (time ()))
ans = 09:52:42 PM (EDT) Thursday 20 September 2012
Get yyyy-mm-dd hh:mm:ss format
strftime ("%Y-%m-%d %H:%M:%S", localtime (time ()))
ans = 2012-09-20 21:56:07
Sources:
https://octave.sourceforge.io/octave/function/datestr.html
https://octave.sourceforge.io/octave/function/strftime.html

Convert date to closest end-of-month date MATLAB

I need to convert a datenumber to its closest end-of-month date. I found an online link but it is very inefficient for a large matrix (at http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times). Does Matlab (Financial Toolbox) has an inbuilt function for this? I couldn't find it.
date_in = 734421 ;
somefunction(date_in) --> Sept 2010
Thanks!
Basically, it sounds like you are asking for whether a given date is closer to the preceding or following month. You can greatly simplify the logic involved if you use the functions EOMDAY to find the date for the end of the month and ADDTODATE to shift the current month up or down by one. Here's an example function that takes a date number as input:
function closestString = closest_month(dateNumber)
dateVector = datevec(dateNumber);
daysInMonth = eomday(dateVector(1),dateVector(2));
if dateVector(3) > daysInMonth/2
dateNumber = addtodate(dateNumber,1,'month');
else
dateNumber = addtodate(dateNumber,-1,'month');
end
closestString = datestr(dateNumber,'mmm yyyy');
end
I had some errors in my previous version. Here's the logic incorporated into a function. It also checks for the month and updates accordingly.
function out = roundMonth(dateNumber)
dateVector = datevec(dateNumber);
day = dateVector(3);
month = dateVector(2);
year = dateVector(1);
month = month + sign(day - 15 + double(~(month-2)))...
+ double(~(day-15 + double(~(month-2))));
dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
dateVector(2) = mod(month,12) + 12*double(~mod(month,12));
out = datestr(dateVector,'mmm yyyy');
EXAMPLES:
1.
roundMonth(datenum('10-Oct-2010'))
ans =
Sep 2010
2.
roundMonth(datenum('20-Oct-2010'))
ans =
Nov 2010
3.
roundMonth(datenum('20-Dec-2010'))
ans =
Jan 2011
4.
roundMonth(datenum('10-Jan-2010'))
ans =
Dec 2009

How do I convert microseconds into a timestamp?

I took this piece from an unencrypted .DAT file:
Code:
00 e1 27 17 6f e6 69 c0
Which translates to 63,374,851,375,000,000 in decimal. The units for the number are microseconds.
And this huge number cannot bypass the 1st January 1970 00:00:00 format; such a format that most converters use today.
So, yes. Is there such a converter that uses the 1st January of the year 1 format? Or how shall I make one?
And by the way, a timestamp is both date and time.
Thanks in advance!
You do not say what language are you using, if it is a .NET language, you can use: http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx for that constructor the input is in nanoseconds (are you sure that your number is in milliseconds and not in nanoseconds?).
If you are sure it is in milliseconds, the conversion to nanoseconds should be easy: 1 millisecond = 1 000 000 nanoseconds.
But I have the feeling that those are nanoseconds and not milliseconds...
Now that you have told us that it is in microseconds:
C# Example from decimal to yyyy dd MM hh:mm:ss
long microseconds = 63370738175000000;
long ticks = microseconds * 10;
DateTime timestamp = new DateTime(ticks);
Console.WriteLine(timestamp.ToString("yyyy dd MM hh:mm:ss"));
It prints:
2009 20 02 02:49:35
The other way around from yyyy dd MM hh:mm:ss to decimal
String dateString = "2009 20 02 02:49:35";
DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd MM hh:mm:ss",CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
Console.WriteLine(microseconds);
It prints:
63370694975000000
And if you want it in hexadecimal just write:
Console.WriteLine(microseconds.ToString("X"));
Then it will print:
E1234FB3278DC0
If you want the answer in another programming language, please add that to you question.
In JAVA in order to convert microseconds into java.sql.Timestamp:
public static Timestamp getTimestampFromMicros(long pMicros) {
long millis = TimeUnit.MICROSECONDS.toMillis(pMicros);
long shaaritInMicros = pMicros - TimeUnit.MILLISECONDS.toMicros(millis);
Timestamp ts = new Timestamp(millis);
long nanos = ts.getNanos() + TimeUnit.MICROSECONDS.toNanos(shaaritInMicros);
ts.setNanos((int)nanos);
return ts;
}
Use below Java code to covert microseconds to date and time,
long msec = microseconds * 1/1000;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.format(msec);
Which will returns,
2016-01-27 03:41:12