hijri (islamic) calendar problem! - c++

I would convert Gregorian date to Hijri (Islamic) date. After may search on the web, I found a source code to convert it.
I converted the code from Java and PHP to C base.
The implement some times working without any problem. But some days has problem.
I need your help either fix the implement or a available code that will work without any problem!
BTW I found another source code (http://emr.cs.iit.edu/~reingold/calendar.C) that is C++ base. As I don't know C++ if anyone can convert that to C Base or Objective C would be prefect (still not sure this code will work correctly or not).
P.S. You can check the correct date in: islamicfinder.org/Hcal/index.php
void gregorian_to_hijri(int* h_y, int* h_m, int* h_d, int g_y, int g_m, int g_d)
{
int year, month, day;
int zyr;
int zd;
int zm;
int zy;
float zjd;
int zl;
int zn;
int zj;
year = g_y;
month = g_m;
day = g_d;
zyr = year;
zd = day;
zm = month;
zy = zyr;
if((zy > 1582) || ((zy == 1582) && (zm > 10)) || ((zy == 1582) && (zm == 10) && (zd > 14)))
{
zjd = ((1461 * (zy + 4800 + ((zm - 14) / 12))) / 4)
+ ((367 * (zm - 2 - 12 * (((zm - 14) / 12)))) / 12)
- ((3 * (((zy + 4900 + ((zm - 14) / 12)) / 100))) / 4) + zd - 32075;
}
else
{
zjd = 367 * zy - ((7 * (zy + 5001 + ((zm - 9) / 7))) / 4)
+ ((275 * zm) / 9) + zd + 1729777;
}
zl = zjd - 1948440 + 10632;
zn = ((zl - 1) / 10631);
zl = zl - 10631 * zn + 354;
zj = (((10985 - zl) / 5316)) * ((int)((50 * zl) / 17719))
+ ((zl / 5670)) * ((int)((43 * zl) / 15238));
zl = zl - (((30 - zj) / 15)) * (((17719 * zj) / 50))
- ((zj / 16)) * (((15238 * zj) / 43)) + 29;
zm = ((24 * zl) / 709);
zd = zl - ((709 * zm) / 24);
zy = 30 * zn + zj - 30;
*h_y = zy;
*h_m = zm;
*h_d = zd;
}

Assuming this is for a Mac (Cocoa) or iOS (Cocoa Touch) app, since that's where you see Objective C most often, then you can just do something like this:
// Create a Gregorian Calendar
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Set up components of a Gregorian date
NSDateComponents *gregorianComponents = [[NSDateComponents alloc] init];
gregorianComponents.day = 4;
gregorianComponents.month = 12;
gregorianComponents.year = 2010;
// Create the date
NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];
[gregorianComponents release];
[gregorianCalendar release];
// Then create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCivilCalendar];
// And grab those date components for the same date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit |
NSMonthCalendarUnit |
NSYearCalendarUnit)
fromDate:date];
NSLog(#"[In Hijri calendar ->] Day: %ld, Month: %ld, Year:%ld",
[hijriComponents day],
[hijriComponents month],
[hijriComponents year]);
[hijriCalendar release];
If all you want is the current date, then you can skip setting up the gregorian date altogether and just do this:
// Create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
// And grab the date components for the current date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit |
NSMonthCalendarUnit |
NSYearCalendarUnit)
fromDate:[NSDate date]];
[hijriCalendar release];

Have a look at this topic: how to convert hijari date into gregorian date in java script?
The question mentions JavaScript but the top answer seems to have links to implementations in a variety of languages.

You should be able to do this in Objective-C (if that really is an option) using NSCalendar.

Related

Why is my TODAY function in IF formula returning a value other than 0?

