You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 25, 2026. It is now read-only.
Feature: convert PhpDateTime into native System.Datetime object #18
Perhaps a cool feature would be to allow for Php datetime objects to be directly translated to native System.DateTime.
I don't need this myself but it should be easy enough to implement - i have this code-snippet that does the job halfway already which we can use - so just posting this as a nice to have backlog item ^^.
privatestaticDateTime?ParsePhpDateTimeStringSafe(stringdateString,stringtimezone,longtimezoneType){try{returnParsePhpDateTimeString(dateString,timezone,timezoneType);}catch{// We're swallowing exceptions because we'll be handling null values instead.returnnull;}}privatestaticDateTimeParsePhpDateTimeString(stringdateString,stringtimezone,longtimezoneType){varlocalDateTimePattern=LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss.ffffff");varlocalDateTime=localDateTimePattern.Parse(dateString).GetValueOrThrow();// See: https://stackoverflow.com/a/17711005/4122889// Type 1; A UTC offset, such as in new DateTime("17 July 2013 -0300");// Type 2; A timezone abbreviation, such as in new DateTime("17 July 2013 GMT");// Type 3: A timezone identifier, such as in new DateTime("17 July 2013", new DateTimeZone("Europe/London"));switch(timezoneType){case1:varoffSetPattern=OffsetPattern.CreateWithInvariantCulture("+HH:mm");varoffset=offSetPattern.Parse(timezone).Value;varzonedDateTimeFromOffset=localDateTime.InZoneStrictly(DateTimeZone.ForOffset(offset));returnzonedDateTimeFromOffset.ToDateTimeUtc();case2:thrownewNotSupportedException("Not (Yet) support converting from timeZonetype 2 - but doable to add in!");case3:vardateTimeZone=DateTimeZoneProviders.Tzdb[timezone];varzonedDateTime=dateTimeZone.AtStrictly(localDateTime);vardateTimeUtc=zonedDateTime.ToDateTimeUtc();returndateTimeUtc;default:thrownewArgumentOutOfRangeException(nameof(timezoneType));}}
This does however use the NodaTime package which may not be a dependency we'd like. Optionally we could move this to a seperate package to give consumers more control over this.
that sounds like it'd be useful, but I'm afraid it's way beyond the scope of this library. I am leaving the issue open for now, because I might add a second package to this repository for that purpose. Not sure if I'd want to maintain that yet.
Activity
StringEpsilon commentedon Dec 11, 2021
Hi,
that sounds like it'd be useful, but I'm afraid it's way beyond the scope of this library. I am leaving the issue open for now, because I might add a second package to this repository for that purpose. Not sure if I'd want to maintain that yet.