peter32
Goto Top

Zeitangabe mit aktueller Angabe vergleichen

Hallo,

ich suche eine möglichst einfache Lösung, wie ich ein Zeitformat in einer Textdatei mit der aktuellen Zeit vergleichen kann, etwa so:

Wenn 04:10:19 länger her ist wie 30 sekunden von der aktuellen Zeit, dann gehe zu "zu lang", ansonsten "noch im Rahmen".

Das hier ist ein Auszug aus einer Beispieldatei:
142:16-08-31 04:10:19 Hiersteht ein Text unbekannter Länge

Und ich möchte eben diese 04:10:19 vergleichen mit der aktuellen Zeit und wenn die aktuelle Zeit später als 04:10:49 beträgt, dann soll zum Programmteilcode "zu lang" gehen und ansonsten zum code "noch im Rahmen".

Hoffe, ich hab mich verständlich ausgedrückt face-smile

LG
Peter

Content-Key: 314053

Url: https://administrator.de/contentid/314053

Printed on: April 23, 2024 at 15:04 o'clock

Mitglied: 129813
Solution 129813 Aug 31, 2016 updated at 13:48:28 (UTC)
Goto Top
powershell -Executionpolicy ByPass -Command "[datetime]::ParseExact('04:10:19','HH:mm:ss',[cultureinfo]::CurrentCulture).AddSeconds(30) -lt (get-date)"  
Outputs True if it's longer than 30 seconds ago, otherwise False.
Place your variable where the static time is placed.

Put a for loop around to capture the output, finished.

Regards
Member: Peter32
Peter32 Aug 31, 2016 at 15:15:59 (UTC)
Goto Top
Thanks so much so far!

Unfortunately I am still a beginner... face-confused

Could you please further explain your comment or give me an example code with the "for loop" and true (e.g. as an echo true) and false (e.g. as an echo false)?

Thanks in advance face-smile
Mitglied: 129813
Solution 129813 Aug 31, 2016, updated at Sep 05, 2016 at 15:47:35 (UTC)
Goto Top
set "yourtime=10:30:00"  
for /f "delims=" %%a in ('powershell -Executionpolicy ByPass -Command "write-host ([datetime]::ParseExact('%yourtime%','HH:mm:ss',[cultureinfo]::CurrentCulture).AddSeconds(30) -lt (get-date))"') do set result=%%a  
if /i "%result%" == "True" (  
   echo More than 30s
) else (
   echo Less than 30s
)
Member: Peter32
Peter32 Sep 01, 2016 at 11:05:54 (UTC)
Goto Top
Thanks! face-smile
Member: Peter32
Peter32 Sep 01, 2016 at 15:12:46 (UTC)
Goto Top
Hmm, but I didn't get where I can put my "example.txt" where the information is?
Mitglied: 129813
129813 Sep 01, 2016 updated at 15:35:55 (UTC)
Goto Top
Oh man, i don't know how your file looks like, so i cannot make an expample, so loop throuph your file, extract your time to a variable and set this variable to youtime.
I now showed you so much for loops face-sad

Turn on your brain man.
Member: Biber
Biber Sep 01, 2016 updated at 17:07:09 (UTC)
Goto Top
Moin Peter32,

ich sach ma' so:
Es lesen ja offensichtlich einige mit in diesem Thread und die meisten davon waren bestimmt genauso gespannt auf highloads Antwort wie ich.
Und vermutlich hätten fast alle nicht so höflich geantwortet.

Um konstruktiv zu bleiben:
- es ging aus deiner Eröffnungsfrage nicht hervor, dass du eigentlich noch gar nix in der Hand hast. Auch ich habe erwartet, es ginge nur noch um den Vergleich einer Zeitangabe mit der aktuellen Zeit.

- irgendwie hast du doch aber in deiner Datei die relevante Zeile gefunden. Der Ausgabe nach ja mit find/findstr. Dann müsstest du doch im Gegensatz zu uns wissen, wonach du gesucht hast.

- und eine bestimmte Spalte fieselst du als soundsovieltes Token in einer FOR / F-Anweisung raus.

Beispiele dazu findest du hier im Forum in Beiträgen, die fast so alt sind wie du, aber auch aktuellen Fragen von dir aus diesem Jahr.

Solltest du wissen, wenn du die Antworten liest und nicht nur Lösungen copy&pastest.

Grüße
Biber
Member: Peter32
Peter32 Sep 05, 2016 at 15:39:41 (UTC)
Goto Top
OK Ja sorry da habe ich wirklich nicht ganz mitgedacht! Danke für die Lösung!

Allerdings ein Problem:
Ich bekomme IMMER "Less than 30s", egal was ich eingebe als Variable "yourtime" und egal wie ich die 30s anpasse.. ich bekomme IMMER dasselbe Ergebnis?
Mitglied: 129813
Solution 129813 Sep 05, 2016 at 15:45:05 (UTC)
Goto Top
Here it works...
Member: Peter32
Peter32 Sep 06, 2016 updated at 13:51:32 (UTC)
Goto Top
That's so weird, today it works but yesterday it didn't face-confused

Anyway thank you very much!

I thought about one more thing. What if
set "yourtime=23:59:45"  

but my programm is checking it at 00:00:50 (so 50 seconds after new day began)?

or second example:

What if
set "yourtime=23:59:45"  

but my programm is checking it at 00:00:10 (so 10 seconds after new day began)?

Or even more interesting:

is it possible to add a date also?

So for example:
What if
set "yourtime=16:00:15"  
set "yourdate=16-09-05  

but my program but my programm is checking it next day on 6th of September at 16:00:20 (so 5 seconds after yourtime but one day later)?
And one beside thing: My format of the date is given in this format: yy-mm-dd, how can we just turn that around to intergrate it in this program?

my file looks like this:

16-09-06 14:30:46 Some text...

first there is the date and second there is the time
Mitglied: 129813
Solution 129813 Sep 06, 2016 updated at 14:26:15 (UTC)
Goto Top
ut my programm is checking it at 00:00:50 (so 50 seconds after new day began)?
Tha's normal because if you don't add a date to the time it uses the current day as the date part.

is it possible to add a date also?
Sure you can add it to the above parseexact method:
[datetime]::ParseExact('%yourdate% %yourtime%','yyyy-MM-dd HH:mm:ss',[cultureinfo]::CurrentCulture).AddSeconds(30)
Member: Peter32
Peter32 Sep 06, 2016 at 15:57:00 (UTC)
Goto Top
Thanks!

But I still don't know how to get the information from my sample.txt in the variables:

16-09-06 14:30:46 Some text...
How do I get my variables like this

yourtime=14:30:46
yourdate=2016-09-06

Thanks in advance!
Mitglied: 129813
Solution 129813 Sep 06, 2016 updated at 16:12:11 (UTC)
Goto Top
You have learned nothing ...
X) FOR-LOOP
echo off & setlocal ENABLEDELAYEDEXPANSION
for /f "usebackq tokens=1,2 delims= " %%i in ("C:\file.txt") do (  
   set "datetime=%%i %%j"  
   for /f "delims=" %%a in ('powershell -Executionpolicy ByPass -Command "write-host ([datetime]::ParseExact('!datetime!','yy-MM-dd HH:mm:ss',[cultureinfo]::CurrentCulture).AddSeconds(30) -lt (get-date))"') do set result=%%a  
   if /i "!result!" == "True" (  
      echo More than 30s
   ) else (
      echo Less than 30s
   )
)
Member: Peter32
Peter32 Sep 07, 2016 at 12:45:47 (UTC)
Goto Top
Thank you..