UtcToUnixTimeStamp() static private method

Converts System_DateTime representing UTC time to UNIX timestamp.
static private UtcToUnixTimeStamp ( System.DateTime dt ) : int
dt System.DateTime Time.
return int
Ejemplo n.º 1
0
        static int GetSwatchBeat(System_DateTime utc)
        {
            int seconds = DateTimeUtils.UtcToUnixTimeStamp(utc);
            int beat    = (int)(((seconds - (seconds - ((seconds % 86400) + 3600))) * 10) / 864) % 1000;

            return((beat < 0) ? beat + 1000 : beat);
        }
Ejemplo n.º 2
0
        internal static PhpArray GetTimeOfDay(System_DateTime utc, TimeZoneInfo /*!*/ zone)
        {
            var result = new PhpArray(0, 4);

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

            //int current_dst = 0;
            if (zone.IsDaylightSavingTime(local))
            {
                // TODO: current_dst
                //var rules = zone.GetAdjustmentRules();
                //for (int i = 0; i < rules.Length; i++)
                //{
                //    if (rules[i].DateStart <= local && rules[i].DateEnd >= local)
                //    {
                //        current_dst = (int)rules[i].DaylightDelta.TotalHours;
                //        break;
                //    }
                //}
            }

            const int ticks_per_microsecond = (int)TimeSpan.TicksPerMillisecond / 1000;

            result["sec"]         = PhpValue.Create(DateTimeUtils.UtcToUnixTimeStamp(utc));
            result["usec"]        = PhpValue.Create((int)(local.Ticks % TimeSpan.TicksPerSecond) / ticks_per_microsecond);
            result["minuteswest"] = PhpValue.Create((int)(utc - local).TotalMinutes);
            //result["dsttime"] = PhpValue.Create(current_dst);

            return(result);
        }
