日落和日出时间计算 [英] Sunset and Sunrise Timing Calculation

查看:79
本文介绍了日落和日出时间计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有以下C#代码来计算日出和日落时间

Hi,
I have following code of C# to calculate Sunrise and Sunset time

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);
            }
        }
    }


当我运行asp.net MVc页面时,它同时显示两个.我认为计算定时的公式不起作用,或者View中可能存在一些问题,我也在发布view代码

请让我知道如何处理这种情况,查看代码如下所示


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

@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

推荐答案

您似乎正在使用代码项目"文章中的库(
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:

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



这是一个错误检查,似乎并没有真正起作用.唯一的问题是,现在您将不再检查有效的坐标,但是由于检查首先没有进行,因此这不应该成为问题.

之所以返回相同的值,是因为此错误检查使该方法失败.由于日出和日落变量作为ref对象传递,因此它们保留最初设置的值(运行应用程序的时间).

如果发现值不完全准确,则可能需要检入该文章的注释. Rafone设置了一条称为一个不错的问题"的注释,在此之后,经过几次回复,他发布了修改后的代码,以获得更好的结果.



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.


事实上,文件运行正常,我只需要更改经度和纬度的值即可,因为我在My Prayer控制器中的工作位置是
完成了
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


这篇关于日落和日出时间计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