Add 0's to date/time in the filename with saveFrame()

edited April 2016 in Questions about Code

Hey there! This is my first post here, already found a lot of helpful stuff here just reading. I was just wondering if this is something I'm doing wrong or if it is not intended.

When I use saveFrame() to save a projects current canvas, I usually wanna have this result: Projectname-20161230-184555.png which usually corresponds to following line:

saveFrame(Project+"-"+year()+month()+day()+"-"+hour()+minute()+second()+".png");

(yes, I'm from Europe, I like my months in front of my days :P)

The only real issue is that every function returns exactly what it is supposed to (which is totally right): When it is 8 am for example it returns an 8 when I call hour(). If I want to auto-sort the files though I need the 0 in front of every digit that doesnt have 2 digits, so I came up with this (quite hacky) solution (titled as void savePNG() because it isn't a one-liner anymore so I can call it):

void savePNG() {
  if (second()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+hour()+minute()+"0"+second()+".png");
  } else if (minute()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+hour()+"0"+minute()+second()+".png");
  } else if (hour()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+"0"+hour()+minute()+second()+".png");
  } else if (minute()<10 && second()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+hour()+"0"+"0"+minute()+"0"+second()+".png");
  } else if (second()<10 && hour()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+"0"+hour()+minute()+"0"+second()+".png");
  } else if (minute()<10 && hour()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+"0"+hour()+"0"+minute()+second()+".png");
  } else if (second()<10 && minute()<10 && hour()<10) {
    saveFrame(Project+"-"+year()+month()+day()+"-"+"0"+hour()+"0"+minute()+"0"+second()+".png");
  } else {
    saveFrame(Project+"-"+year()+month()+day()+"-"+hour()+minute()+second()+".png");
  }
}

And yes, I forgot about months and days here aswell.

Is there an easier way or is that just the way it's supposed to be?

Answers

  • edited April 2016

    Thx for your post @GoToLoop. That seems to be another way to add the 0 to the left of the number. I didn't know that one before, but it doesn't make the code smoother in any way because I still have to go through all those if's

  • edited December 2017 Answer ✓
    // Forum.Processing.org/two/discussion/16001/
    // add-0-s-to-date-time-in-the-filename-with-saveframe#Item_3
    
    // GoToLoop 2016-Apr-13
    
    void setup() {
      background((color) random(#000000));
    
      String stamp = timestamp("Project", ".png");
      println(stamp);
    
      String path = dataPath(stamp);
      println(path);
    
      saveFrame(path);
      //saveFrame( dataPath( timestamp("Project", ".png") ) );
    
      exit();
    }
    
    static final String timestamp(final String name, final String ext) {
      return name + "-" + year() + nf(month(), 2) + nf(day(), 2) +
        "-" + nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2) + ext;
    }
    
  • Answer ✓

    Java's simpleDateFormat was made for this.

  • Thank you @GoToLoop and @koogs for your posts. I'm really sorry @GoToLoop that I couldn't wrap my head around what you posted yesterday, a lot was going on. It struck me when I read line 22 of your example that nf() just adds a character (0) when it is not already 2 characters wide... thats pure genius and exactly what I was looking for. Also learned something new with static and final, pretty cool stuff! Thank you very much!

  • edited April 2016

    Also learned something new with static and final, pretty cool stuff!

    Just an observation: Neither static nor final are necessary for the definition of function timestamp().

    1. https://Processing.org/reference/static.html
    2. https://Processing.org/reference/final.html

    I've just declared timestamp() as static to indicate that it's a stateless utility function.
    It means timestamp() only works w/ the parameters passed to it.
    And it can't access nor depend on the state of the sketch's canvas.

Sign In or Register to comment.