I want to write the If statement below in a Google Sheet:
If today's date is greater than the payment deadline, then return the overdue payment amount. Otherwise, return 0.
I did this:
=if(F11<"=TODAY()",C11,0) + if(F12<"=TODAY()",C12,0) + if(F13<"=TODAY()",C13,0) + if(F14<"=TODAY()",C14,0) + if(F15<"=TODAY()",C15,0) + if(F16<"=TODAY()",C16,0) + if(F17<"=TODAY()",C17,0)
The values in column F are dates written the date format month/day/year. All of the dates in column F are greater than today's date.
The values in column C are dollar amounts with decimals.
First Attempt:
=if(F11<"=TODAY()",C11,0) + if(F12<"=TODAY()",C12,0) + if(F13<"=TODAY()",C13,0) + if(F14<"=TODAY()",C14,0) + if(F15<"=TODAY()",C15,0) + if(F16<"=TODAY()",C16,0) + if(F17<"=TODAY()",C17,0)
Second Attempt:
=if(F11<"TODAY()",C11,0) + if(F12<"TODAY()",C12,0) + if(F13<"TODAY()",C13,0) + if(F14<"TODAY()",C14,0) + if(F15<"TODAY()",C15,0) + if(F16<"TODAY()",C16,0) + if(F17<"TODAY()",C17,0)
Third Attempt:
=if(DATEVALUE(F11)<"TODAY()",C11,0) + if(DATEVALUE(F12)<"TODAY()",C12,0) + if(DATEVALUE(F13)<"TODAY()",C13,0) + if(DATEVALUE(F14)<"TODAY()",C14,0) + if(DATEVALUE(F15)<"TODAY()",C15,0) + if(DATEVALUE(F16)<"TODAY()",C16,0) + if(DATEVALUE(F16)<"TODAY()",C17,0)
I expect to get 0 overdue payments as all the payment deadlines are greater than today's day. Could you please help me?
your formula should be:
=IF(F11<TODAY(), C11, 0) +
IF(F12<TODAY(), C12, 0) +
IF(F13<TODAY(), C13, 0) +
IF(F14<TODAY(), C14, 0) +
IF(F15<TODAY(), C15, 0) +
IF(F16<TODAY(), C16, 0) +
IF(F17<TODAY(), C17, 0)
next level formula would be:
=ARRAYFORMULA(SUM(IF(F11:F17<TODAY(), C11:C17, 0)))
or shorter:
=SUMIF(F11:F17, "<"&TODAY(), C11:C17)

Use IF with multiple ELSEIF and ELSE in a spreadsheet cell

I am trying to make this statement applicable to some cells in my google spreadsheet :
if ((B29 - B36) > 0) {
(B29 - B36) * (D36 / 100) + F35 + F34 + F33
} else if ((B29 - B35) > 0) {
(B29 - B35) * (D35 / 100) + F34 + F33);
} else if (B29 - B34 > 0) {
(B29 - B34) * (D34 / 100) + F33);
} else {
0
}
I tried to make it with only IF, but the cells didn't like the syntax :
=IF((B29 - B36) > 0);((B29 - B36) * (D36 / 100) + F35 + F34 + F33);IF((B29 - B35) > 0);((B29 - B35) * (D35 / 100) + F34 + F33);IF((B29 - B34) > 0);((B29 - B34) * (D34 / 100) + F33);0
How can I achieve this?
correct syntax would be like this:
=IF(B29-B36 > 0; (B29-B36)*(D36/100)+F35+F34+F33;
IF(B29-B35 > 0; (B29-B35)*(D35/100)+F34+F33;
IF(B29-B34 > 0; (B29-B34)*(D34/100)+F33; 0)))

Adding minutes by literals

