0

I have a batch script that starts with saving its start time for future use, which it does thusly:

set BATCHSTART=%TIME%

At the end of the batch, I'd like to re-convert it into a C# DateTime, and I need to do it in a locale-independent way. So, how can I do this?

I tried using the registry values described here (HKCU\Control Panel\International value sTimeFormat), unfortunately it doesn't work: On the Windows 10 Server machine I tried it on, the registry says "HH:mm:ss", but %TIME% returns "HH:mm:ss.ff" instead (I also tried on a French Windows 10 machine, got the same registry value but %TIME returned "HH:mm:ss,ff")

So, is there any way to fetch the real time format used by %TIME%? Or failing that, to parse it in a way that won't run afoul of locale problems?

Or even another way to store the time during the execution of a batch file?

Medinoc
  • 6,577
  • 20
  • 42
  • The DateTime struct in C# can output a configurable culture and format. You have not shown us exactly what it is outputting for you, or broke that down into its components for us to understand. If you want help with something specific, you need to provide us with that information. Also, as you are not new here, and must know how to use this site, what did you find throughout this sites' pages when you used the search facility at the top of the page. I cannot believe that you are the first person ever to want to a time string in a specific format from a batch-file. – Compo Jan 20 '23 at 15:41
  • @Compo I'm not trying to get it in a specific form ; I'm trying to get the form it's in. Also, all the code I've seen so far on SO seemed to assume the format was invariant, which I know from tests it's not (since the decimal separator affects it). – Medinoc Jan 20 '23 at 15:46
  • What do you mean by the form it is in? You've shown three `HH:mm:ss`, `HH:mm:ss.ff` and `HH:mm:ss,ff`. What is wrong with any of those? And/Or how do you want it to look? And I can asure you that there are hundreds of examples of how to retrieve date and time strings in an exact format, regardless of language/locale or user/PC configuration. I do not believe that you've tried hard enough, If you're suggesting that those do not exist. – Compo Jan 20 '23 at 15:59
  • *What is wrong with any of those*? What is wrong is, **I don't know how my software can know which one it is.** The registry setting that supposedly tells you... doesn't. – Medinoc Jan 20 '23 at 16:40
  • You can get it in whichever way you want it! _(including in exactly the same format as you intend it to be unnecessarily converted to later)_. But if you do not tell us what format you want, your software expects, or your final converted DateTime string is supposed to be, how do you expect us to assist you? – Compo Jan 20 '23 at 16:46
  • [some methods to get a date/time string independent of locale/user settings](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format?rq=1) – Stephan Jan 20 '23 at 17:32

1 Answers1

-1

From experimentation, for now I'll simply assume this is the format used:

    "HH:mm:ss" + CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator + "ff"
Medinoc
  • 6,577
  • 20
  • 42
  • 1
    Yes, you are right. Do you want to know a very simple method to get the value of `CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator` in a Batch file? Easy: `%time:~-3,1%` – Aacini Jan 23 '23 at 05:09