0

I have the following statement:

All times are in UTC and summer time adjustments are not included in the returned data.

With the following json:

{
    "results":{
        "sunrise":"10:45:50 PM",
        "sunset":"10:51:51 AM",
        "solar_noon":"4:48:50 AM",
        "day_length":"12:06:01",
        "civil_twilight_begin":"10:25:26 PM",
        "civil_twilight_end":"11:12:15 AM",
        "nautical_twilight_begin":"10:00:25 PM",
        "nautical_twilight_end":"11:37:16 AM",
        "astronomical_twilight_begin":"9:35:23 PM",
        "astronomical_twilight_end":"12:02:18 PM"
    },
    "status":"OK"
}

This json is the result from my app in "CET" for Singapore. I need to include the time adjustment so I can display the sunrise (06:46) and sunset (18:52) in local Singapore time. ...or other parts of the world.

I have been struggling getting this correctly done in a good way. I have tried several suggestions, including manually calculating, found when googling but still have complexity problems. I would like to ask the community for some help solving this in an effective way.

PeterK
  • 4,243
  • 4
  • 44
  • 74
  • 4
    Use `DateTime.Parse`/`DateTime.TryParse` to convert the string to a DateTime, then `DateTime.ToLocalTime` to convert it to the local timezone. If you want to use a specific timezone instead, see: [How to convert DateTime in Specific timezone?](https://stackoverflow.com/q/9869051/8967612) – 41686d6564 stands w. Palestine Nov 07 '22 at 10:58
  • I am wondering, too: Where does the CET come into play? You stated that the json contains UTC, only. Now, for application of the correct Daylight Savings on top of the local TimeZone, you need to have the date, too. – Fildor Nov 07 '22 at 11:05
  • Well the CET is the time zone I am currently in so I guess I had to cater to that for the actual calculation or am I wrong. – PeterK Nov 07 '22 at 11:08
  • `DateTime.Parse` on a time only will automatically include the current date, and give a `DateTimeKind` of `Unspecified`, which `.ToLocalTime()` will convert to local under the assumption it's UTC (though an explicit call to `DateTime.SpecifyKind` for clarity is advisable). – Jeroen Mostert Nov 07 '22 at 11:29
  • @ 41686d6564 stands w. Palestine I think that fixed it, at least it looks like that. – PeterK Nov 07 '22 at 11:29
  • DateTime _myTime = DateTime.Parse("10:45:50 PM"); DateTime utcTime = _myTime.ToUniversalTime(); TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Asia/Singapore"); DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(_myTime, timeInfo); print(utcTime + " >> " + userTime); 11 / 7 / 2022 9:45:50 PM >> 11 / 8 / 2022 6:45:50 AM – PeterK Nov 07 '22 at 11:30

0 Answers0