File: strftime.c

package info (click to toggle)
catdoc 0.94.4-1.1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd, wheezy
  • size: 2,940 kB
  • sloc: ansic: 4,186; tcl: 262; makefile: 220; sh: 212
file content (70 lines) | stat: -rw-r--r-- 1,784 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "strftime.h"

static const char *monthAbbr[]={
	"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
size_t strftime(char *s,size_t max, const char *format,
		const struct tm *tm) {
	size_t i=0,j=0;
	while(j<max && format[i]) {
		if (format[i]!='%') {
			s[j]=format[i];
			i++;j++;
		} else {
			i++;
			switch (format[i]) {
				case '%': s[j++]='%';break;
				case 'm': if ((j+2) >=max) return 0;
						  j+=sprintf(s+j,"%02d",tm->tm_mon+1);
						  break;	 	  
				case 'd': if ((j+2) >=max) return 0;
						  j+=sprintf(s+j,"%02d",tm->tm_mday);
						  break;
				case 'y': if ((j+2) >=max) return 0;
						  j+=sprintf(s+j,"%02d",tm->tm_year%100);
						  break;
				case 'Y': if ((j+4) >=max) return 0;
						  j+=sprintf(s+j,"%d",tm->tm_year+1900);	  
						  break;
				case 'b': if ((j+3)>=max) return 0;
							 strcpy(s+j,monthAbbr[tm->tm_mon]); 
						  	 j+=3;
						  break;
				case 'l': if ((j+2) >= max) return 0;
							  { int hour = tm->tm_hour;
								  if (hour>12) hour -=12;
								  if (hour == 0) hour = 12;
								  j+=sprintf(s+j,"%2d",hour);
								  break;
							  }
				case 'p':  if ((j+2) >= max) return 0;
						  if (tm->tm_hour >11) {
							  strcpy(s+j,"PM");
						  } else {
							  strcpy(s+j,"AM");
						  }
						   j+=2;
						  break;
				case 'H':  if ((j+2) >= max) return 0;
						   j+=sprintf(s+j,"%02d",tm->tm_hour);
							break;	   
				case 'M':if ((j+2) >= max) return 0;
						   j+=sprintf(s+j,"%02d",tm->tm_min);
							break;
				case 'S':if ((j+2) >= max) return 0;
						   j+=sprintf(s+j,"%02d",tm->tm_sec);
							break;
				default:			
							;
			}
			i++;
		}	
	}
	if (j>=max) return 0;
	else {
		s[j]=0;
		return j;
	}
}