Exemple #1
0
        /// <summary>
        /// This method gets the timezone
        /// of a given location
        /// </summary>
        /// <param name="input"></param>
        /// <returns>location timezone</returns>
        public Timezone GetTimeZone(TimeZoneInput input)
        {
            /* Create URL based on input paramters */
            string apiURL = ApiBaseURL + "tz.ashx?q=" + input.query + "&format=" + input.format + "&callback=" + input.callback + "&key=" + APIKey;

            /* Get the web response*/
            string result = RequestHandler.Process(apiURL);

            /*Serialize the json output and parse in the helper class*/
            Timezone timeZone = (Timezone) new JavaScriptSerializer().Deserialize(result, typeof(Timezone));

            return(timeZone);
        }
        /// <summary>
        /// This method consumes the World Weather
        /// Premium API to show location TimeZone in JSON Format
        /// </summary>
        public void GetTimeZone()
        {
            try {
                /* Set input parameters for the API */
                String        Format = "JSON";
                TimeZoneInput input  = new TimeZoneInput();
                input.query  = SearchTextBox.Text;
                input.format = Format;

                /* Call GetTimeZone method with input parameters */
                API_Implementation api      = new API_Implementation();
                Timezone           timeZone = api.GetTimeZone(input);

                /*Display Results*/
                DisplayResultsTextBox.Text  = "\r\n Local Time: " + timeZone.data.time_zone[0].localtime;
                DisplayResultsTextBox.Text += "\r\n Time Offset: " + timeZone.data.time_zone[0].utcOffset;
            }
            catch (Exception ex) {
                ex.GetBaseException();
            }
        }
        public IActionResult TimeZone([FromBody] TimeZoneInput input)
        {
            var utcTime    = DateTime.UtcNow;
            var serverTime = DateTime.Now;
            var ip         = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var timeZoneId = TimeZoneInfo.FindSystemTimeZoneById(input.Zone);
                var returnVal  = new CurrentTimeQuery
                {
                    UTCTime  = utcTime,
                    ClientIp = ip,
                    Time     = TimeZoneInfo.ConvertTime(serverTime, timeZoneId)
                };

                using (var db = new ClockworkContext())
                {
                    /*TODO: to make this works properly, use tools > NuGet Package Manager > Package Manager Counsole, open "20171207214527_InitialCreate"
                     * in Migration folder and type in "Update-Database" to initialize the database, also check index.cshtml in clockwork.web, get url there need to be updated as well*/
                    db.CurrentTimeQueries.Add(returnVal);
                    var count = db.SaveChanges();
                    Console.WriteLine("{0} records saved to database", count);

                    Console.WriteLine();
                    foreach (var CurrentTimeQuery in db.CurrentTimeQueries)
                    {
                        Console.WriteLine(" - {0}", CurrentTimeQuery.UTCTime);
                    }
                }

                return(Ok(returnVal));
            }
            catch
            {
                return(StatusCode(222, "invalid timezone"));
            }
        }