ho could I manipulate some Int adding them as minutes and sum them? the result should be in hours and minutes just like 1:15 or 6:30.
My playground gives 1.25 but I expected 1.15
struct standardDayOfWork {
var dailyHours : Double = 0
}
var dayToUse = standardDayOfWork()
enum hourFractions : Double {
case quarter = 15
case half = 30
case threeQuarter = 45
case hour = 60
}
dayToUse.dailyHours += hourFractions.half.rawValue
dayToUse.dailyHours += hourFractions.half.rawValue
dayToUse.dailyHours += hourFractions.quarter.rawValue
var total = dayToUse.dailyHours / 60 //1.25
Because in the decimal system a quarter is 0.25.
To get numeric 1.15 you could use this weird expression:
var total = Double(Int(dayToUse.dailyHours) / 60) + (dayToUse.dailyHours.truncatingRemainder(dividingBy: 60) / 100.0)
Or if you can live with a formatted "hh:mm" string I'd recommend
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
formatter.string(from: dayToUse.dailyHours * 60)

Calculating distance and speed using gps coordinates using qt c++

I am attempting to convert the first code snippet at:
Calculate distance between 2 GPS coordinates
// var R = 6371; // km
// var dLat = (lat2-lat1).toRad();
// var dLon = (lon2-lon1).toRad();
// var lat1 = lat1.toRad();
// var lat2 = lat2.toRad();
// var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
// Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
// var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// var d = R * c;
(also shown below) for use in qt c++. It is used in a program that simulates gps flight inside of a radio control simulator. The GpsCalcLoop grabs gps latitude and longitude (in decimal format) once per second. These are fed into my converted code in hopes of getting a distance traveled during that one second of time. Speed would then be calculated.
I used the information at this page:
http://doc.qt.io/qt-4.8/qtcore-qmath-h.html
to convert the math functions. The output, however, is not what I expected. No matter what is input into lat2, lat1, lon2, and lon1 the results are always the same, '16777200.00'.
I am hoping that someone more familiar with qt c++ will be able to show me where I have gone wrong with the conversion. or point me to where I can find qt c++ code that is working.
I will add that I have attempted to use "#include "<"QGeoPositionInfo">" but get a 'No such file or directory' error when running the program.
void TelemetrySimulator::onGpsCalcLoop()
{
if(rungps)
{
ui -> valuetest1 -> setValue(flat);
ui -> valuetest2 -> setValue(flat2);
ui -> valuetest3 -> setValue(flon);
ui -> valuetest4 -> setValue(flon2);
ui -> valuetest5 -> setValue(flat-flat2);
ui -> valuetest6 -> setValue(flon-flon2);
flat2 = flat;
flon2 = flon;
//--https://stackoverflow.com/questions/365826 //calculate-distance-between-2-gps-coordinates--
// var R = 6371; // km
// var dLat = (lat2-lat1).toRad();
// var dLon = (lon2-lon1).toRad();
// var lat1 = lat1.toRad();
// var lat2 = lat2.toRad();
// var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
// Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
// var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// var d = R * c;
//--------------my conversio to qt c++---------
//---converted to qt using this web page -> http://doc.qt.io/qt-4.8/qtcore-qmath-h.html----
double R, dLat, dLon, lat1, lat2, lon2, lon1, a, c, d;
lat2 = flat2;
lat1 = flat;
lon2 = flon2;
lon1 = flon;
R = 6371; // km
dLat = qAcos(lat2-lat1); //dLat = (lat2-lat1).toRad(); instead of qAcos could be qAsin, qAtan, qCos, qSin,
// qTan. All refer to Radials.
dLon = qAcos(lon2-lon1); //dLon = (lon2-lon1).toRad();
lat1 = qAcos(lat1); //lat1 = lat1.toRad();
lat2 = qAcos(lat2); //lat2 = lat2.toRad();
a = qSin(dLat/2) * qSin(dLat/2) + //Math.sin(dLat/2) * Math.sin(dLat/2) +
qSin(dLon/2) * qSin(dLon/2) * qCos(lat1) * qCos(lat2);//Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
c = 2 * qAtan2(qSqrt(a), qSqrt(1-a)); //Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
d = R * c;
ui -> valuetest7 -> setValue(d);
}
}
Thanks to everyone for your great help! Here is the final code:
void MainWindow::onGpsCalcLoop()
{
//Get initial values from gps run loop-------
double lat2 = flat2;
double lat1 = flat;
double lon2 = flon2;
double lon1 = flon;
//GPS distance and speed calculation -------
double dLat = .01745329 * (lat2 - lat1);
double dLon = .01745329 * (lon2 - lon1);
lat1 = .01745329 * lat1;
lat2 = .01745329 * lat2;
double a = qSin(dLat/2) * qSin(dLat/2) +
qSin(dLon/2) * qSin(dLon/2) * qCos(lat1) * qCos(lat2);
double c = 2 * qAtan2(qSqrt(a), qSqrt(1-a));
double d = 3975 * c;
//Distance calculation done. Next is total distance and speed------
td = td + d;
if (d > .1)
{
d = 0;
td = 0;
}
if (ui -> metric -> isChecked())
{
ui -> valuetest1 -> setValue(td * 1.6);
ui -> valuetest2 -> setValue((d * 3600) * 1.6);
}
else
{
ui -> valuetest1 -> setValue(td);
ui -> valuetest2 -> setValue(d * 3600);
}
flat2 = flat;
flon2 = flon;
}
I used .01745329*(whatever) instead of qDegreesToRadians(whatever) just to keep things as simple as possible.
Again, thanks to all for your help!

