Beispiel #1
0
        static PhpValue GetSunTime(Context ctx, int timestamp, TimeFormats format, double latitude, double longitude, double zenith, double offset, bool getSunrise)
        {
            var zone  = PhpTimeZone.GetCurrentTimeZone(ctx);
            var utc   = DateTimeUtils.UnixTimeStampToUtc(timestamp);
            var local = TimeZoneInfo.ConvertTime(utc, zone);

            if (Double.IsNaN(latitude) || Double.IsNaN(longitude) || Double.IsNaN(zenith))
            {
                //LibraryConfiguration config = LibraryConfiguration.GetLocal(ScriptContext.CurrentContext);

                //if (Double.IsNaN(latitude))
                //    latitude = config.Date.Latitude;
                //if (Double.IsNaN(longitude))
                //    longitude = config.Date.Longitude;
                //if (Double.IsNaN(zenith))
                //    zenith = (getSunrise) ? config.Date.SunriseZenith : config.Date.SunsetZenith;
                throw new NotImplementedException();
            }

            if (Double.IsNaN(offset))
            {
                offset = zone.GetUtcOffset(local).TotalHours;
            }

            double result_utc = CalculateSunTime(local.DayOfYear, latitude, longitude, zenith, getSunrise);
            double result     = result_utc + offset;

            switch (format)
            {
            case TimeFormats.Integer:
                return(PhpValue.Create((timestamp - (timestamp % (24 * 3600))) + (int)(3600 * result)));

            case TimeFormats.String:
                return(PhpValue.Create(string.Format("{0:00}:{1:00}", (int)result, (int)(60 * (result - (double)(int)result)))));

            case TimeFormats.Double:
                return(PhpValue.Create(result));

            default:
                //PhpException.InvalidArgument("format");
                //return PhpValue.Null;
                throw new ArgumentException();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns a part of a specified timestamp.
        /// </summary>
        /// <param name="format">Format definition for output.</param>
        /// <param name="timestamp">Nuber of seconds since 1970 specifying a date.</param>
        /// <returns>Part of the date, e.g. month or hours.</returns>
        public static int idate(Context ctx, string format, int timestamp)
        {
            if (format == null || format.Length != 1)
            {
                //PhpException.InvalidArgument("format");
                throw new ArgumentException();
            }

            return(GetDatePart(format[0], DateTimeUtils.UnixTimeStampToUtc(timestamp), PhpTimeZone.GetCurrentTimeZone(ctx)));
        }
Beispiel #3
0
        private int GetUnixTimeStamp(Context ctx, System.DateTime utcStart, out string error)
        {
            var zone  = PhpTimeZone.GetCurrentTimeZone(ctx);
            var start = TimeZoneInfo.ConvertTime(utcStart, TimeZoneInfo.Utc, zone);// zone.ToLocalTime(utcStart);

            // following operates on local time defined by the parsed info or by the current time zone //

            if (have_date > 0 && have_time == 0)
            {
                h = 0;
                i = 0;
                s = 0;
            }
            else
            {
                if (h == -1)
                {
                    h = start.Hour;
                }
                if (i == -1)
                {
                    i = start.Minute;
                }
                if (s == -1)
                {
                    s = start.Second;
                }
            }

            if (y == -1)
            {
                y = start.Year;
            }
            if (m == -1)
            {
                m = start.Month;
            }
            else if (m == 0)
            {
                m = 1; --relative.m;
            }
            if (d == -1)
            {
                d = start.Day;
            }
            else if (d == 0)
            {
                d = 1; --relative.d;
            }

            int days_overflow;

            CheckOverflows(y, m, ref d, ref h, out days_overflow);

            var result = new System.DateTime(y, m, d, h, i, s, DateTimeKind.Unspecified);

            result = result.AddDays(relative.d + days_overflow);
            result = result.AddMonths(relative.m);
            result = result.AddYears(relative.y);
            result = result.AddHours(relative.h);
            result = result.AddMinutes(relative.i);
            result = result.AddSeconds(relative.s);

            // adds relative weekday:
            if (have_weekday_relative > 0)
            {
                int dow        = (int)result.DayOfWeek;
                int difference = relative.weekday - dow;

                if ((relative.d < 0 && difference < 0) || (relative.d >= 0 && difference <= -relative.weekday_behavior))
                {
                    difference += 7;
                }

                if (relative.weekday >= 0)
                {
                    result = result.AddDays(difference);
                }
                else
                {
                    result = result.AddDays(dow - relative.weekday - 7);
                }
            }

            // convert to UTC:
            if (have_zone > 0)
            {
                result = result.AddMinutes(-z);
            }
            else
            {
                if (zone.IsInvalidTime(result))
                {
                    // We ended up in an invalid time. This time was skipped because of day-light saving change.
                    // Figure out the direction we were moving, and step in the direction until the next valid time.
                    int secondsStep = ((result - utcStart).Ticks >= 0) ? 1 : -1;
                    do
                    {
                        result = result.AddSeconds(secondsStep);
                    }while (zone.IsInvalidTime(result));
                }

                result = TimeZoneInfo.ConvertTime(result, zone, TimeZoneInfo.Utc);// zone.ToUniversalTime(result);
            }

            error = null;
            return(DateTimeUtils.UtcToUnixTimeStamp(result));
        }