Exemplo n.º 1
0
        /// <summary>
        /// Returns a part of current time.
        /// </summary>
        /// <param name="format">Format definition for output.</param>
        /// <returns>Part of the date, e.g. month or hours.</returns>
        public static int idate(Context ctx, string format)
        {
            if (format == null || format.Length != 1)
            {
                //PhpException.InvalidArgument("format");
                throw new ArgumentException();
            }

            return(GetDatePart(format[0], System_DateTime.UtcNow, PhpTimeZone.GetCurrentTimeZone(ctx)));
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns an associative array containing the date information.
        /// </summary>
        /// <param name="utc">UTC date time.</param>
        /// <returns>Associative array with date information.</returns>
        static PhpArray GetDate(Context ctx, System_DateTime utc)
        {
            PhpArray result = new PhpArray(1, 10);

            var zone  = PhpTimeZone.GetCurrentTimeZone(ctx);
            var local = TimeZoneInfo.ConvertTime(utc, zone);

            result.Add("seconds", local.Second);
            result.Add("minutes", local.Minute);
            result.Add("hours", local.Hour);
            result.Add("mday", local.Day);
            result.Add("wday", (int)local.DayOfWeek);
            result.Add("mon", local.Month);
            result.Add("year", local.Year);
            result.Add("yday", local.DayOfYear - 1); // PHP: zero based day count
            result.Add("weekday", local.DayOfWeek.ToString());
            result.Add("month", local.ToString("MMMM", DateTimeFormatInfo.InvariantInfo));
            result.Add(0, DateTimeUtils.UtcToUnixTimeStamp(utc));

            return(result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// The localtime() function returns an array identical to that of the structure returned by the C function call.
 /// The first argument to localtime() is the timestamp. The second argument to the localtime() is
 /// the isAssociative, if this is set to <c>false</c> than the array is returned as a regular, numerically indexed array.
 /// If the argument is set to <c>true</c> then localtime() is an associative array containing all the different
 /// elements of the structure returned by the C function call to localtime.
 /// </summary>
 /// <remarks>
 /// Returned array contains these elements if isAssociative is set to true:
 /// <list type="bullet">
 /// <term><c>"tm_sec"</c></term><description>seconds</description>
 /// <term><c>"tm_min"</c></term><description>minutes</description>
 /// <term><c>"tm_hour"</c></term><description>hour</description>
 ///	<term><c>"tm_mday"</c></term><description>day of the month</description>
 ///	<term><c>"tm_mon"</c></term><description>month of the year, starting with 0 for January</description>
 /// <term><c>"tm_year"</c></term><description>Years since 1900</description>
 /// <term><c>"tm_wday"</c></term><description>Day of the week</description>
 /// <term><c>"tm_yday"</c></term><description>Day of the year</description>
 /// <term><c>"tm_isdst"</c></term><description>Is daylight savings time in effect</description>
 /// </list>
 /// </remarks>
 /// <param name="timestamp"></param>
 /// <param name="returnAssociative"></param>
 /// <returns></returns>
 public static PhpArray localtime(Context ctx, int timestamp, bool returnAssociative)
 {
     return(GetLocalTime(PhpTimeZone.GetCurrentTimeZone(ctx), DateTimeUtils.UnixTimeStampToUtc(timestamp), returnAssociative));
 }
Exemplo n.º 5
0
 /// <summary>
 /// The localtime() function returns an array identical to that of the structure returned by the C function call.
 /// Current time is used, regular numericaly indexed array is returned.
 /// </summary>
 /// <returns>Array containing values specifying the date and time.</returns>
 public static PhpArray localtime(Context ctx)
 {
     return(GetLocalTime(PhpTimeZone.GetCurrentTimeZone(ctx), System_DateTime.UtcNow, false));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Gets time information.
 /// </summary>
 /// <remarks>
 /// It returns <see cref="PhpArray"/> containing the following 4 entries:
 /// <list type="table">
 /// <item><term><c>"sec"</c></term><description>Unix timestamp (seconds since the Unix Epoch)</description></item>
 /// <item><term><c>"usec"</c></term><description>microseconds</description></item>
 /// <item><term><c>"minuteswest"</c></term><description>minutes west of Greenwich (doesn't take daylight savings time in consideration)</description></item>
 /// <item><term><c>"dsttime"</c></term><description>type of DST correction (+1 or 0, determined only by the current time zone not by the time)</description></item>
 /// </list>
 /// </remarks>
 /// <returns>Associative array</returns>
 public static PhpArray gettimeofday(Context ctx)
 {
     return(GetTimeOfDay(System_DateTime.UtcNow, PhpTimeZone.GetCurrentTimeZone(ctx)));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Returns a string formatted according to the given format string using the given integer timestamp.
 /// </summary>
 /// <param name="format">Format definition for output.</param>
 /// <param name="timestamp">Nuber of seconds since 1970 specifying a date.</param>
 /// <returns>Formatted string.</returns>
 public static string date(Context ctx, string format, int timestamp)
 {
     return(FormatDate(format, DateTimeUtils.UnixTimeStampToUtc(timestamp), PhpTimeZone.GetCurrentTimeZone(ctx)));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns a string formatted according to the given format string using the current local time.
 /// </summary>
 /// <param name="format">Format definition for output.</param>
 /// <returns>Formatted string.</returns>
 public static string date(Context ctx, string format)
 {
     return(FormatDate(format, System_DateTime.UtcNow, PhpTimeZone.GetCurrentTimeZone(ctx)));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Gets the current local time with respect to the current PHP time zone.
 /// </summary>
 internal static System_DateTime GetNow(Context ctx)
 {
     return(TimeZoneInfo.ConvertTime(System_DateTime.UtcNow, PhpTimeZone.GetCurrentTimeZone(ctx)));
 }
Exemplo n.º 10
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)));
        }
Exemplo n.º 11
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));
        }