Modulus in Pascal

I am trying to translate some Pascal code into C++ code. I am stuck trying to figure out how to translate this portion.
Function ThetaG_JD(jd : double) : double;
var
UT,TU,GMST : double;
begin
**UT := Frac(jd + 0.5);**
jd := jd - UT;
TU := (jd - 2451545.0)/36525;
GMST := 24110.54841 + TU * (8640184.812866 + TU * (0.093104 - TU * 6.2E-6));
**GMST := Modulus(GMST + 86400.0*1.00273790934*UT,86400.0);**
ThetaG_JD := twopi * GMST/86400.0;
end; {Function ThetaG_JD}
I am particularly having trouble with the two lines I made bold. How can I translate this to c++? Thank you so much.
In C++ the equivalent functions would be:
fmod to get a floating point modulus
modf to break a floating point item into its fraction and integral parts (equivalent of Frac).
If you want to calculate Julian Day, Greenwich Mean Sidereal Time and Local Mean Sidereal Time, maybe the below can help you - written in PowerShell:
<#
.Synopsis
Astronomy calculations
.Description
Some helper functions to calculate:
- Julian Day,
- Greenwich Mean Sidereal time,
- Local Mean Sidereal Time.
#>
cls
# https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
function Get-JulianDay
{
param ( [System.DateTime]$dt )
$year = $dt.Year
$month = $dt.Month
$day = $dt.Day
$hour = $dt.Hour
$minute = $dt.Minute
$second = $dt.Second
$a = [System.Math]::Floor((14 - $month) / 12)
$y = $year + 4800 - $a
$m = $month + 12 * $a - 3
$JDN = $day + [System.Math]::Floor((153 * $m + 2) / 5) + 365 * $y + [System.Math]::Floor($y / 4) - [System.Math]::Floor($y / 100) + [System.Math]::Floor($y / 400) - 32045
$JD = $JDN + ($hour - 12) / 24 + $minute / 1440 + $second / 86400
return ($JD)
}
# https://en.wikipedia.org/wiki/Sidereal_time#Definition
# http://aa.usno.navy.mil/faq/docs/GAST.php
function Get-GMST
{
param ( [double]$JD )
$D = $JD - 2451545.0
$GMST = 18.697374558 + 24.06570982441908 * $D
return ($GMST % 24)
}
function Get-LMST
{
param ( [double]$gmst, [double]$longitude )
return ( $gmst + $longitude / 15.0 )
}
# Test above functions
$current = (Get-Date).ToUniversalTime()
$jd = Get-JulianDay -dt $current
$gmst = Get-GMST -JD $jd
$longitude = 17.668487800
$lmst = Get-LMST -gmst $gmst -longitude $longitude
$lst = [timespan]::FromHours($lmst).ToString()
Write-Host "Local mean sidereal time: $lst"