#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12

    use time and date for serial in linux


    I have a bit of code that writes dns records and im trying to figure out how i can generate a serial number in the format of
    Code:
    [yyyyMMddNN]
    The nn can stay the same. So what i need to do is take the year, month and date from the system plus "nn" which for argument sake is allways "00" and assign it to a variable. If some one can give me a hand that would be great.

    Thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    Code:
    #include <time.h>
    #include <stdio.h>
    #include <string.h>
    
    static char *serial_new(char *serial, int n) {
    	struct tm *today;
    	time_t t;
    	char buf[3];	
    
    	t = time(NULL);
    	today = localtime(&t);
    	
    	// yyyy
    	sprintf(serial, "%i", today->tm_year + 1900);
    
    	// MM
    	if (today->tm_mon + 1 < 10)
    		sprintf(buf, "0%i", today->tm_mon + 1);
    	else
    		sprintf(buf, "%i", today->tm_mon + 1);
    	strcat(serial, buf);
    
    	// dd
    	if (today->tm_mday < 10)
    		sprintf(buf, "0%i", today->tm_mday);
    	else
    		sprintf(buf, "%i", today->tm_mday);
    	strcat(serial, buf);
    
    	// NN
    	if (n < 10)
    		sprintf(buf, "0%i", n);
    	else
    		sprintf(buf, "%i", n);
    	strcat(serial, buf);
    	serial[11] = 0;
    
    	return serial;
    }
    
    int main(int argc, char* argv[]) {
    	char serial[12];
    
    	printf("Serial (00): %s\n", serial_new(serial, 0));
    	printf("Serial (10): %s\n", serial_new(serial, 10));
    	printf("Serial (99): %s\n", serial_new(serial, 99));
    
    	return 0;
    }
    Output:

    Code:
    Serial (00): 2003091400
    Serial (10): 2003091410
    Serial (99): 2003091499

    Hope this is what you want... :-)

    Wingman
    Last edited by Wingman; September 14th, 2003 at 12:54 PM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Since you have the time in a tm struct, you could use a single call to strftime to create a time string formatted the way you specify it. Google on it or read the man page.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12
    Thanks dwise1_aol.

    I had a look in my C++ book and I found

    Code:
    #include <time.h>
    
    size_t strftime(char *s, size_t maxsize, const char *format,
        const struct tm *timptr);
    Sorry for sounding dumb but I cant get it to work.
    Last edited by phantom_turtle; September 14th, 2003 at 07:53 PM.
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    searching this forum can help a whole lot.
    see my post (or one of the others) in this thread:
    Making time in simple dd/mm/yyyy hh:mm:ss format

IMN logo majestic logo threadwatch logo seochat tools logo