0

I have following situation. I have C++ code

1) SystemTimeToFileTime(&st, &m_Time)

Before above call st is assembled by parsing value from string and assigning to its members manually (please see paragraph about that below) - but only year, month, day, hour, minutes and seconds are assigned to it.

m_Time is instance variable of type FILETIME.

This method gets called in a function called ToDate (Here is link to that function for those interested: http://codepad.org/POoiKHMw, I have documented every line of it). Basically in the end of that method SystemTimeToFileTime gets called with assembled st structure being passed to it, I thought I can replicate that manual parsing part in C# easily. That is why I asked only about one function - SystemTimeToFileTime.

Afterwards there is a function like that:

2) dppDatetime_t RsDateTime::GetAsPrepaidTime() const
{
    dppDatetime_t dt;// Just a custom structure, this is no problem to create in C#
    SYSTEMTIME st = GetLocalTimeAsSystem();
    dt.Year     = st.wYear;
    dt.Month    = st.wMonth;
    dt.Day      = st.wDay;
    dt.Hour     = st.wHour;
    dt.Minute   = st.wMinute;
    dt.Second   = st.wSecond;
    return dt;
}

GetLocalTimeAsSystem defined as:

3) SYSTEMTIME RsDateTime::GetLocalTimeAsSystem() const
{
    SYSTEMTIME st;

    ::FileTimeToSystemTime(&m_Time, &st);
    return st;
}

I basically need to replicate functionality I mentioned in above: 1), 2), 3) points in C#. I would really appreciate some help on how to go with this? How to achieve same result in C#?

User first makes call to 1), then 2) (but 2) calls 3) internally).

Can't I implement what is done above using only DateTime structure in C#?

I am not asking how to do the manual parsing done in ToDate function, I can do that myself too.


EDIT: After comments, here is sample of some input/output:

This is input one can receive in item 1), this string will be parsed and st structure assembled:

151010070707

Output, what gets written to dppDatetime_t:

year:2015
month:10
day:10
hour:7
min:7
sec:7

PS These are the format strings I have seen in C++ code the ToDate function is being called with:

  //YYMMDDhhmmss -  e.g. "151030123030"
  //DD.MM.YY
  //DD.MM.YYYY - 01.01.2005
  //DDMMYYYY
  • What's the purpose of this back & forth conversion? You have a `SYSTEMTIME` to begin with (that you initialize yourself). – Amit Oct 30 '15 at 08:57
  • @Amit: That is a good point, I don't know, but: The way C++ projects uses this now is it parses manually string like DD.MM.YYYY and stores its components in st - see bullet (1). Then at some point, it is calling item 2) –  Oct 30 '15 at 09:05
  • @Amit: C++ code has functions to manually parse strings like YYMMDDhhmmss, DD.MM.YY, DDMMYYYY - to `st` variable. Then at some point it calls item 2). (I don't mind implementing that manual parsing code, to avoid possible differences). Like I said I don't want to lose information also, that is why I am looking for advice how to go with this –  Oct 30 '15 at 09:08
  • Why should you care about a C++ project? do what you need. `FileTimeToSystemTime` & `SystemTimeToFileTime` are "*inverse functions*": ***x == F(G(x))***. – Amit Oct 30 '15 at 09:08
  • @Amit: Like I said I am afraid to lose some information. But the C++ parsing supports parsing only strings which contains year, month, day, hour, second and minute. I thought I could lose information because it is storing this structure as FILETIME. Do you see? And then reading back from FILETIME? –  Oct 30 '15 at 09:09
  • Can you state what inputs and outputs you want to use? Avoid any mention of current implementation; just state clearly what inputs you have to handle and what the outputs for those inputs should be. – Matthew Watson Oct 30 '15 at 09:10
  • @Amit:Please see my updated question –  Oct 30 '15 at 09:26
  • @MatthewWatson: Please find updated question –  Oct 30 '15 at 09:26
  • It's not at all clear how a string `"151030123030"` would be parsed to 2013-10-10 07:07:07 – Matthew Watson Oct 30 '15 at 09:29
  • @MatthewWatson: That was typo, please see my updated question. That string is passed in function ToDate mentioned in bullet 1) –  Oct 30 '15 at 09:30
  • Even after the edit, I can't see how `"151030123030"` would end up with the hour, minute and seconds all `07`. Are there still errors in the spec? – Matthew Watson Oct 30 '15 at 09:33
  • @MatthewWatson: Sorry again typo –  Oct 30 '15 at 09:43

1 Answers1

0

I think there are errors in your specification.

However, if you want to parse a string like "151030123030" into a DateTime where the string is in the format "yyMMddHHmmss", you can do it like so:

string input = "151030123030";

var date = DateTime.ParseExact(input, "yyMMddHHmmss", CultureInfo.InvariantCulture);

Console.WriteLine(date); // Prints "30/10/2015 12:30:30"
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Since there are various formats possible to be passed to the ToDate function - please check link in bullet one, I don't mind to implement the manual parsing done there by hand also in C#. Assuming I did that, you think there are no more issues? Can I just use DateTime object instead in 1) and 2) cases where I need? –  Oct 30 '15 at 09:44
  • Or is there a necessity for me to use FILETIME structures and SYSTEMTIME structures or similar in C#? (Can I lose some information?) –  Oct 30 '15 at 09:45
  • @Quser If you are only wanting to parse string, you can just use the `DateTime.ParseExact()` method, giving it the appropriate C# version of the parse string. [See here for details](https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx) – Matthew Watson Oct 30 '15 at 09:49
  • I Want to achieve steps 1) and 2) (without manual parsing as in ToDate function - I will do that myself). And I don't want to lose information in this process. String parsing is one part of it as I understand, isn't it? –  Oct 30 '15 at 10:06
  • @Quser Your sample input (a string) and output (the date/time components) can be handled using `DateTime.ParseExact()` without losing any data as long as the times don't include anything more accurate than milliseconds. I don't understand what else you need. – Matthew Watson Oct 30 '15 at 10:15
  • I thought because C++ side uses FILETIME and SYSTIME they are achieving something I can miss on my C# side, do you follow? –  Oct 30 '15 at 10:17
  • Also doc says "If format is a custom format pattern that does not include date or time separators (such as "yyyyMMddHHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H"." - to avoid such things I don't mind copying the manual parsing done in C++ also to C#, just I am unsure it using FILETIME and SYSTIME structures in C++ is doing something I can miss on C# side. Do you understand now? –  Oct 30 '15 at 10:18
  • @Quser As far as I can see, using those structs isn't doing anything that DateTime parsing won't do. – Matthew Watson Oct 30 '15 at 10:33
  • here is how I implemented all that in C#: http://codepad.org/Rl4gc9NF, looks ok to you? ;) –  Oct 30 '15 at 11:08