Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
Hi,
I have following code of C# to calculate Sunrise and Sunset time
C#
public class PrayerController : Controller
    {
        IPrayerService _cService;
        System.Guid applicationID;
        public PrayerController()
        {
            applicationID = (System.Guid)Helpers.Utility.GetApplicationID;
            _cService = new PrayerServiceClient();
        }
        public PrayerController(IPrayerService svc)
        {
            _cService = svc;
           
        }

        public ActionResult Index()
        {
            DateTime date = DateTime.Today;
            bool isSunrise = false;
            bool isSunset = false;
            DateTime sunrise = DateTime.Now;
            DateTime sunset = DateTime.Now;
            SunTimes.Instance.CalculateSunRiseSetTimes(new SunTimes.LatitudeCoords
                                   (39, 16, 19, SunTimes.LatitudeCoords.Direction.North),
                                                new SunTimes.LongitudeCoords
                                   (76, 29, 41, SunTimes.LongitudeCoords.Direction.West),
                                                date, ref sunrise, ref sunset,
                                 ref isSunrise, ref isSunset);
            ViewData["sunSet"] = sunset.ToShortTimeString();
            ViewData["sunRise"] = sunrise.ToShortTimeString();
            List<Prayer> model = _cService.GetPrayerCollectionList(applicationID).ToList();
            return View(model);
            
        }
 
        public ActionResult Edit(int id)
        {
            Prayer model = _cService.getPrayer(id, applicationID);
            if (model != null)
                return View(model);
            else
                return View();
        }

        //
        // POST: /Prayer/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Prayer model)
        {
            try
            {
                _cService.SavePrayer(model, applicationID);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
    }

when I run the asp.net MVc Page it shows both Same times I think formula to Calculate Timings is not working or there may be some problem in View I am also posting the code of view

Please let me know what can I do handle this situation, Code for view is given below
C#
@model IEnumerable<CMg.Data.Prayer>

@section PrayerIndex{
    <h2>Index</h2>

    <table class="data-table">
        <tr><td colspan="4"> Today's Sun Rise Time: @ViewData["sunRise"] <br />
        Today's Sun-Set Time: @ViewData["sunSet"]
        </td></tr>
        <tr align="left">
        @if (HttpContext.Current.User.Identity.Name == "anagrah")
          { 
            <th style="width:100px;"></th>  
              }        
            <th align="left" style="width:130px;">
                PrayerName
            </th>
            <th align="left" style="width:130px;">
                StartTime
            </th>
            <th align="left" style="width:130px;">
                EndTime
            </th>
        </tr>

    @{ foreach (var item in Model) { 
    
        <tr align="left">
            @if (HttpContext.Current.User.Identity.Name == "anagrah")
              { 
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.PrayerID })                
            </td>       
            }   
            <td>
                @Html.Encode(item.PrayerName)
            </td>
            <td>
            @if (item.PrayerName.Contains("Maghrib")) 
            {  @Html.Encode(System.Convert.ToDateTime(ViewData["sunSet"]).AddMinutes(2).ToShortTimeString())}
            @if (!item.PrayerName.Contains("Maghrib")) 
            {@Html.Encode(item.StartTime)}
            </td>
            <td>
                @if (item.PrayerName.Contains("Maghrib")) 
                 {@Html.Encode(System.Convert.ToDateTime(ViewData["sunSet"]).AddMinutes(22).ToShortTimeString())}
            @if (!item.PrayerName.Contains("Maghrib"))                 
            {@Html.Encode(item.EndTime)}
            </td>
        </tr>
    
     } }

    </table>

Please give me some suggestions
Thank You
Posted

It looks like you are using a library from a Code Project article (C# Class for Calculating Sunrise and Sunset Times[^]). There seems to be a bug or two in the code an the author is no longer updating the code. However, I believe the fix will be to remove the following lines from the author's code:

C#
if ((Sign(zone) == Sign(lon)) && (zone != 0))
{
   Debug.Print("WARNING: time zone and longitude are incompatible!");
   return false;
}


This is an error check that doesn't seem to really work. The only issue is that now you will not be checking for valid coordinates, but since the check didn't work in the first place, that shouldn't be an issue.

The reason why you were returning the same value is because this error check fails the method. Since your sunrise and sunset variables are passed in as ref objects, they retain the values they were initially set with (which was the time when the application was run).

If you find that the values aren't fully accurate, you might want to check in the comments of that article. There is a comment set by Rafone called "Nice one but question" where, after a couple responses, he posts code he modified to get better results. That might be something to look into.
 
Share this answer
 
Comments
Sandeep Mewara 5-Jun-12 10:18am    
Good answer. 5!
M Ali Qadir 7-Jun-12 8:41am    
I have removed the Code you suggested, but not working yet, In fact my page showing same sunrise and sunset, what else I can do?? I don't Think I have Problem that is mentioned in "Nice one but question"
Tim Corey 7-Jun-12 8:51am    
Step through the code to see where the method is going wrong. I was able to re-create your problem using your code and then I solved it with the above solution so there must be something else that is different about your code. If you step through the code, you will see where the information gets changed (or not changed).
M Ali Qadir 8-Jun-12 6:52am    
Should I go through the SunTimes Class or Controller Class??
Tim Corey 8-Jun-12 7:37am    
Put your breakpoint at the entry point of the SunTimes class and walk through the code from there. The issue is in that class.
In fact that file was Working Correctly, I just have to change the Values of longitudes and latitudes to due my work location in My Prayer controller
and it's done
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900