public static DateTimeImmutable createFromFormat(Context ctx, string format, string time, DateTimeZone timezone = null) { // arguments var tz = (timezone != null) ? timezone._timezone : PhpTimeZone.GetCurrentTimeZone(ctx); var dateinfo = DateInfo.ParseFromFormat(format, time, out var errors); ctx.SetProperty <DateTimeErrors>(errors); if (errors != null && errors.HasErrors) { return(null); } return(new DateTimeImmutable(ctx, dateinfo.GetDateTime(ctx, System_DateTime.UtcNow), tz)); // TODO: dateinfo.TimeZones }
///// <summary> ///// Gets/sets/resets legacy configuration setting "date.timezone". ///// </summary> //internal static object GsrTimeZone(LibraryConfiguration/*!*/ local, LibraryConfiguration/*!*/ @default, object value, IniAction action) //{ // string result = (local.Date.TimeZone != null) ? local.Date.TimeZone.StandardName : null; // switch (action) // { // case IniAction.Set: // { // string name = Core.Convert.ObjectToString(value); // TimeZoneInfo zone = GetTimeZone(name); // if (zone == null) // { // PhpException.Throw(PhpError.Warning, LibResources.GetString("unknown_timezone", name)); // } // else // { // local.Date.TimeZone = zone; // } // break; // } // case IniAction.Restore: // local.Date.TimeZone = @default.Date.TimeZone; // break; // } // return result; //} /// <summary> /// Gets an instance of <see cref="TimeZone"/> corresponding to specified PHP name for time zone. /// </summary> /// <param name="phpName">PHP time zone name.</param> /// <returns>The time zone or a <B>null</B> reference.</returns> internal static TimeZoneInfo GetTimeZone(string /*!*/ phpName) { if (string.IsNullOrEmpty(phpName)) { return(null); } // simple binary search (not the Array.BinarySearch) var timezones = PhpTimeZone.s_lazyTimeZones.Value; int a = 0, b = timezones.Length - 1; while (a <= b) { int x = (a + b) >> 1; int comparison = StringComparer.OrdinalIgnoreCase.Compare(timezones[x].PhpName, phpName); if (comparison == 0) { return(timezones[x].Info); } if (comparison < 0) { a = x + 1; } else //if (comparison > 0) { b = x - 1; } } // try custom offset or a known abbreviation: var dt = new DateInfo(); var _ = 0; if (dt.SetTimeZone(phpName, ref _)) { // +00:00 // -00:00 // abbr return(dt.ResolveTimeZone()); } // return(null); }
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)); }
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)); }