Ejemplo n.º 3
0
        public static long getlastmod(Context ctx)
        {
            try
            {
                var file = ctx.MainScriptFile.Path;
                if (file == null)
                {
                    return(-1);
                }

                return(DateTimeUtils.UtcToUnixTimeStamp(System.IO.File.GetLastWriteTime(System.IO.Path.Combine(ctx.RootPath, file)).ToUniversalTime()));
            }
            catch (System.Exception)
            {
                return(-1);
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the last modified time of the given file (does not work with directories.)
        /// </summary>
        /// <param name="ftp_stream">The link identifier of the FTP connection.</param>
        /// <param name="remote_file">The file from which to extract the last modification time.</param>
        /// <returns>Returns the last modified time as a Unix timestamp on success, or -1 on error.</returns>
        public static long ftp_mdtm(PhpResource ftp_stream, string remote_file)
        {
            var resource = ValidateFtpResource(ftp_stream);

            if (resource == null)
            {
                return(-1);
            }

            try
            {
                return(DateTimeUtils.UtcToUnixTimeStamp(resource.Client.GetModifiedTime(remote_file, FtpDate.Original).ToUniversalTime()));
            }
            catch (FtpCommandException ex)
            {
                PhpException.Throw(PhpError.Warning, ex.Message);
            }
            catch (ArgumentException ex)
            {
                PhpException.Throw(PhpError.Warning, Resources.Resources.file_not_exists, ex.ParamName);
            }

            return(-1);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
 /// </summary>
 /// <returns>Number of seconds since 1970.</returns>
 public static int time()
 {
     return(DateTimeUtils.UtcToUnixTimeStamp(System_DateTime.UtcNow));
 }
Ejemplo n.º 7
0
        internal static string FormatDate(string format, System_DateTime utc, TimeZoneInfo zone)
        {
            Debug.Assert(zone != null);

            if (format == null)
            {
                return(string.Empty);
            }

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

            // here we are creating output string
            StringBuilder result = new StringBuilder();
            bool          escape = false;

            foreach (char ch in format)
            {
                if (escape)
                {
                    result.Append(ch);
                    escape = false;
                    continue;
                }

                switch (ch)
                {
                case 'a':
                    // Lowercase Ante meridiem and Post meridiem - am or pm
                    result.Append(local.ToString("tt", DateTimeFormatInfo.InvariantInfo).ToLowerInvariant());
                    break;

                case 'A':
                    // Uppercase Ante meridiem and Post meridiem - AM or PM
                    result.Append(local.ToString("tt", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'B':
                    // Swatch Beat (Internet Time) - 000 through 999
                    result.AppendFormat("{0:000}", GetSwatchBeat(utc));
                    break;

                case 'c':
                {
                    // ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
                    result.Append(local.ToString("yyyy-MM-dd'T'HH:mm:ss", DateTimeFormatInfo.InvariantInfo));

                    TimeSpan offset = zone.GetUtcOffset(local);
                    result.AppendFormat("{0}{1:00}:{2:00}", (offset.Ticks < 0) ? "" /*offset.Hours already < 0*/ : "+", offset.Hours, offset.Minutes);
                    break;
                }

                case 'd':
                    // Day of the month, 2 digits with leading zeros - 01 to 31
                    result.Append(local.ToString("dd", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'e':
                    // Timezone identifier (added in PHP 5.1.0)
                    //result.Append(zone.Id);
                    //break;
                    throw new NotImplementedException();

                case 'D':
                    // A textual representation of a day, three letters - Mon through Sun
                    result.Append(local.ToString("ddd", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'F':
                    // A full textual representation of a month, such as January or March - January through December
                    result.Append(local.ToString("MMMM", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'g':
                    // 12-hour format of an hour without leading zeros - 1 through 12
                    result.Append(local.ToString("%h", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'G':
                    // 24-hour format of an hour without leading zeros - 0 through 23
                    result.Append(local.ToString("%H", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'h':
                    // 12-hour format of an hour with leading zeros - 01 through 12
                    result.Append(local.ToString("hh", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'H':
                    // 24-hour format of an hour with leading zeros - 00 through 23
                    result.Append(local.ToString("HH", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'i':
                    // Minutes with leading zeros - 00 to 59
                    result.Append(local.ToString("mm", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'I':
                    // Whether or not the date is in daylights savings time - 1 if Daylight Savings Time, 0 otherwise.
                    result.Append(zone.IsDaylightSavingTime(local) ? "1" : "0");
                    break;

                case 'j':
                    // Day of the month without leading zeros - 1 to 31
                    result.Append(local.ToString("%d", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'l':
                    // A full textual representation of the day of the week - Sunday through Saturday
                    result.Append(local.ToString("dddd", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'L':
                    // Whether it's a leap year - 1 if it is a leap year, 0 otherwise.
                    result.Append(System_DateTime.IsLeapYear(local.Year) ? "1" : "0");
                    break;

                case 'm':
                    // Numeric representation of a month, with leading zeros - 01 through 12
                    result.Append(local.ToString("MM", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'M':
                    // A short textual representation of a month, three letters - Jan through Dec
                    result.Append(local.ToString("MMM", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'n':
                    // Numeric representation of a month, without leading zeros - 1 through 12
                    result.Append(local.ToString("%M", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'N':
                    // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
                    int day_of_week = (int)local.DayOfWeek;
                    result.Append(day_of_week == 0 ? 7 : day_of_week);
                    break;

                case 'o':
                {
                    // ISO-8601 year number. This has the same value as Y, except that if the ISO
                    // week number (W) belongs to the previous or next year, that year is used instead.
                    // (added in PHP 5.1.0)
                    int week, year;
                    GetIsoWeekAndYear(local, out week, out year);
                    result.Append(year);
                    break;
                }

                case 'O':
                {
                    // Difference to Greenwich time (GMT) in hours Example: +0200
                    TimeSpan offset = zone.GetUtcOffset(local);
                    string   sign   = (offset.Ticks < 0) ? ((offset.Hours < 0) ? string.Empty : "-") : "+";
                    result.AppendFormat("{0}{1:00}{2:00}", sign, offset.Hours, offset.Minutes);
                    break;
                }

                case 'P':
                {
                    // same as 'O' but with the extra colon between hours and minutes
                    // Difference to Greenwich time (GMT) in hours Example: +02:00
                    TimeSpan offset = zone.GetUtcOffset(local);
                    string   sign   = (offset.Ticks < 0) ? ((offset.Hours < 0) ? string.Empty : "-") : "+";
                    result.AppendFormat("{0}{1:00}:{2:00}", sign, offset.Hours, offset.Minutes);
                    break;
                }

                case 'r':
                    // RFC 822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
                    result.Append(local.ToString("ddd, dd MMM yyyy H:mm:ss ", DateTimeFormatInfo.InvariantInfo));
                    goto case 'O';

                case 's':
                    // Seconds, with leading zeros - 00 through 59
                    result.Append(local.ToString("ss", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'S':
                    result.Append(GetDayNumberSuffix(local.Day));
                    break;

                case 't':
                    // Number of days in the given month 28 through 31
                    result.Append(System_DateTime.DaysInMonth(local.Year, local.Month));
                    break;

                case 'T':
                    // Timezone setting of this machine Examples: EST, MDT ...
                    result.Append(zone.IsDaylightSavingTime(local) ? zone.DaylightName : zone.StandardName);
                    break;

                case 'U':
                    // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
                    result.Append(DateTimeUtils.UtcToUnixTimeStamp(utc));
                    break;

                case 'u':
                    // Microseconds (added in PHP 5.2.2)
                    result.Append((utc.Millisecond / 1000).ToString("D6"));
                    break;

                case 'w':
                    // Numeric representation of the day of the week - 0 (for Sunday) through 6 (for Saturday)
                    result.Append((int)local.DayOfWeek);
                    break;

                case 'W':
                {
                    // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year)
                    int week, year;
                    GetIsoWeekAndYear(local, out week, out year);
                    result.Append(week);
                    break;
                }

                case 'y':
                    // A two digit representation of a year Examples: 99 or 03
                    result.Append(local.ToString("yy", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'Y':
                    // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
                    result.Append(local.ToString("yyyy", DateTimeFormatInfo.InvariantInfo));
                    break;

                case 'z':
                    // The day of the year starting from 0
                    result.Append(local.DayOfYear - 1);
                    break;

                case 'Z':
                    // TimeZone offset in seconds:
                    result.Append((int)zone.GetUtcOffset(local).TotalSeconds);
                    break;

                case '\\':
                    // Escape char. Output next character directly to the result.
                    escape = true;
                    break;

                default:
                    // unrecognized character, print it as-is.
                    result.Append(ch);
                    break;
                }
            }

            if (escape)
            {
                result.Append('\\');
            }

            return(result.ToString());
        }
Ejemplo n.º 8
0
        private static int GetDatePart(char format, System_DateTime utc, TimeZoneInfo /*!*/ zone)
        {
            var local = TimeZoneInfo.ConvertTime(utc, zone);// zone.ToLocalTime(utc);

            switch (format)
            {
            case 'B':
                // Swatch Beat (Internet Time) - 000 through 999
                return(GetSwatchBeat(utc));

            case 'd':
                // Day of the month
                return(local.Day);

            case 'g':
            case 'h':
                // 12-hour format:
                return((local.Hour == 12) ? 12 : local.Hour % 12);

            case 'G':
            case 'H':
                // 24-hour format:
                return(local.Hour);

            case 'i':
                return(local.Minute);

            case 'I':
                return(zone.IsDaylightSavingTime(local) ? 1 : 0);

            case 'j':
                goto case 'd';

            case 'L':
                return(System_DateTime.IsLeapYear(local.Year) ? 1 : 0);

            case 'm':
                return(local.Month);

            case 'n':
                goto case 'm';

            case 's':
                return(local.Second);

            case 't':
                return(System_DateTime.DaysInMonth(local.Year, local.Month));

            case 'U':
                return(DateTimeUtils.UtcToUnixTimeStamp(utc));

            case 'w':
                // day of the week - 0 (for Sunday) through 6 (for Saturday)
                return((int)local.DayOfWeek);

            case 'W':
            {
                // ISO-8601 week number of year, weeks starting on Monday:
                int week, year;
                GetIsoWeekAndYear(local, out week, out year);
                return(week);
            }

            case 'y':
                return(local.Year % 100);

            case 'Y':
                return(local.Year);

            case 'z':
                return(local.DayOfYear - 1);

            case 'Z':
                return((int)zone.GetUtcOffset(local).TotalSeconds);

            default:
                //PhpException.InvalidArgument("format");
                //return 0;
                throw new ArgumentException();
            }
        }
Ejemplo n.º 9
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));
        }