0

I'm working on a IRC Twitch.tv BOT (PircBot) and i want to implement a !uptime chatcommand which will display how long the Stream is online.

I searched in google a Bit and i only found solutions which requires a GUI.

Can Some1 tell me what libraries are good to use or give me some exampel code?

I need to display seconds, minutes, hours, days.

First i thought about doing a normal timer which counts +1 all seconds but i think its easier and there are some proper functions to handle such "count"-timers, right?

Im fine with any hints!

thanks :)

Thats what i came up with now:

In my Main class, i got a timer which i call with:

utimecounttimer();

My Timer looks like this:

  public void utimecounttimer() {

        uptimetimer.scheduleAtFixedRate(new TimerTask() {
              @Override
              public void run() {
                  if (isstreamlive == true){
                  UptimeCount.Uptimestart();
                  }else{
                      UptimeCount.Uptimestop();
                  }
              }
              }, 1000, 1000);
}

and then my UptimeCount Class is here:

public class UptimeCount {

    public static long startTime = 0;
    public static long  OnlineTimeMillis = 0;

    public static float OnlineTimeSec = 0;
    public static float  OnlineTimeMin = 0;
    public static float OnlineTimeHour = 0;
    public static float OnlineTimeDay = 0;



    public static void Uptimestart(){

        if(startTime == 0){
        startTime = System.currentTimeMillis();
        }else


        if(startTime != 0){
        OnlineTimeMillis = System.currentTimeMillis()-startTime;
        OnlineTimeSec = OnlineTimeMillis /1000F;
        OnlineTimeMin = OnlineTimeMillis /(60*1000F);
        OnlineTimeHour = OnlineTimeMillis /(60*60*1000F);
        OnlineTimeDay = OnlineTimeMillis /(24*60*60*1000F);
        }


        System.out.println("Seconds"+OnlineTimeSec);
        System.out.println("Minutes"+OnlineTimeMin);
        System.out.println("Hours"+OnlineTimeHour);
        System.out.println("Days"+OnlineTimeDay);

    }



    public static void Uptimestop(){

        startTime = 0;
        OnlineTimeMillis = 0;       
        OnlineTimeSec = 0;
        OnlineTimeMin = 0;
        OnlineTimeHour = 0;
        OnlineTimeDay = 0;
    }


}

And then, last but not least il get the Infos in chat with the following line in my Main class:

if (message.equalsIgnoreCase("!uptime")) {
    sendMessage(channel," Stream is running for: "+UptimeCount.OnlineTimeDay +" Days, "+UptimeCount.OnlineTimeHour +" Hours. "+UptimeCount.OnlineTimeMin +" Minutes and " +UptimeCount.OnlineTimeSec +" Seconds.");
}

I didnt test it yet but i think i have to format the ms output of the floats and then it should be working, right?

user3220962
  • 371
  • 1
  • 7
  • 19
  • 1
    If you can use Guava, you have `StopWatch` – fge Feb 26 '14 at 09:03
  • Why not write your custom thread which keeps on incrementing this time counter and have a helper method embedded in it which tells you the seconds, minutes, hours and days the timer was running? Have it set up as a Daemon thread, so that it isn't running when your main application isn't running, but running only when the main application is running. – Aman Agnihotri Feb 26 '14 at 09:04
  • Use the system clock to detect how much time has elapsed. Use Joda library to get proper values to display. – Ted Bigham Feb 26 '14 at 09:08
  • Il take a look at it fge, thanks. @Awesome so u say i should go with my thoughts on doing a simple +1 timer thing? if yes, i do not understand what u mean by "helper method" or running it as a daemon thread. im still pretty new to java =P – user3220962 Feb 26 '14 at 09:09
  • @ ted that wouldnt work. The Bot is online/running the whole time but it will connect automatic to the Stream as soon its online and right there it shoudl start counting so im not sure if i can tell the program to get the systemtime from that point then? – user3220962 Feb 26 '14 at 09:11

2 Answers2

1
// it will give you  current time in mille seconds

long startTime = System.currentTimeMillis();


// Mill second diffrence 

long  OnlineTimeMillis = System.currentTimeMillis()-startTime;


// time diff in mille seconds since  you are online 

float OnlineTimeSec = OnlineTimeMillis /1000F;

// time diff in Minutes  since  you are  online
float  OnlineTimeMin = OnlineTimeMillis /(60*1000F);

// time diff in Hours  since  you are online
float OnlineTimeHour = OnlineTimeMillis /(60*60*1000F);

// time diff in days  since  you are online  
float OnlineTimeDay = OnlineTimeMillis /(24*60*60*1000F);
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
  • This looks interesting. Thanks! Im able to just put that stuff in a method so i can only call it as soon as the bot connects to the stream IRC when its live and start counting from there? – user3220962 Feb 26 '14 at 09:16
  • Keeping the above stuff in method and run in separate thread when users gets connected to stream @user3220962? – Sumeet Kumar Yadav Feb 26 '14 at 09:21
  • 1
    I guess so? pack it into a static void xyz() so i can call that one as soon the bot connects each second since then so the floats getting updated or is my thinking on this one wrong? – user3220962 Feb 26 '14 at 09:25
  • update your question with your code @user3220962 – Sumeet Kumar Yadav Feb 26 '14 at 09:28
  • gonan write it right now. just give me some minutes =) – user3220962 Feb 26 '14 at 09:36
  • Hey, i did edit the post above to your needs. Hope for an reply =) Thanks in advise! Oh snap, it seems i tried to edit your post :o. im sorry! i just edited mine now. this site is a bit irritating for a beginner like me hehe – user3220962 Feb 26 '14 at 09:58
1

You can use Joda Time to represent durations and times:

// at startup of the stream
DateTime startedAt = DateTime.now();

// later
Duration elapsed = new Duration(startedAt, DateTime.now());

And could use the solution described in this answer to format your output, to get a human readable output (ie. "1 hours 2 minutes").

Please also check Duration since it has methods like toStandardHours, toStandardMinutes which can be used to display the total number of hours/minutes/etc. elapsed (ie. "1 hours | 62 minutes").

Community
  • 1
  • 1
David Lantos
  • 670
  • 4
  • 8