How to parse time

I have a .csv file that has time stamps in the format HH:MM:SS.MS. Example below:

Time,Z-axis,Y-axis,X-axis,Battery,∞Celsius,EDA(uS),Event
---------------------------------------------------------
12:00:45.000,-0.100,-0.060,1.080,-1,19.100,0.000,0
12:00:45.125,-0.090,-0.060,1.080,-1,19.100,0.002,0
12:00:45.250,-0.100,-0.060,1.080,-1,19.100,0.000,0
12:00:45.375,-0.090,-0.060,1.090,-1,19.100,0.000,0
12:00:45.500,-0.100,-0.060,1.080,-1,19.100,0.000,0
12:00:45.625,-0.090,-0.060,1.080,-1,19.100,0.000,0

I want to be able to read the data in each row/row at user-specified intervals as integers. However, currently the only way I can see to parse the time stamp is as a string. Can anyone help me convert it to an integer?

Answers

  • Which part of this is giving you trouble?

    Can you read the file in line by line? If not, post an MCVE that shows what you've tried for reading the file in. Note that this shouldn't include any of the date parsing data, as that's a separate problem.

    If you can read the file in and are just having trouble parsing the date, then post an MCVE showing what you've tried for parsing the date, without any of the file reading logic, since that's a separate problem. Just hardcode a date String and try to print out its parsed value.

    What exactly do you mean when you say you want to convert the timestamp to an integer? What should that integer represent? The unix timestamp? Just the hour? Something else? Again, MCVEs go a long way.

  • edited September 2014 Answer ✓

    How about a simpler customized TimeStamp class? O:-)

    /**
     * TimeStamp (v1.23)
     * by GoToLoop (2014/Sep)
     *
     * forum.processing.org/two/discussion/7001/how-to-parse-time
     */
    
    void setup() {
      TimeStamp stamp = new TimeStamp("12:00:45.375");
      println(stamp);
      exit();
    }
    
    final class TimeStamp {
      byte hh, mm, ss;
      short ms;
    
      TimeStamp(float h, float m, float s, float l) {
        setStamp(h, m, s, l);
      }
    
      void setStamp(float h, float m, float s, float l) {
        hh = (byte)  min(abs(h), 23);
        mm = (byte)  min(abs(m), 59);
        ss = (byte)  min(abs(s), 59);
        ms = (short) min(abs(l), 999);
      }
    
      TimeStamp(String ts) {
        parseStamp(ts);
      }
    
      void parseStamp(String ts) {
        float[] units = float(split(ts, ':'));
        int l = int(splitTokens(nf(units[2], 0, 3), ".,")[1]);
        setStamp(units[0], units[1], units[2], l);
      }
    
      @ Override String toString() {
        return nf(hh, 2) + ':' + nf(mm, 2) + ':'
          + nf(ss, 2) + '.' + nf(ms, 3);
      }
    }
    
  • edited September 2014 Answer ✓

    An even more complete version. It's sort()-ready and got other tricks too: :-bd
    http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-java.lang.Object:A-
    http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-

    /**
     * TimeStamp (v2.14)
     * by GoToLoop (2014/Sep)
     *
     * forum.processing.org/two/discussion/7001/how-to-parse-time
     */
    
    final String[] stamps = {
      "12:00:45.000", "12:00:45.625", "12:00:45.125", 
      "12:00:45.500", "12:00:45.250", "12:00:45.375"
    };
    
    final TimeStamp[] times = new TimeStamp[stamps.length];
    
    void setup() {
      int idx = 0;
      for (String s: stamps)  times[idx++] = new TimeStamp(s);
      println(times);
      println();
    
      java.util.Arrays.sort(times);
      println(times);
      exit();
    }
    
    class TimeStamp implements Cloneable, Comparable<TimeStamp> {
      byte hh, mm, ss;
      short ms;
    
      TimeStamp(float h, float m, float s, float l) {
        setStamp(h, m, s, l);
      }
    
      void setStamp(float h, float m, float s, float l) {
        hh = (byte)  min(abs(h), 23);
        mm = (byte)  min(abs(m), 59);
        ss = (byte)  min(abs(s), 59);
        ms = (short) min(abs(l), 999);
      }
    
      TimeStamp(String ts) {
        parseStamp(ts);
      }
    
      void parseStamp(String ts) {
        float[] units = float(split(ts, ':'));
        int l = int(splitTokens(nf(units[2], 0, 3), ".,")[1]);
        setStamp(units[0], units[1], units[2], l);
      }
    
      @ Override TimeStamp clone() {
        try {
          return (TimeStamp) super.clone();
        }
        catch (CloneNotSupportedException cause) {
          throw new RuntimeException(cause);
        }
      }
    
      @ Override int hashCode() {
        return hh<<27 | mm<<21 | ss<<15 | ms;
      }
    
      @ Override boolean equals(Object o) {
        return hashCode() == o.hashCode();
      }
    
      @ Override int compareTo(TimeStamp ts) {
        return hashCode() - ts.hashCode();
      }
    
      @ Override String toString() {
        return nf(hh, 2) + ':' + nf(mm, 2) + ':'
          + nf(ss, 2) + '.' + nf(ms, 3);
      }
    }
    
  • edited September 2014

    Another variation. This time the TimeStamp class is immutable: (*)

    /**
     * TimeStamp (v3.02)
     * by GoToLoop (2014/Sep)
     *
     * forum.processing.org/two/discussion/7001/how-to-parse-time
     */
    
    final String[] stamps = {
      "12:00:45.000", "12:00:45.625", "12:00:45.125", 
      "12:00:45.500", "12:00:45.250", "12:00:45.375"
    };
    
    final TimeStamp[] times = new TimeStamp[stamps.length];
    
    void setup() {
      int idx = 0;
      for (String s: stamps)  times[idx++] = new TimeStamp(s);
      println(times);
      println();
    
      java.util.Arrays.sort(times);
      println(times);
    
      println("\n" + times[0].equals(times[0].clone()));
      exit();
    }
    
    final class TimeStamp implements Cloneable, Comparable<TimeStamp> {
      final byte hh, mm, ss;
      final short ms;
      final int hash;
    
      TimeStamp(float h, float m, float s, float l) {
        hh = (byte)  min(abs(h), 23);
        mm = (byte)  min(abs(m), 59);
        ss = (byte)  min(abs(s), 59);
        ms = (short) min(abs(l), 999);
    
        hash = hh<<26 | mm<<20 | ss<<14 | ms;
      }
    
      TimeStamp(float... f) {
        this(f[0], f[1], f[2]
          , int(splitTokens(nf(f[2], 0, 3), ".,")[1]));
      }
    
      TimeStamp(String ts) {
        this(float(split(ts, ':')));
      }
    
      @ Override TimeStamp clone() {
        try {
          return (TimeStamp) super.clone();
        }
        catch (CloneNotSupportedException cause) {
          throw new RuntimeException(cause);
        }
      }
    
      @ Override int hashCode() {
        return hash;
      }
    
      @ Override boolean equals(Object o) {
        return hash == o.hashCode();
      }
    
      @ Override int compareTo(TimeStamp ts) {
        return hash - ts.hash;
      }
    
      @ Override String toString() {
        return nf(hh, 2) + ':' + nf(mm, 2) + ':'
          + nf(ss, 2) + '.' + nf(ms, 3);
      }
    }
    
  • Tried to run your code, got an ArrayIndexOutOfBoundsException (1) on line 44.

  • edited September 2014

    Tried to run your code, got an ArrayIndexOutOfBoundsException (1) on line 44.

    I knew I shoulda used splitTokens() instead of just split()! b-(
    Checking out for a dot only isn't enough! Some locales use comma instead! =:)
    It's all fixed now: , int(splitTokens(nf(f[2], 0, 3), ".,")[1])); :-B

Sign In or Register to comment.