extracting minutes from a timestamp

edited May 2014 in How To...

is there a way to extract seconds or minutes from a time-stamp in processing?

Tagged:

Answers

  • What type of timestamp is it? Do you have example code?

    What are you trying to extract? The seconds or minutes of the current system time, or the elapsed seconds or minutes since starting the sketch?

  • edited May 2014

    in my database (SQLite) i have a timestamp for each order, and i want to extract the minutes and seconds to later deduce the elapsed time by comparing it to processing timestamp.

    and i have an ETA on each order measured in minutes integer.

  • What format is your original timestamp in? What format is your Processing timestamp in?

    There are many different ways to represent timestamps. Which one are you using?

  • edited May 2014

    this is the SQLite timestamp ,

    ![image alt text](C:\Users\abdokhoury\Documents\Bluetooth\Inbox\SQlite Timestamp.jpg)

    i am using processing 2.1.1 and any Processing timestamp would do

  • edited May 2014

    this is the SQLite timestamp ,

    2014-05-20 17:19:50.000

    i am using processing 2.1.1 and any Processing timestamp would do

    I tried to upload an image for you but it is not working sorry :-/ :-/ :-/

  • String timestamp = "2014-05-20 17:19:50.000";
    
    String[] m = match(timestamp, "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:(\\d{2})\\.\\d{3}$");
    println(m[1]);
    int seconds = int(m[1]);
    
  • wow that is great, thanks man , =D> but how can i get the minutes though , appreciate it

  • Hello, thank you again for your help, could you please let me know how can i extract the minutes, thank you very much

  • String timestamp = "2014-05-20 17:19:54.000";
    
    String[] m = match(timestamp, "^(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})$");
    
    int year = int(m[1]);
    int month = int(m[2]);
    int day = int(m[3]);
    int hours = int(m[4]);
    int minutes = int(m[5]);
    int seconds = int(m[6]);
    int millisecs = int (m[7]);
    

    Everything you have within brackets will be added to your string array 'm'. So if you only want minutes and seconds you'd bracket them and fetch them from the string array.

  • You could also use Java's built in SimpleDateFormat class:

    import java.text.SimpleDateFormat;
    
    SimpleDateFormat from = new SimpleDateFormat("YYYY-MM-dd kk:mm:ss.SSS");
    SimpleDateFormat to = new SimpleDateFormat("mm");
    
    void draw() {
      background(0);
    
      try {
        String timestamp = "2014-05-20 17:19:50.000";
        String minutes = to.format(from.parse(timestamp));
        println(minutes);
      }
      catch(Exception e) {
        //something went wrong with the parsing
      }
    }
    
  • edited May 2014

    @KevinWorkman, nice clean ( except the catch block :P ) Java-based style solution there! =D>
    I'm also here to present even another alternative to @PhiLho's. Since I still have to learn regex and find it confusing... :o3
    So instead of match(), I'm gonna replace it w/ splitTokens(): :O)

    // forum.processing.org/two/discussion/5280/extracting-minutes-from-a-timestamp
    
    String timestamp = "2014-05-20 17:19:54.000";
    
    //int[] tokens = int(match(timestamp, 
    //"^(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})$"));
    
    int[] tokens = int(splitTokens(timestamp, "- :."));
    
    int ye = tokens[0], mo = tokens[1], da = tokens[2];
    int hours = tokens[3], minutes = tokens[4];
    int seconds = tokens[5], millisecs = tokens[6];
    
    println(tokens);
    exit();
    
  • edited May 2014

    Yes, KevinWorkman's solution is to be preferred!

    Sorry for the hasty answer, indeed I caught seconds instead of minutes...

    GoToLoop, this is a only simple example of regex... :-) If you are curious about them, tell me what you think of my article: Regular Expressions: a simple, easy tutorial

  • _vk_vk
    edited May 2014

    @PhiLho Thanks. Great Stuff. I still reading it.

    DId I miss something or the first example should be:

    IsMatch("[C:]\tmp", "\[C\:\]\\")
    

    instead of:

    IsMatch("[C:]\tmp", "\[C\:\\\]")
    

    ? : )

  • Good point, _vk. I will fix that. Thanks.
    I wrote that years ago and nobody ever pointed that out. :-/

Sign In or Register to comment.