4

I am programming the back end of my website (ASP.Net C# framework 4.0). I am listening for results using my PayPal IPN listener, which will update my back-end database.

When a payment is made I would like to record the payment date. I am having trouble parsing the payment_date parameter that is given to me from the IPN message. It looks like this:

args["payment_date"]: 10:23:05 Dec 02, 2013 PST

The following try-parse is always returning a false:

DateTime paymentDate = DateTime.UtcNow;
DateTime.TryParse(args["payment_date"], out paymentDate);

PST stands for Pacific Standard Time right? I read that some US states that use PST will switch to PDT (Pacific Daylight Time) at some point in the year.

In my database I would like to store all DateTimes as UTC. Is there a way that I can ask PayPal to give me the payment date in UTC? Or do I have to write my own parser? If so then is there anything else I should watch out for apart from the switch from PST to PDT?

Perhaps you might think I should just use my own server time to record the payment date, but I just thought it would be better to use the date given by PayPal themselves in the actual IPN message.

Ben
  • 3,241
  • 4
  • 35
  • 49
  • 1
    Possible duplicate: http://stackoverflow.com/questions/13445510/paypal-datetime-payment-date-parsing-issue – Patrick Quirk Dec 02 '13 at 21:09
  • Nice find @PatrickQuirk - not sure why this did not appear when I looked for it. – Ben Dec 02 '13 at 22:36
  • Seems a shame that I can't just ask PayPal to give me the payment date in UTC. Also according to [this answer](http://stackoverflow.com/a/19004676/340045) PayPal use several date-time formats. How inconsiderate of them. – Ben Dec 04 '13 at 11:05

2 Answers2

1

Just specify a custom DateTime format that matches what PayPal gives you, and pass that to the TryParse or TryParseExact method:

DateTime paymentDate = DateTime.UtcNow;
string format = "hh:mm:ss MMM dd, yyyy";
DateTime.TryParseExact(args["payment_date"], out paymentDate, format, CultureInfo.InvariantCulture);

See: http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

Parsing the timezone shorthand is not supported in .NET, so per Patrick's comment on my answer, check here for a workaround: Parse DateTime with time zone of form PST/CEST/UTC/etc

Community
  • 1
  • 1
Haney
  • 32,775
  • 8
  • 59
  • 68
  • 2
    Parsing the abbreviations is not supported in .NET, so the "K" formatter won't match. See http://stackoverflow.com/questions/241789/parse-datetime-with-timezone-of-form-pst-cest-utc-etc for an ugly workaround... – Patrick Quirk Dec 02 '13 at 21:03
  • So since .NET doesn't support timezone shorthand, and according to Patrick's link, how about using `.Replace("PST", "-07:00").Replace("PDT", "-08:00")` (did I get that the right way round?) and adding zzz to the format? – Ben Dec 02 '13 at 22:49
  • Just checked and PST is -08:00 and PDT is -07:00. – Ben Dec 02 '13 at 23:20
  • You could, but it's overly expensive to do repeated replacements... Each one costs you an O(n) traversal of the string. – Haney Dec 03 '13 at 00:28
  • 1
    Thanks David, at the moment I don't think my online shop will have to worry about the computational expense of a couple of Replace methods. The date-time string is fairly short anyway so shouldn't impact too much. If it starts to become a bottleneck then I will be earning so much that I will be able to afford to pay someone else to do my website for me ;-) – Ben Dec 04 '13 at 11:00
  • Exactly. Good mentality. Don't solve problems you don't yet have. – Haney Dec 04 '13 at 17:57
1

You can also check here:

How do I convert Paypal's HH:MM:SS DD Mmm(.) YYYY PST/PDT to a C# UTC DateTime?

But your format should be in HH:MM:SS DD Mmm(.) YYYY PST/PDT first.

Community
  • 1
  • 1
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17