Converting the WhenChanged attribute (Generalized Time) in LDAP to DateTime in C #

I recently moved from using the S.DS namespace (which uses ADSI) to the S.SD.Protocol namespace. The only problem is that ADSI handled the conversion of Generalized Time to DateTime for me. Now I am returning the value "20070828085401.0Z" for the WhenChanged attribute. DateTime.Parse () does not convert this, so is there another way?

+8
source share
4 answers

The format you get is close to the date and time pattern ("o") and universal sortable date and time patterns ("u") for the standard date and time format, as described here .

One tricky solution would be to massage the string suitable for the template, and then use the standard format string ā€œoā€ or ā€œuā€ with ParseExact .

The best way would be to create a custom format string that matches the data you are already receiving. In the "How standard format strings work" section of the standard date and time format strings page, you will see all arbitrary format strings equivalent to "o" and "u". This should give you a good start.


EDIT : add code

string format = "yyyyMMddHHmmss.f'Z'"; string target = "20070828085401.0Z"; DateTime d = DateTime.ParseExact(target, format, CultureInfo.InvariantCulture); 

In the comments, lixonn notes that using the above format string, ParseExact will not be able to successfully 199412160532-0500 time string like 199412160532-0500 .

He will also not analyze a number of other valid strings, such as times, without the 'Zulu' trailing indicator ( 20070828085401.0 ); time without a fractional part ( 20070828085401Z ) and time representing minutes and seconds as a fractional hour ( 2007082808.90028Z ).

The format string can be made a little more forgiving by replacing the hard-coded 'Z' custom K specifier that will take 'Z', an offset of type -0500 and nothing. Whether this additional flexibility is useful depends on your application.

Please note that even with the K specifier the Lixonn string will not be successfully parsed, since it does not have a fractional part to match the .f component of the format string.

+6
source

You will need to use DateTime.ParseExact() with the exact format. You may have to play a little with the format, but it will be something like this.

 DateTime result; CultureInfo provider = CultureInfo.InvariantCulture; string format="yyyyMMddhhmmss.0Z"; result = DateTime.ParseExact(dateString, format, provider); 
+1
source

You can use datetime .strptime() .

  import datetime # Since 0Z denotes UTC, you can get rid of it and apply the timezone # later if you would like time_string = "20070828085401.0Z".split('.')[0] time_object = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%S") 

time_object should be output as datetime.datetime(2007, 8, 28, 8, 54, 1) . I believe this will be the least time zone and is equivalent to UTC time.

0
source
 // WIN32 FILETIME is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). // While the unix timestamp represents the seconds since January 1, 1970 (UTC). private static long Win32FileTimeToUnixTimestamp(long fileTime) { //return fileTime / 10000L - 11644473600000L; return DateTimeOffset.FromFileTime(fileTime).ToUnixTimeSeconds(); } // The GeneralizedTime follows ASN.1 format, something like: 20190903130100.0Z and 20190903160100.0+0300 private static long GeneralizedTimeToUnixTimestamp(string generalizedTime) { var formats = new string[] { "yyyyMMddHHmmss.fZ", "yyyyMMddHHmmss.fzzz" }; return DateTimeOffset.ParseExact(generalizedTime, formats, System.Globalization.CultureInfo.InvariantCulture).ToUnixTimeSeconds(); } 
0
source

Source: https://habr.com/ru/post/1346288/


All Articles