UnixTimeStampToUtc() static private method

Converts UNIX timestamp (number of seconds from 1.1.1970) to System_DateTime.
static private UnixTimeStampToUtc ( int timestamp ) : System.DateTime
timestamp int UNIX timestamp
return System.DateTime
Ejemplo n.º 1
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)));
        }
Ejemplo 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();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets access and modification time of file.
        /// </summary>
        /// <remarks>
        /// Attempts to set the access and modification time of the file named by
        /// path to the value given by time. If the option time is not given,
        /// uses the present time. If the third option atime is present, the access
        /// time of the given path is set to the value of atime. Note that
        /// the access time is always modified, regardless of the number of parameters.
        /// If the file does not exist, it is created.
        /// </remarks>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">The file to touch.</param>
        /// <param name="mtime">The new modification time.</param>
        /// <param name="atime">The desired access time.</param>
        /// <returns><c>true</c> on success, <c>false</c> on failure.</returns>
        public static bool touch(Context ctx, string path, int mtime = 0, int atime = 0)
        {
            // Create the file if it does not already exist (performs all checks).
            //PhpStream file = (PhpStream)Open(path, "ab");
            //if (file == null) return false;
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileMayExist, CheckAccessOptions.Quiet))
            {
                return(false);
            }

            if (!file_exists(ctx, path))
            {
                // Open and close => create new.
                wrapper.Open(ctx, ref path, "wb", StreamOpenOptions.Empty, StreamContext.Default)
                ?.Dispose();
            }

            var access_time       = (atime > 0) ? DateTimeUtils.UnixTimeStampToUtc(atime) : System.DateTime.UtcNow;
            var modification_time = (mtime > 0) ? DateTimeUtils.UnixTimeStampToUtc(mtime) : System.DateTime.UtcNow;

            //access_time -= DateTimeUtils.GetDaylightTimeDifference(access_time, System.DateTime.UtcNow);
            //modification_time -= DateTimeUtils.GetDaylightTimeDifference(modification_time, System.DateTime.UtcNow);

            try
            {
                File.SetLastWriteTimeUtc(path, modification_time);
                File.SetLastAccessTimeUtc(path, access_time);

                // Clear the cached stat values
                clearstatcache();
                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_file_access_denied, FileSystemUtils.StripPassword(path));
            }
            catch (System.Exception e)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_error, FileSystemUtils.StripPassword(path), e.Message);
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Internal version common for <see cref="setcookie"/> and <see cref="setrawcookie"/>.
        /// </summary>
        internal static bool SetCookieInternal(Context ctx, string name, string value, int expire, string path, string domain, bool secure, bool httponly, bool raw)
        {
            var httpctx = ctx.HttpPhpContext;

            if (httpctx == null)
            {
                return(false);
            }

            DateTimeOffset?expires;

            if (expire > 0)
            {
                expires = new DateTimeOffset(DateTimeUtils.UnixTimeStampToUtc(expire).ToLocalTime());
            }
            else
            {
                expires = null;
            }

            httpctx.AddCookie(name, raw ? value : WebUtility.UrlEncode(value), expires, path ?? "/", domain, secure, httponly);

            return(true);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Parses a string containing an English date format into a UNIX timestamp relative to a specified time.
 /// </summary>
 /// <param name="time">String containing time definition.</param>
 /// <param name="start">Timestamp (seconds from 1970) to which is the new timestamp counted.</param>
 /// <returns>Number of seconds since 1/1/1970 or -1 on failure.</returns>
 public static int strtotime(string time, int start)
 {
     return(StringToTime(time, DateTimeUtils.UnixTimeStampToUtc(start)));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Identical to the date() function except that the time returned is Greenwich Mean Time (GMT)
 /// </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 gmdate(string format, int timestamp)
 {
     return(FormatDate(format, DateTimeUtils.UnixTimeStampToUtc(timestamp), DateTimeUtils.UtcTimeZone));
 }
Ejemplo n.º 7
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));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Returns an associative array containing the date information of the timestamp.
 /// </summary>
 /// <param name="timestamp">Number of seconds since 1970.</param>
 /// <returns>Associative array with date information.</returns>
 public static PhpArray getdate(Context ctx, int timestamp)
 {
     return(GetDate(ctx, DateTimeUtils.UnixTimeStampToUtc(timestamp)));
 }
Ejemplo n.º 9
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)));
 }