mihainan

Lab - PC

Dec 1st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. char* timestr(struct tm *t, char* time) {
  7.     char s[10];
  8.      int n;
  9.    
  10. time = malloc(20 * sizeof(char));
  11.  
  12.  sprintf(s,"%02d-", t->tm_mday);
  13.     strcpy(time, s);
  14.    
  15.   sprintf(s,"%02d-", t->tm_mon);
  16.     strcat(time, s);
  17.    
  18.   sprintf(s,"%d\n", 1900 + t->tm_year);
  19.     strcat(time, s);
  20.    
  21.   sprintf(s,"%02d:", t->tm_hour);
  22.     strcat(time, s);
  23.     sprintf(s,"%02d:", t->tm_min);
  24.         strcat(time, s);
  25.     sprintf(s,"%02d", t->tm_sec);
  26.         strcat(time, s);
  27. }
  28.  
  29. int main() {
  30.     struct tm *timp;
  31.      time_t t;
  32.      char *ctime;
  33.      t = time(NULL);
  34.       timp = localtime(&t);
  35.     ctime = timestr(timp, ctime);
  36.      printf("%s\n", ctime);
  37.   return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment