To download NI software, including the products shown below, visit ni.com/downloads.
Overview
This example is used to convert a date and time string back into a timestamp.
Description
It's common to convert timestamps to strings when writing data to a file, and there are a number of native LabVIEW functions that allow you to do so (Get Date/Time String VI, Format Date/Time String VI). However, if you need to convert a date and time string back into a timestamp, there is no built-in function in LabVIEW to do so! There are good reasons to do this, such as reading data from an old logfile and plotting it onto a LabVIEW Graph. If you want time on the x-axis, you're currently a little out of luck since you can't use Date/Time strings to define the x-axis.
Attached is a solution which allows one to take a Date String and Time String (formatted per the outputs of the Get Date/Time String VI), and create a Timestamp. It makes a few extra conversions, and is meant to be modified to fit your application's specific use case.
Requirements
Steps to Implement or Execute Code
Additional Information or References
VI Snippet
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
You can use the following format string with "Format into String" and "Scan from String" to persist an 8601 UTC timestamp.
%^<%Y-%m-%dT%H:%M:%S%3uZ>T
To SanjayC Thanks very much. Could you possibly recompile to LV 8.2 and post that vi as well? Thanks
Also, to PhilipBrooks I tried your string format and it crashed LV twice (ver 8.2)... just FYI Thanks
Cal
Calvin: I put up a LV 8 and a LV 8.2 version. I did get a warning when saving back to LV 8 and LV 8.2, so let me know if the 8.2 version is broken in any way. Hopefully you find this function useful.
Phillip: does your solution allow you to go from a string to a timestamp? I'm not totally clear on the alternative you suggest.
Calvin: I tested this format string in both directions with LabVIEW 8.6 and 2010; I can't test 8.2, sorry...
BranchSupport-NIC: The format string example I gave works with both "Format into String" and "Scan from String".
There was a discussion on LAVA about persisting timestamp data in a file for the OpenG library and we hit upon the one I provided above.
Morning,
Thanks for the 8.2 version it seems to work just fine. No errors.
Philip: I haven't had a chance to investigate your solution in 8.2. I expect it will be some time before I can again but I will let you know here what I find.
Thanks again,
Cal
I typically have to do this with time strings that have an unknown format. There is a little gem from More Good Ideas that can handle arbritrary time stamps. It's downloadable from their site. Here's the page which describes it, just search it for parse arbritrary:
http://www.mooregoodideas.com/General_VIs/Time/index.html
The downside to this is that if you need to parse a lot of these strings it is not very fast. I've been thinking about creating such a function that would remember the format from the previous parse and use that to speed up the next.
Is there any way that I can get the sub second accuracy? The string has the accuracy there but when it converts it the function truncates the sub second data.
Mike
I'm not able to provide an example from where I'm at right now, but I think the only thing you should need to do is change the value of the digit(s) before the uZ. the u indicates the fractional part. so a string of
%^<%Y-%m-%dT%H:%M:%S%6uZ>T
should return a timestamp with microsecond accuracy as long as there are 6 digits after the decimal point.
I recall that there is a bug that does not handle the locale specific decimal separator (always reverts to period/dot) but this was fixed in LV2012.
This code doesn't work as expected on systems observing Daylight Saving Time, the Date/Time to Seconds primitive automatically adds an hour to the time in this case.
An approach like the one PhillipBrooks mentioned converts properly:
No one of the vi's works with Labview2015,
I also gave problems adding microseconds to TimeStamp.
The Cluster has no problem.
I have problem in the function "Date/Time to Seconds".
Thank you in advance
Best Regards
Carmine D'Errico
The format string function most certainly supports microseconds. If you use the snippet I originally posted, the DISPLAY precision of the timestamp control and indicator on the front panel were only set for 3 digits (milliseconds).
My second post suggested modifying the format string from %3u to %6u. That would properly set "resulting string" to 6 digits, but the timestamps would APPEAR to only have milliseconds. It is important not to confuse the VALUE of a variable with the FORMATTING of a value.
http://www.ni.com/tutorial/7900/en/