Exemple #1
0
 public virtual DateTimeImmutable setTimezone(DateTimeZone timezone) => throw new NotImplementedException();
Exemple #2
0
        public static DateTime createFromFormat(Context ctx, string format, string time, DateTimeZone timezone = null)
        {
            // arguments
            var tz = (timezone != null) ? timezone._timezone : PhpTimeZone.GetCurrentTimeZone(ctx);

            if (format == null)
            {
                //PhpException.InvalidArgument("format");
                //return false;
                throw new ArgumentNullException();
            }

            if (time == null)
            {
                //PhpException.InvalidArgument("time");
                //return false;
                throw new ArgumentNullException();
            }

            if (format == "U")
            {
                long seconds = 0;
                if (Int64.TryParse(time, out seconds))
                {
                    DateTimeOffset offset = DateTimeOffset.FromUnixTimeSeconds(seconds);
                    return(new DateTime(ctx)
                    {
                        Time = offset.UtcDateTime,
                        // TODO should be set as UTC ?
                        TimeZone = TimeZoneInfo.Utc
                    });
                }
                else
                {
                    throw new ArgumentException("The time argument could not be parsed as an integer.");
                }
            }

            var builder = new StringBuilder();

            //used to replace 24 hour H character with 12 hour format h if needed
            bool twelveHour = false;

            //flag to reset the datetime to base Unix DateTime (1.1.1970)
            //bool resetUnixDateTime = false;

            // create DateTime from format+time
            for (int i = 0; i < format.Length; i++)
            {
                var c = format[i];

                switch (c)
                {
                case 'd':
                case 'j':
                    builder.Append("dd");
                    break;

                case 'D':
                case 'l':
                    builder.Append("ddd");
                    break;

                case 'F':
                case 'M':
                    builder.Append("MMM");
                    break;

                case 'm':
                case 'n':
                    builder.Append("MM");
                    break;

                case 'Y':
                    builder.Append("yyyy");
                    break;

                case 'a':
                case 'A':
                    builder.Append("tt");
                    twelveHour = true;
                    break;

                case 'g':
                case 'G':
                case 'h':
                case 'H':
                    builder.Append("HH");
                    break;

                case 'i':
                    builder.Append("mm");
                    break;

                case 's':
                    builder.Append("ss");
                    break;

                case '!':
                //resetUnixDateTime = true;
                //throw new NotImplementedException("Unix time resetting is not implemented.");
                case '?':
                case '/':
                case '*':
                case '+':
                case '#':
                case 'U':
                case 'S':
                case 'z':
                case 'e':
                case 'O':
                case 'P':
                case 'T':
                    throw new NotImplementedException($"Format modifier '{c}' at position {i}.");

                default:
                    builder.Append(c);
                    break;
                }
            }

            if (twelveHour)
            {
                builder.Replace('H', 'h');
            }

            var csStyleFormat = builder.ToString();

            var success = System_DateTime.TryParseExact(time, csStyleFormat, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out var dateTime);

            if (success)
            {
                return(new DateTime(ctx)
                {
                    Time = dateTime
                });
            }
            else
            {
                throw new NotImplementedException("Given datetime format string is not supported or it is invalid.");
            }
        }
Exemple #3
0
 public static DateTimeImmutable createFromFormat(string format, string time, DateTimeZone timezone = null) => throw new NotImplementedException();
Exemple #4
0
 // public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
 public DateTime(Context ctx, string time = null, DateTimeZone timezone = null)
     : this(ctx)
 {
     ctx.SetProperty(DateTimeErrors.Empty);
     __construct(time, timezone);
 }