Get full server uptime lazarus / 21-General ---------------------------------------------------------------------- nugax · 2022-04-04 12:55 Hey Guys and Gals, I have a routine that reads an epoch time that is saved in a file. It then displays the uptime. The trouble I am having is it seems to only return hours of uptime, not full (days, hours, mins, secs). The saved file (pid file) is created upon server start and just contains a line of text that is the epoch time of the start of the server. Can anyone see where im going wrong? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //Get Server Uptime Stringprocedure GetServerUptime();var  sStartTime       : TDateTime;  F                : textfile;  sPidString       : string;  TServerStartTime : int64;  TCurrentTime     : int64;  TUptime          : int64; begin    if (FileExists(programinfo.cbbsInfo.sCBBSPid)) then  begin    Assign(F, programinfo.cbbsInfo.sCBBSPid);    Reset(F);    //Read in PID    Readln(f,sPidString);    //Read In Server Start Time    Readln(f, TServerStartTime);    Close(F);     //Calculate Uptime    TCurrentTime := DateTimeToUnix(Time);     TUptime  := (TCurrentTime - TServerStartTime);    //TUptime := (TServerStartTime - TCurrentTime);      writeln('Start Time: ' + TimeToStr(UnixToDateTime(TServerStartTime)));     writeln('Current Time: ' +  TimeToStr(UnixToDateTime(TCurrentTime)));     writeln('-[ CyberBBS Uptime: ' + TimeToStr(UnixToDateTime(TUptime)) + ' ]- [EPOCH TIME]');   //  writeln('UpTime: ' + TimeToStr(UnixToDateTime(TUptime)));   end  else  //PID File Does Not Exist  begin     writeln('No PID File or CyberBBS server is not running.');     halt;  end; ---------------------------------------------------------------------- nugax · 2022-04-04 12:56 I suspect i need to use a format date or something on the result, but not sure which one... ---------------------------------------------------------------------- MarkMLl · 2022-04-04 13:13 So what's in the file? MarkMLl ---------------------------------------------------------------------- winni · 2022-04-04 13:31 Hi! Use the data from /proc/uptime: /proc/uptime pseudo-file contains two numbers like that: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 1646896.16 3179285.64     The first number is how long the system has been up in seconds.     The second number is how much of that time the machine has spent idle in seconds. Winni ---------------------------------------------------------------------- Zvoni · 2022-04-04 13:41 Quote from: nugax on April 04, 2022, 02:56:35 pmI suspect i need to use a format date or something on the result, but not sure which one... I wouldn't There is PeriodBetween in DateUtils, which returns Years, Months and Days between to TDateTime-Variables It should be pretty easy to "expand" that function to include hours, minutes and seconds PeriodBetween uses DecodeDate from SysUtils. And SysUtils also has DecodeTime which returns hh,mm,ss,ms - part from a TDateTime or just subtract the two TDateTimes from each other and throw the result at DecodeTime to get the hours, minutes etc. Meaning: Convert your Unix-Epoch to TDateTime, and do the calculations i mentioned EDIT: If StartHour>CurrentHour subtract one Day, If StartMinute>CurrentMinute subtract one hour and so on.... ---------------------------------------------------------------------- MarkMLl · 2022-04-04 14:00 Quote from: winni on April 04, 2022, 03:31:38 pmUse the data from /proc/uptime: But that's the uptime of the host computer, not the uptime of the server/daemon associated with the specific PID which I presume is what OP's asking for. This isn't the first time I've come across a .pid which holds additional information, but my understanding is that there's absolutely no standard format. MarkMLl ---------------------------------------------------------------------- nugax · 2022-04-04 14:14 Mark, you are right. Its not the uptime of a server, its a file I generate with the epoch time of the program running. I am writing a util to calcuate the uptime (as it is a server program.) Here is the structure of the file I am reading in: First line is PID  (Process ID) of the server (in order to be able to kill it by a 3rd party program) Second Line is the epoch/unix time of when it started, written to a file. Example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cadams@skynet:~$ cd /opt/cbbs/tmpcadams@skynet:/opt/cbbs/tmp$ lscbbs_pid.tmp  chttpd  sessionscadams@skynet:/opt/cbbs/tmp$ cat cbbs_pid.tmp 30391-2209105425cadams@skynet:/opt/cbbs/tmp$ ---------------------------------------------------------------------- nugax · 2022-04-04 14:16 If i run the code i provided, I get uptime, just hours though. it doesnt take into account days, hours, mins, seconds (i believe) I think I need to format it.. just unclear what to use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cadams@skynet:/opt/cbbs/tmp$ cat cbbs_pid.tmp 30391-2209105425cadams@skynet:/opt/cbbs/tmp$ lscbbs_pid.tmp  chttpd  sessionscadams@skynet:/opt/cbbs/tmp$ cd ..cadams@skynet:/opt/cbbs$ lsbin  binkd  cbbs  cmanage  data  display  echomail  etc  logs  tmp  webrootcadams@skynet:/opt/cbbs$ ./cmanage -upStart Time: 15:36:15Current Time: 09:15:57-[ CyberBBS Uptime: 17:39:42 ]- [EPOCH TIME]cadams@skynet:/opt/cbbs$ ---------------------------------------------------------------------- nugax · 2022-04-04 14:19 This server has been running for like a week. Just want to be able to see that. I have a cron job that starts it in the background (forks it). ---------------------------------------------------------------------- Zvoni · 2022-04-04 14:27 untested. No sanity-check whatsoever Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Uses SysUtils, DateUtils;Var  StartTime,EndTime:TDateTime;  yy,mm,dd,hhh,mmm,sss,mss:Word; Procedure FullPeriodBetween(Const ANow,AThen:TDateTime;out y,m,d,hh,mm,ss,ms:word);Var  hh1,hh2,mm1,mm2,ss1,ss2,ms1,ms2:Word;Begin  PeriodBetween(ANow,AThen,y,m,d);  DecodeTime(ANow,hh1,mm1,ss1,ms1);  DecodeTime(AThen,hh2,mm2,ss2,ms2);  IF hh1>hh2 Then Dec(d);  DecodeTime(AThen-ANow,hh,mm,ss,ms);End; begin  StartTime:=Now;  //Here goes your Start-Epoch converted to TDateTime  EndTime:=EncodeDateTime(2022,04,06,15,10,20,123);   //Here goes your CurrentTime or just use "Now"  FullPeriodBetween(StartTime,EndTime,yy,mm,dd,hhh,mmm,sss,mss);  Writeln(Format('%d Years - %d months - %d days - %d hours - %d minutes - %d seconds - %d millisec.',[yy,mm,dd,hhh,mmm,sss,mss]));end.  EDIT: Just read again your first post. Have you tried "Now" instead of "Time" in your Line 25 in first Post? That's probably your "mistake" https://www.freepascal.org/docs-html/rtl/sysutils/time.html QuoteTime returns the current time in TDateTime format. The date part of the TDateTimeValue is set to zero. Your TCurrentTime only has Time-Information, no Day/Date-Information ---------------------------------------------------------------------- nugax · 2022-04-04 14:32 I havent. Does NOW return epoch time? Quote from: Zvoni on April 04, 2022, 04:27:12 pmuntested. No sanity- check whatsoever Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Uses SysUtils, DateUtils;Var  StartTime,EndTime:TDateTime;  yy,mm,dd,hhh,mmm,sss,mss:Word; Procedure FullPeriodBetween(Const ANow,AThen:TDateTime;out y,m,d,hh,mm,ss,ms:word);Var  hh1,hh2,mm1,mm2,ss1,ss2,ms1,ms2:Word;Begin  PeriodBetween(ANow,AThen,y,m,d);  DecodeTime(ANow,hh1,mm1,ss1,ms1);  DecodeTime(AThen,hh2,mm2,ss2,ms2);  IF hh1>hh2 Then Dec(d);  DecodeTime(AThen-ANow,hh,mm,ss,ms);End; begin  StartTime:=Now;  //Here goes your Start-Epoch converted to TDateTime  EndTime:=EncodeDateTime(2022,04,06,15,10,20,123);   //Here goes your CurrentTime or just use "Now"  FullPeriodBetween(StartTime,EndTime,yy,mm,dd,hhh,mmm,sss,mss);  Writeln(Format('%d Years - %d months - %d days - %d hours - %d minutes - %d seconds - %d millisec.',[yy,mm,dd,hhh,mmm,sss,mss]));end.  EDIT: Just read again your first post. Have you tried "Now" instead of "Time" in your Line 25 in first Post? ---------------------------------------------------------------------- Zvoni · 2022-04-04 14:35 Quote from: nugax on April 04, 2022, 04:32:21 pmI havent. Does NOW return epoch time? No. "Now" returns a "full" TDateTime: The Date AND Time And you should probably use "DateTimeToStr" instead of just "TimeToStr" ---------------------------------------------------------------------- MarkMLl · 2022-04-04 14:38 Quote from: Zvoni on April 04, 2022, 04:35:14 pmQuote from: nugax on April 04, 2022, 04:32:21 pmI havent. Does NOW return epoch time? No. "Now" returns a "full" TDateTime: The Date AND Time Also it's basically a floating point number. A cursory glance at OP's example data (a -ve number with around 10 digits) is inconclusive, since the unix date from earlier today is e.g. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TDateTime: 8 bytes, extended: 10 bytesLocal time: 2022-04-04 07:18:45.4UTC: 2022-04-04 06:18:45.4Unix secs: 1649056725POSIX secs: 1649053125.43801 (2022-04-04 06:18:45.4)  MarkMLl ---------------------------------------------------------------------- Zvoni · 2022-04-04 16:47 Mark, I‘ll never understand people using Unixepoch to express difference in Time. Unixepoch is seconds passed since Jan 1st 1970 (? Correct me if i‘m wrong). Meaning: he would have to calculate everything the hard way by hand. And now throw in Leapyear and you‘re „out-of-sync“. His best bet is probably the sample i‘ve provided above, if he really wants to express it in years, months, days and so on…. EDIT: if he wants days as the maximum expression, then he could use DaysBetween as the „inner“ function instead of PeriodBetween ---------------------------------------------------------------------- MarkMLl · 2022-04-04 17:30 Quote from: Zvoni on April 04, 2022, 06:47:53 pmMark, I‘ll never understand people using Unixepoch to express difference in Time. Unixepoch is seconds passed since Jan 1st 1970 (? Correct me if i‘m wrong). Meaning: he would have to calculate everything the hard way by hand. And now throw in Leapyear and you‘re „out-of-sync“. Well I'm certainly not (in this particular case): it gets complicated depending on whether your seconds are counted up monotonically from 1970 or calculated based on the current UTC date and time... and working from memory that's the distinction between the two numbers I showed from a program startup earlier in the day. https://en.wikipedia.org/wiki/Unix_time However I'm equally critical of timestamps represented as a floating point number (i.e. TDateTime etc.), since apart from anything else their precision gets worse the further you move from their epoch: more bits are needed to represent the integer part, so fewer are available for the fraction. My preferred solution- although I don't use this is practice- is IBM mainframe representation, which is defined as (a) being monotonic (b) having a particular bit (bit 10?) guaranteed to increment once per second (or perhaps once per 1.024... I forget) and (c) having lesser bits updated pro rata according to the capabilities of the hardware. After all, when the earliest machines came out a hardware millisecond counter cost an appreciable amount and system timeslices might be as long as half a second... So it's horses for courses. For something at the system level which doesn't need locale-specific manipulation and is unlikely to need maths done on it across a system restart, particularly if stored with a PID which is similarly low-level, "unix seconds" is probably no worse choice than the alternatives. BUT a unix second is a positive number, traditionally no larger than 2^31 and latterly no larger than 2^32, so I am concerned as to what the second number in something like Code: [Select]cadams@skynet:/opt/cbbs/tmp$ cat cbbs_pid.tmp 30391 -2209105425 actually represents. Obligatory reference: https://en.wikipedia.org/wiki/Calendrical_Calculations MarkMLl ---------------------------------------------------------------------- winni · 2022-04-04 17:37 Hi! The simple way: In the unit dateUtils there is this function Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DateTimeToUnix(  const AValue: TDateTime;  AInputIsUTC: Boolean = True ):Int64;  So you can convert a TDateTime date to Unix epoch seconds. Now you can compute or compare the seconds. After you have done that you get all values back to TDateTime: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function UnixToDateTime(const AValue: Int64; aReturnUTC : Boolean = true): TDateTime;  To get year, month, day, hour, second and ms you do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);  Winni ---------------------------------------------------------------------- Zvoni · 2022-04-05 06:23 Here untested code for "just" days with hours, minutes etc. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Uses SysUtils, DateUtils;Var  StartTime,EndTime:TDateTime;  dd,hhh,mmm,sss,mss:Word; Procedure GetUptime(Const ANow,AThen:TDateTime;out d,hh,mm,ss,ms:word);Var  tmp:TDateTime;   Begin  d:=DaysBetween(ANow,AThen);  If AThen Quotecadams@skynet:/opt/cbbs/tmp$ cat cbbs_pid.tmp 30391 -2209105425 cadams@skynet:/opt/cbbs/tmp$ ls cbbs_pid.tmp  chttpd  sessions QuoteSo I'm afraid that almost everybody here is barking up the wrong tree: OP has shown us what's in the file, it's /not/ a TDateTime, so all this wibbling about comparing it directly with Now() isn't getting anywhere. MarkMLl Yes, and as the OP has stated he has full control what's inside that file *shrug* ---------------------------------------------------------------------- MarkMLl · 2022-04-05 07:44 Quote from: Zvoni on April 05, 2022, 09:31:53 amYes, and as the OP has stated he has full control what's inside that file *shrug* Yes, so as you say he's not putting it in the best place. But the bottom line is that "the way of unix" is either to use a 1970-based seconds count, or an email-format time and date (with all the parsing problems that implies). And in any event, the FIRST thing for him to do is to fix the formatting of the number he's writing. MarkMLl ---------------------------------------------------------------------- Zvoni · 2022-04-05 07:46 Quote from: MarkMLl on April 05, 2022, 09:44:10 amQuote from: Zvoni on April 05, 2022, 09:31:53 amYes, and as the OP has stated he has full control what's inside that file *shrug* Yes, so as you say he's not putting it in the best place. But the bottom line is that "the way of unix" is either to use a 1970-based seconds count, or an email-format time and date (with all the parsing problems that implies). And in any event, the FIRST thing for him to do is to fix the formatting of the number he's writing. MarkMLl Agreed, and as i stated (even with code): Take that UnixEpoch from his File, convert it to TDateTime, compare it to "Now" using my sample code, and he can get what he wants (Days, hours, minutes, seconds of UpTime). Provided his UnixEpoch is in local time and not UTC, since "Now" returns local DateTime ---------------------------------------------------------------------- MarkMLl · 2022-04-05 07:48 Quote from: Zvoni on April 05, 2022, 09:46:56 amQuote from: MarkMLl on April 05, 2022, 09:44:10 am... the FIRST thing for him to do is to fix the formatting of the number he's writing. Agreed, and as i stated (even with code): Take that UnixEpoch from his File, convert it to TDateTime, compare it to "Now" using my sample code, and he can get what he wants (Days, hours, minutes, seconds of UpTime) Unanimous :-) MarkMLl