! * \brief l_getFormattedDate() * * \return formatted date string, or NULL on error * * * Notes: * (1) This is used in pdf, in the form specified in section 3.8.2 of * http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf * (2) Contributed by Dave Bryan. Works on all platforms. * */
| 1101 | * </pre> |
| 1102 | */ |
| 1103 | char * |
| 1104 | l_getFormattedDate() |
| 1105 | { |
| 1106 | char buf[sizeof "199812231952SS-08'00'"] = "", sep = 'Z'; |
| 1107 | l_int32 gmt_offset, relh, relm; |
| 1108 | time_t ut, lt; |
| 1109 | struct tm *tptr; |
| 1110 | |
| 1111 | ut = time(NULL); |
| 1112 | |
| 1113 | /* This generates a second "time_t" value by calling "gmtime" to |
| 1114 | fill in a "tm" structure expressed as UTC and then calling |
| 1115 | "mktime", which expects a "tm" structure expressed as the |
| 1116 | local time. The result is a value that is offset from the |
| 1117 | value returned by the "time" function by the local UTC offset. |
| 1118 | "tm_isdst" is set to -1 to tell "mktime" to determine for |
| 1119 | itself whether DST is in effect. This is necessary because |
| 1120 | "gmtime" always sets "tm_isdst" to 0, which would tell |
| 1121 | "mktime" to presume that DST is not in effect. */ |
| 1122 | tptr = gmtime(&ut); |
| 1123 | tptr->tm_isdst = -1; |
| 1124 | lt = mktime(tptr); |
| 1125 | |
| 1126 | /* Calls "difftime" to obtain the resulting difference in seconds, |
| 1127 | * because "time_t" is an opaque type, per the C standard. */ |
| 1128 | gmt_offset = (l_int32) difftime(ut, lt); |
| 1129 | |
| 1130 | if (gmt_offset > 0) |
| 1131 | sep = '+'; |
| 1132 | else if (gmt_offset < 0) |
| 1133 | sep = '-'; |
| 1134 | |
| 1135 | relh = L_ABS(gmt_offset) / 3600; |
| 1136 | relm = (L_ABS(gmt_offset) % 3600) / 60; |
| 1137 | |
| 1138 | strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", localtime(&ut)); |
| 1139 | sprintf(buf + 14, "%c%02d'%02d'", sep, relh, relm); |
| 1140 | return stringNew(buf); |
| 1141 | } |
no test coverage detected