Solution for my bad dates

Following up my previous post “I have bad dates“, here’s my solution. I may have to tweak this some at the end of the year but this seems to be working for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
public static DateTime wsGetDate(string badSabreDate, DateTime srcDT)
{
    // force the bad sabre date into a leap year
    var leapYear = "2000-" + badSabreDate;
 
    var year = srcDT.Year;
    DateTime dt = DateTime.Now.AddYears(-1);
    if (DateTime.TryParse(leapYear, out dt))
    {
	if (dt.Month < srcDT.Month)
	{
	    // parsed month is less than srcDT, increment my year
	    year++;
	}
 
	return new DateTime(year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    }
 
    var msg2 = string.Format("Can't parse {0} into a valid DateTime, {1}", badSabreDate, leapYear);
    throw new ApplicationException(msg2);
}
//

And here’s a piece of the car segment parsing code that calls the new method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
if (_Resp.TravelItinerary.ItineraryInfo.ReservationItems[i].Vehicle != null)
{
    #region CAR
    var car = _Resp.TravelItinerary.ItineraryInfo.ReservationItems[i].Vehicle;
    Log.Debug("Car --------------------------------------------------");
    string confID = "";
    if (car.ConfirmationNumber != null) confID = SabreWS.wsString(car.ConfirmationNumber);
 
    int RPHv = SabreWS.wsInt(car.SegmentNumber);
    string statusv = SabreWS.wsString(car.Status);
    string ID = "";
    if (car.POS != null) ID = SabreWS.wsString(car.POS.Source.RequestorID);
 
    Log.Debug(RPHv + statusv.PadLeft(4) + " - " + " requestorID: " + ID);
 
    string PU = SabreWS.wsString(car.VehRentalCore.LocationDetails.LocationCode);
    tmpForeignSts = AirportLookupCache.GetForeignStatus(PU);
    ForeignSts = tmpForeignSts > ForeignSts ? tmpForeignSts : ForeignSts;
 
    DateTime PUDT = wsGetDate(car.VehRentalCore.PickUpDateTime, DateTime.Now);
    Log.Debug("Pickup1: " + PU.PadRight(5) + PUDT.ToString());
 
    string RT = "";
    if (car.VehRentalCore.DropOffLocationDetails != null) RT = SabreWS.wsString(car.VehRentalCore.DropOffLocationDetails.LocationCode);
    if (RT.Length == 0) RT = PU;
    tmpForeignSts = AirportLookupCache.GetForeignStatus(RT);
    ForeignSts = tmpForeignSts > ForeignSts ? tmpForeignSts : ForeignSts;
 
    DateTime RTDT = PUDT;
    try
    {
	RTDT = wsGetDate(car.VehRentalCore.ReturnDateTime, PUDT);
    }
    catch (Exception a)
    {
	a.Message.ToString();
	Log.Error(car.VehRentalCore.ReturnDateTime + " --> error parsing this as the Return DateTime, forcing to PUDT");
    }
...
//