How to work with reverse formula in c#. convert Controller datetime format to normal date time using formula?

Viewed 82

I am working with Controller device, which maintain user attendance logs. Controller store logs time details like below:

Controller Time Actual Time
690581614 2021-06-26 20:13:34.000
690581620 2021-06-26 20:13:40.000
690590751 2021-06-26 22:45:51.000

It uses internal formula to convert normal date to number date like below

((Year - 2000) * 12 * 31 + (Month - 1) * 31 + (Day - 1)) * (24 * 60 * 60) + Hour * 60 * 60 + Minute * 60 + Second;

If we consider our date time is "2021-06-26 22:45:51.000" then it look like

((2021 - 2000) * 12 * 31 + (06 - 1) * 31 + (26 - 1)) * (24 * 60 * 60) + 22 * 60 * 60 + 45 * 60 + 51;

once we get result : 690590751

Means 690590751 (Controoler DT) == "2021-06-26 22:45:51.000".

---I am getting below values which i want to convert to actual date..Please help me to get proper conversion formula for converting from Controller DT to Normal Date for C#.

690590751 => Normal Date Formula?

1 Answers

You can calculate a DateTime from the number like this:

public static DateTime ControllerValueToDateTime(int value)
{
    int seconds = value % 60;
    value /= 60;
    int minutes = value % 60;
    value /= 60;
    int hours = value % 24;
    value /= 24;
    int day = (value % 31) + 1;
    value /= 31;
    int month = (value % 12) + 1;
    value /= 12;
    int year = 2000 + value;
    return new DateTime(year, month, day, hours, minutes, seconds, DateTimeKind.Unspecified);
}

% is the remainder operator, so 63 % 60 would return 3, as would 123 % 60, etc. So we can slowly remove individual parts from the number to get the component values, and then build a DateTime instance using them.

Testing this, we get the following results:

Controller Time Actual Time
690581614 2021-06-26 20:13:34.000
690581620 2021-06-26 20:13:40.000
690590751 2021-06-26 22:45:51.000

Try it online

Related