[Class] Time String Expression Parser

Discussion in 'Resources' started by Dragonphase, Mar 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Dragonphase

    A short while ago, I created a class which could be used to convert a String expression as a time into the appropriate number of seconds. This could be useful in several situations, such as creating delays that can be translated from a String expression into milliseconds, seconds, minutes, hours, etc.

    If you come across any errors, please feel free to point them out and I will fix them.

    Source available on Gist (Could not paste here due to formatting issues).

    Usage:

    Code:java
    1. String delay = "1y2mo3d4h5m6s789ms"; //1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds and 789 milliseconds.
    2. Time time = new Time(delay);
    3. int milliseconds = time.getMilliseconds();
    4. double seconds = time.getSeconds();
    5. double minutes = time.getMinutes();
    6. double hours = time.getHours();
    7. double days = time.getDays();
    8. double months = time.getMonths();
    9. double years = time.getYears();


    The resulting translated time is stored as an integer in milliseconds; seconds, minutes, hours, days, months and years are returned as a double.

    Limitations:

    Months are not returned accurately, as the number of days in a month differs depending on the current month. Due to this, the default number used to represent the number of days in a month for this class is 30.

    It's not recommended to use months or years as they will return incorrect values when used. They are just here for the sake of showing an example of how to use the class.
     
    xCyanide likes this.
  2. Offline

    BungeeTheCookie

    Wow. My prayers have been answered.

    Dragonphase
    How would you do this using milliseconds?
     
  3. Offline

    Dragonphase

    BungeeTheCookie You mean how would you pass a string containing milliseconds? Like "2m30s400ms"?

    You would need to change the initial storage type to milliseconds and add another parameter to the switch/case statement. Let me take a look and I'll put my results in the main post.

    BungeeTheCookie Alright, it's updated. It now translates everything into Milliseconds, which is returned as an integer. Seconds, Minutes, Hours, Days, Months and Years are now returned as a double.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    BungeeTheCookie

    Gracias.
     
  5. Offline

    Staartvin

    It might be smart to change the milliseconds to long instead of int.
     
  6. Offline

    Loogeh

    Dragonphase Staartvin is right, as an integer it could only hold up to approximately 24.8 days of milliseconds (max int value 2147483647). However as a long it could hold up to 106751991167 days worth of milliseconds (max long value 9223372036854775807). Quite a large difference hahahaha
     
  7. Offline

    Dragonphase

    Staartvin Loogeh

    I've looked into this previously; it would be smart to do that but at the time I was wondering who would actually want to schedule something for over a month. I suppose I'll change it anyways.
     
  8. Offline

    Staartvin

    Dragonphase Ah, that's good to hear. You never know, there might be someone that wants to use years or something.
     
  9. Offline

    Loogeh

    Dragonphase As one example, people might want to make a ban plugin. It wouldn't be a very good idea to store their ban time in #d#h#m format but I always just like to be on the safe side.
     
  10. Offline

    Dragonphase

    Loogeh Well, they don't have to store it in #d#h#m format, they can simply use this as a way to easily parse a #d#h#m format into milliseconds, like the following example:

    Code:java
    1. /ban user 5h //Ban user for 5 hours
    2. /mute user 5m //Mute user for 5 minutes
    3. /schedule cleanup 1h //Schedule a server cleanup every hour


    These commands could use the Time class to parse the string expression and store the millisecond value of the result.

    You could store the current time and the length of the time like so:

    Code:java
    1. long banTime = Calendar.getInstance().getTimeInMillis();
    2. long banLength = new Time("1h")


    Then, you would use this information when you want to check whether the time limit has been reached or passed:

    Code:java
    1. long currentTime = Calendar.getInstance().getTimeInMillis();
    2. if (currentTime - banTime >= banLength.getMilliseconds()){
    3. //Time limit reached, unban user
    4. }
     
  11. Offline

    AoH_Ruthless

    While your reasoning is logical, it's not a good idea to assume what your users might want. When making anything public, you should incorporate as many features as possible that anyone can use to any extent that they choose. A simple change like that isn't very taxing (imo) and it can make the difference for a few people :)
     
    Dragonphase likes this.
  12. Offline

    xCyanide

    Dragonphase
    Very nice :), I used some of the code for my plugin and I learned some stuff I didn't even know about java :p
     
  13. Offline

    Dragonphase

Thread Status:
Not open for further replies.

Share This Page