We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
https://Processing.org/reference/nf_.html
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
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!
Just an observation: Neither
static
norfinal
are necessary for the definition of function timestamp().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.