Skip to content
This repository was archived by the owner on Apr 25, 2026. It is now read-only.
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

Description

@sommmen

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 ^^.

private static DateTime? ParsePhpDateTimeStringSafe(string dateString, string timezone, long timezoneType)
{
    try
    {
        return ParsePhpDateTimeString(dateString, timezone, timezoneType);
    }
    catch
    {
        // We're swallowing exceptions because we'll be handling null values instead.
        return null;
    }
}

private static DateTime ParsePhpDateTimeString(string dateString, string timezone, long timezoneType)
{
    var localDateTimePattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss.ffffff");
    var localDateTime = 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)
    {
        case 1:
            var offSetPattern = OffsetPattern.CreateWithInvariantCulture("+HH:mm");
            var offset = offSetPattern.Parse(timezone).Value;
            var zonedDateTimeFromOffset = localDateTime.InZoneStrictly(DateTimeZone.ForOffset(offset));
            return zonedDateTimeFromOffset.ToDateTimeUtc();
        case 2:
            throw new NotSupportedException("Not (Yet) support converting from timeZonetype 2 - but doable to add in!");
        case 3:
            var dateTimeZone = DateTimeZoneProviders.Tzdb[timezone];
            var zonedDateTime = dateTimeZone.AtStrictly(localDateTime);
            var dateTimeUtc = zonedDateTime.ToDateTimeUtc();
            return dateTimeUtc;
        default:
            throw new ArgumentOutOfRangeException(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.

We may also be able to get the code working without NodaTime, perhaps with something like https://github.com/mattjohnsonpint/TimeZoneNames for the timezonedb values but i have not given this much thought.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions