0

I'm rewriting some code from Java to C#. Unfortunately most of code was "hacked" so I'm just guessing how the code was written. Right now, I have to reproduce some inserts into data base..

I got a little confused in the time stamp I have to insert. For example in current database the record is: 10-AUG-10 05.02.57.347694000 PM

Values in current database

In my code I was able to get something similar: 22-Aug-16 11.52.20.9857574 AM

The idea of this code is to compare the timestamp created with the current one and perform some actions if the first one was created within the last 60 seconds.

First question:

How can I exactly reproduce 10-AUG-10 05.02.57.347694000, I see that all the records have "000" at the end, do I need an if/else for that or is there any function to keep the last part 9 digits long?

Second question:

If it was only numbers, I know I can retrieve the values from the database and then compare, but since the format is: 10-AUG-10 05.02.57.347694000, not really sure how to handle that part

Any suggestions?


SOLUTION:

After reading some comments I got a better understanding on what to do with this issue. I decided to keep things simple and just create DateTime variables and then just use a subtract method in order to see how many minutes of difference there are between those two.

Example:
DateTime guidTimestamp; //8/25/2016 10:02:47 AM
DateTime currentTimestamp = DateTime.Now;

public int calculateMinutes(DateTime dt1, DateTime dt2)   
    {  
            int min = 0;  
        try     
        {      
            min = (int)((dt1 - dt2).TotalSeconds);    
        }    
        catch (Exception ex)    
        {    
            Debug.WriteLine("Exception - Date Time error: " + ex.Message);    
        }    
        return min;    
    } 
Rolando F
  • 148
  • 3
  • 17
  • 3
    what is the type of that field? if it's not a date/datetime, then those aren't timestamps, they're STRINGS. and since your strings are not in a "good" format for direct comparisons, you're stuck with a large/hideous pile of string operations to reformat them into something that CAN be compared directly. – Marc B Aug 22 '16 at 16:47
  • 3
    In C# (or any language for that matter) when working with dates, you are far better off to use DateTime objects. When comparing them, take the difference of the Ticks, create a TimeSpan from it, then you can use the properties of the TimeSpan to determine the difference. In your case, you'd create the TimeSpan from the Ticks difference and look at the Hours and Minutes properties to see if they are both 0 (that would mean less than 60 seconds difference). – Kevin Aug 22 '16 at 17:03
  • 3
    http://stackoverflow.com/questions/341175/datetime-parse-and-making-it-work-with-a-specific-format see this on how you can convert your strings into DateTimes – Simon Farshid Aug 22 '16 at 17:10
  • 1
    @Kevin In C# `DateTime` has overloaded comparison operators so you can just compare them directly. It also overloads the subtraction operator so you can subtract two `DateTime`s and get a `TimeSpan` if you need a "less precise" comparison. No need to deal with ticks. – Kyle Aug 22 '16 at 17:56
  • @Kyle thanks for the suggestion. I needed to know if the first DateTime was created within the last minute, so I just subtracted directly and use the result to see if it is ">" or "<" than 60. Thanks! – Rolando F Aug 25 '16 at 19:49
  • @MarcB originally the code used datetime, but I see that for some reason this value gets complicated with some concatenation, which to be honest don't see any useful reason for that. I decided to just create them as DateTime and then just substract. Thanks for the clarification. – Rolando F Aug 25 '16 at 19:54
  • at least convert them into a most-significant-first formatting, them. pure numeric `yyyy-mm-dd hh:mm:ss`. even though that's a string. it'll sort exactly how you'd expect it to be sorted. `08/16/2016` and `2016-08-16` might be read the same by humans, but to a computer, `08/16` is LESS than the other one, because 0 comes before 2 – Marc B Aug 25 '16 at 19:55

1 Answers1

2

The data type of the field in DB is likely string. You shoudn't carry about such a big precision of the value as MSDN says.

The following line of code produces string with current date in exactly the same format as you mentioned:

DateTime.Now.ToString("dd-MMM-yy hh.mm.ss.ffffff tt", System.Globalization.CultureInfo.InvariantCulture).ToUpper() + "000"

The following line of code produces DateTime from your string with custom format:

DateTime.ParseExact("10-AUG-10 05.02.57.347694 AM", "dd-MMM-yy hh.mm.ss.ffffff tt", System.Globalization.CultureInfo.InvariantCulture)

As you see you need to get rid of those 3 zeros in the milliseconds part to make ParseExact process the string value. You can read about regular expressions or hardcode it if you wish.

Vladyslav
  • 786
  • 4
  • 19