Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
16 * strftime - convert date and time to a string
18 * size_t strftime (char *array, size_t limitsize,const char*pattern,const struct tm *tme)
19 * Formats Time into string for the given tags[options] with a limit of Limitsize bytes.
21 * %a - Abbreviated weekday name (eg:Mon)
22 * %A - Full weekday name (eg:Monday)
23 * %b - Abbreviated month name (eg:Jan)
24 * %B - full month name (eg:January)
25 * %c - preferred date and time representation (eg:Tue 06 12:34:45 2009)
26 * %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
27 * %d - Day of the month as a decimal number (range 01 to 31)
28 * %D - Same as %m/%d/%y
29 * %e - Day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')
30 * %F- Equivalent to %Y-%m-%d
31 * %g - Same As %G, but without the century.
32 * %G - The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead.
34 * %H - Hour as a decimal number using a 24-hour clock (range 00 to 23)
35 * %I - Hour as a decimal number using a 12-hour clock (range 01 to 12)
36 * %j - Day of the year as a decimal number (range 001 to 366)
37 * %m - Month as a decimal number (range 01 to 12)
38 * %M - Minute as a decimal number
39 * %n - Newline character
40 * %p - UPPER-CASE `AM' or `PM' according to the given time value
41 * %P - Lower-case `am' or `pm' according to the given time value
42 * %r - Time in a.m. and p.m. notation
43 * %R - Time in 24 hour notation
44 * %S - Second as a decimal number
46 * %T - Current time, Equal to %H:%M:%S
47 * %u - Weekday as a decimal number [1,7], with 1 representing Monday
48 * %U - Week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
49 * %V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
50 * %W - Week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
51 * %w - Day of the week as a decimal, Sunday being 0
52 * %x - Preferred date representation for the current locale without the time
53 * %X - Preferred time representation for the current locale without the date
54 * %y - Year as a decimal number without a century (range 00 to 99)
55 * %Y - Year as a decimal number including the century
56 * %Z or %z - Time zone offset or name or abbreviation (Operating System dependent)
58 * Maximum length of this parameter is 1023 characters.
71 static const int dayname_length[7] ={6, 6, 7, 9, 8, 6, 8};
72 static const char*const adayname[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
74 static const char *const dayname[7] ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
76 static const int monthname_length[12] ={7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};
78 static const char *const amonthname[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
80 static const char *const monthname[12] ={"January", "February", "March", "April","May", "June", "July", "August", "September", "October", "November","December"};
83 // The Function Stores all characters in Char * array,until it finds %.
84 int splitter(char *array,size_t limitsize,const char** format,size_t* counter)
86 while(**format!='%' && **format)
88 if(*counter<limitsize-1 )
90 array[*counter]=**format;
100 array[*counter]='\0';
103 // The Funtion is Used for Storing Abbreviated WeekDay Nname in Place of %a. (eg:Mon)
104 int funa(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
106 if(*counter<limitsize-1)
108 strcat(array,adayname[tme->tm_wday]);
117 //The Function is Used for Stroring WeekDay Name in Place of %A. (eg:Monday)
118 int funA(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
120 if(*counter<limitsize-1)
122 strcat(array,dayname[tme->tm_wday]);
123 *counter+=dayname_length[tme->tm_wday];
131 // The Function is Used for Storing Abbreviated Month Name in Place of %b.(eg:Jan)
132 int funb(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
134 if(*counter<limitsize-1)
136 strcat(array,amonthname[tme->tm_mon]);
145 // The Function is Used for Storing Month Name in Place of %B .(eg:January)
146 int funB(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
148 if(*counter<limitsize-1)
150 strcat(array,monthname[tme->tm_mon]);
151 *counter+=monthname_length[tme->tm_mon];
159 // The Function is Used for Storing preferred date and time representation in Place of %c.(eg:Tue 06 12:34:45 2009)
160 int func(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
162 if(*counter<limitsize-24)
164 strcat(array,adayname[tme->tm_wday]);
166 strcat(array,amonthname[tme->tm_mon]);
168 sprintf(&array[*counter]," %.2d %.2d %.2d %.2d %.4d",tme->tm_mday,tme->tm_hour,tme->tm_min,tme->tm_sec,1900+tme->tm_year);
170 array[*counter]='\0';
178 // The Function is Used for Storing Century number in place of %C .(eg:he year divided by 100 and truncated to an integer, range 00 to 99)
179 int funC(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
181 if(*counter<limitsize-1)
183 sprintf(&array[*counter],"%.2d",(1900+tme->tm_year)/100);
185 array[*counter]='\0';
193 // The Function is Used for Storing Day of the month as a decimal number in Place of %d.(eg:range 01 to 31)
194 int fund(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
196 if (*counter < limitsize - 2)
198 sprintf (&array[*counter], "%.2d", tme->tm_mday);
200 array[*counter]='\0';
208 // The Function is Used for Storing Date in form of %m/%d/%y in Place of %D.
209 int funD(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
211 if(*counter<limitsize-8)
213 sprintf(&array[*counter],"%.2d/%.2d/%.2d",tme->tm_mon,tme->tm_mday,(1900+tme->tm_year)%100);
215 array[*counter]='\0';
223 // The Function is Used for Storing Day of the month as a Decimal number, a single digit is preceded by a space in Place of %e. (eg:range ' 1' to '31')
224 int fune(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
226 if(*counter<limitsize-2)
229 sprintf(&array[*counter]," %d",tme->tm_mday);
231 sprintf(&array[*counter],"%2d",tme->tm_mday);
233 array[*counter]='\0';
242 // The Function is Used for Storing date in form of %Y-%m-%d
243 int funF(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
245 if(*counter<limitsize-10)
247 sprintf(&array[*counter],"%4d/%.2d/%.2d",1900+tme->tm_year,tme->tm_mon,tme->tm_mday);
249 array[*counter]='\0';
257 // The Function is same as that of funG but without Century
258 int fung(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
260 if(*counter<limitsize-2)
262 sprintf(&array[*counter],"%.2d",(1900+tme->tm_year)%100);
264 array[*counter]='\0';
272 // The Function is Used For Storing 4-digit year corresponding to the ISO week number in place of %G
273 int funG(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
275 if(*counter<limitsize-4)
277 sprintf(&array[*counter],"%4d",1900+tme->tm_year);
279 array[*counter]='\0';
287 // The Function is same as funb
288 int funh(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
290 if(*counter<limitsize-1)
292 strcat(array,amonthname[tme->tm_mon]);
294 array[*counter]='\0';
302 // The Function is Used for Storing Hour as a decimal number using a 24-hour clock in Place of %H (eg:range 00 to 23)
303 int funH(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
305 if (*counter < limitsize - 2)
307 sprintf (&array[*counter], "%.2d",
310 array[*counter]='\0';
318 // The Function is Used for Storing Hour as a decimal number using a 12-hour clock in Place of %I(eg:range 01 to 12)
319 int funI(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
321 if (*counter < limitsize - 2)
324 sprintf(&array[*counter],"%.2d",tme->tm_hour%12);
328 array[*counter]='\0';
336 // The Function is Used for Storing Day of the year as a decimal number in Place of %j (eg:range 001 to 366)
337 int funj(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
339 if (*counter < limitsize - 3)
341 sprintf (&array[*counter], "%.3d", tme->tm_yday + 1);
343 array[*counter]='\0';
351 // The Function is Used for StoringThe hour (24-hour clock) as a decimal number (range 0 to 23);single digits are preceded by a blankin Place of %k
352 int funk(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
354 if(*counter<limitsize-2)
357 sprintf(&array[*counter]," %d",tme->tm_hour);
359 sprintf(&array[*counter],"%2d",tme->tm_hour);
361 array[*counter]='\0';
369 // The Function is Used for Storing The hour (12-hour clock) as a decimal number (range 1 to 12);single digits are preceded by a blank in Place of %l
370 int funl(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
372 if (*counter < limitsize - 2)
376 if(tme->tm_hour%12<10)
377 sprintf(&array[*counter],"% d",tme->tm_hour%12);
379 sprintf(&array[*counter],"%d",tme->tm_hour%12);
384 array[*counter]='\0';
392 // The Function is Used for Storing Month as a decimal number in Place of %m (eg:range 01 to 12)
393 int funm(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
395 if (*counter < limitsize - 2)
397 sprintf (&array[*counter], "%.2d", tme->tm_mon + 1);
399 array[*counter]='\0';
407 // The Function is Used for Storing Minute as a decimal number in Place of %M
408 int funM(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
410 if (*counter < limitsize - 2)
412 sprintf (&array[*counter], "%.2d", tme->tm_min);
414 array[*counter]='\0';
422 // The Function is Used for Storing `AM' or `PM' according to the given time value, or the corresponding strings for the current locale. Noon is treated as `pm' and midnight as `am'.
423 int funp(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
425 if (*counter< limitsize - 2)
427 if (tme->tm_hour < 12)
440 // The Function is Used for Storing `am' or `pm' according to the given time value, or the corresponding strings for the current locale. Noon is treated as `pm' and midnight as `am'.
441 int funP(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
443 if (*counter< limitsize - 2)
445 if (tme->tm_hour < 12)
458 //The Function is Used for Storing Hour,minute,second,time value in place of %r,same as %I:%M:%S %p'
459 int funr(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
461 if (*counter< limitsize - 11)
465 if(tme->tm_hour%12<10)
466 sprintf(&array[*counter],"% d",tme->tm_hour);
468 sprintf(&array[*counter],"%d",tme->tm_hour);
473 array[*counter]='\0';
476 sprintf (&array[*counter], "%.2d", tme->tm_min);
478 array[*counter]='\0';
481 sprintf (&array[*counter], "%2.2d",tme->tm_sec);
483 array[*counter]='\0';
486 if (tme->tm_hour < 12)
500 // The Function is Used for Storing The time in 24-hour notation in Place of %R .Same as (%H:%M)..
501 int funR(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
503 if (*counter< limitsize - 5)
505 sprintf (&array[*counter], "%.2d",
509 sprintf (&array[*counter], "%.2d", tme->tm_min);
511 array[*counter]='\0';
520 // The Function is Used for Storing The number of seconds since the Epoch, i.e., since 1970-01-0 0:00:00 UTC.in place of %s
521 int funs(char*array,size_t limitsize,size_t *counter,const struct tm*tme)
525 time_t timeinseconds;
526 timeinseconds = time (NULL);
527 temptime=timeinseconds;
533 if(*counter<limitsize-count)
535 sprintf(&array[*counter],"%ld",timeinseconds);
537 array[*counter]='\0';
546 // The Function is Used for Storing The second as a decimal number (eg:range 00 to 60)
547 int funS(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
549 if (*counter < limitsize - 2)
551 sprintf (&array[*counter], "%.2d", tme->tm_sec);
553 array[*counter]='\0';
561 // The Function is Used for Storing The time in 24-hour notation (%H:%M:%S) in place of %T
562 int funT(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
564 if (*counter < limitsize - 2)
566 sprintf (&array[*counter], "%.2d",tme->tm_hour);
568 array[*counter]='\0';
571 sprintf (&array[*counter], "%.2d", tme->tm_min);
573 array[*counter]='\0';
576 sprintf (&array[*counter], "%.2d", tme->tm_sec);
578 array[*counter]='\0';
586 // The Function is Used for Storing The day of the week as a decimal, range 1 to 7, Monday being 1 in Place of %U.
588 int funu(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
590 if (*counter < limitsize - 1)
592 sprintf (&array[*counter], "%.1d",tme->tm_wday);
594 array[*counter]='\0';
602 // The Function is Used for Storing The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01 in Place of %U
603 int funU(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
605 if (*counter < limitsize - 2)
607 int temp= tme->tm_yday/7;
608 if (tme->tm_yday%7 >tme->tm_wday)
610 sprintf(&array[*counter],"%.2d",temp);
612 array[*counter]='\0';
620 // The Function is Used for Storing The day of the week as a decimal, range 0 to 6, Sunday being 0 in Place of %w
621 int funw(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
623 if (*counter < limitsize - 1)
625 sprintf (&array[*counter], "%.1d",tme->tm_wday);
627 array[*counter]='\0';
635 // The Function is Used for Storing The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01 in Place of %W
637 int funW(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
639 if (*counter < limitsize - 2)
641 int temp= tme->tm_yday/7;
642 if (tme->tm_yday%7 > (tme->tm_wday+6)%7)
644 sprintf(&array[*counter],"%.2d",temp);
646 array[*counter]='\0';
654 // The Function is Used for Storing preferred date representation for the current locale without the time in Place of %x
655 int funx(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
657 if (*counter < limitsize - 15)
659 strcat(array,adayname[tme->tm_wday]);
661 array[*counter]='\0';
664 strcat(array,amonthname[tme->tm_mon]);
667 sprintf (&array[*counter]," %.2d %.4d", tme->tm_mday,1900 + tme->tm_year);
669 array[*counter]='\0';
677 // The Function is Used for Storing preferred time representation for the current locale without the date in Place of %X
678 int funX(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
680 if (*counter < limitsize - 8)
682 sprintf (&array[*counter],"%2.2d:%2.2d:%2.2d",tme->tm_hour, tme->tm_min,tme->tm_sec);
684 array[*counter]='\0';
692 // The Function is Used for Storing year as a decimal number without a century (range 00 to 99). in Place of %y
693 int funy(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
695 if (*counter < limitsize - 2)
697 sprintf (&array[*counter], "%.2d",tme->tm_year);
699 array[*counter]='\0';
707 // The Function is Used for Storing The year as a decimal number including the century in Place of %Y
708 int funY(char *array,size_t limitsize,size_t *counter,const struct tm*tme)
710 if (*counter < limitsize - 4)
712 sprintf (&array[*counter], "%.4d",1900 + tme->tm_year);
714 array[*counter]='\0';
723 //- convert/Formats date and time to a string for the given tags[options] with a limit of Limitsize bytes
724 // It formats the broken-down time according to the format specification format and places the result in the character array of Limitsize .The Function returns the number of characters written in the array,
725 // if successful and 0 if the size exceeeds the limit .
727 EXPORT_C size_t strftime (char *array, size_t limitsize,const char*pattern,const struct tm *tme)
729 size_t* counter=(size_t* )malloc(sizeof(size_t));
730 size_t error=0,funerror=0;
732 array[*counter]='\0';
735 error=splitter(array,limitsize,&pattern,counter);
746 if(*counter<limitsize-1)
747 array[(*counter)++]='%';
752 funerror= funa(array,limitsize,counter,tme);
757 funerror=funA(array,limitsize,counter,tme);
762 funerror=funb(array,limitsize,counter,tme);
767 funerror=funB(array,limitsize,counter,tme);
772 funerror=func(array,limitsize,counter,tme);
777 funerror=funC(array,limitsize,counter,tme);
782 funerror=fund(array,limitsize,counter,tme);
787 funerror=funD(array,limitsize,counter,tme);
792 funerror=fune(array,limitsize,counter,tme);
797 funerror=funF(array,limitsize,counter,tme);
802 funerror=fung(array,limitsize,counter,tme);
807 funerror=funG(array,limitsize,counter,tme);
812 funerror=funH(array,limitsize,counter,tme);
817 funerror=funI(array,limitsize,counter,tme);
822 funerror=funj(array,limitsize,counter,tme);
827 funerror=funk(array,limitsize,counter,tme);
832 funerror=funl(array,limitsize,counter,tme);
837 funerror=funm(array,limitsize,counter,tme);
842 funerror=funM(array,limitsize,counter,tme);
847 funerror=funp(array,limitsize,counter,tme);
852 funerror=funP(array,limitsize,counter,tme);
857 funerror=funr(array,limitsize,counter,tme);
862 funerror=funR(array,limitsize,counter,tme);
867 funerror=funs(array,limitsize,counter,tme);
872 funerror=funS(array,limitsize,counter,tme);
877 funerror=funT(array,limitsize,counter,tme);
882 funerror=funu(array,limitsize,counter,tme);
887 funerror=funU(array,limitsize,counter,tme);
892 funerror=funw(array,limitsize,counter,tme);
897 funerror=funW(array,limitsize,counter,tme);
902 funerror=funx(array,limitsize,counter,tme);
907 funerror=funX(array,limitsize,counter,tme);
912 funerror=funy(array,limitsize,counter,tme);
917 funerror=funY(array,limitsize,counter,tme);
935 array[*counter]='\0';