internal static System_DateTime StrToTime(Context ctx, string timestr, System_DateTime time)
        {
            if (string.IsNullOrWhiteSpace(timestr) || (timestr = timestr.Trim()).EqualsOrdinalIgnoreCase("now"))
            {
                return(System_DateTime.UtcNow);
            }

            var result = DateInfo.Parse(ctx, timestr, time, out var error);

            if (error != null)
            {
                ctx.SetProperty <DateTimeErrors>(new DateTimeErrors {
                    Errors = new[] { error }
                });
                throw new Spl.Exception(error);
            }

            return(DateTimeUtils.UnixTimeStampToUtc(result));
        }
Пример #2
0
        public static DateTimeValue Parse(Context ctx, string timestr, TimeZoneInfo?timezone)
        {
            Debug.Assert(ctx != null);
            Debug.Assert(timestr != null);

            //
            ctx !.SetProperty(DateTimeErrors.Empty);

            timestr = timestr != null?timestr.Trim() : string.Empty;

            var localtz = timezone ?? PhpTimeZone.GetCurrentTimeZone(ctx);

            Debug.Assert(localtz != null);

            System_DateTime localdate;

            //
            if (timestr.Length == 0 || timestr.EqualsOrdinalIgnoreCase(DateTimeValue.Now))
            {
                // most common case
                localdate = TimeZoneInfo.ConvertTimeFromUtc(System_DateTime.UtcNow, localtz);
            }
            else
            {
                var result = DateInfo.Parse(timestr, System_DateTime.UtcNow, ref localtz, out var error);
                if (error != null)
                {
                    ctx.SetProperty <DateTimeErrors>(new DateTimeErrors {
                        Errors = new[] { error }
                    });
                    throw new Spl.Exception(error);
                }

                localdate = result;
            }

            //
            return(new DateTimeValue(localdate, localtz));
        }