add fmt_iso8601

master
leitner 10 years ago
parent 695843c79e
commit ad5ce55281

@ -137,6 +137,9 @@ size_t fmt_humank(char* dest,unsigned long long l);
/* "Sun, 06 Nov 1994 08:49:37 GMT" */ /* "Sun, 06 Nov 1994 08:49:37 GMT" */
size_t fmt_httpdate(char* dest,time_t t); size_t fmt_httpdate(char* dest,time_t t);
/* "2014-05-27T19:22:16.247Z" */
size_t fmt_iso8601(char* dest,time_t t);
#define FMT_UTF8 5 #define FMT_UTF8 5
#define FMT_ASN1LENGTH 17 /* enough space to hold 2^128-1 */ #define FMT_ASN1LENGTH 17 /* enough space to hold 2^128-1 */
#define FMT_ASN1TAG 19 /* enough space to hold 2^128-1 */ #define FMT_ASN1TAG 19 /* enough space to hold 2^128-1 */

@ -0,0 +1,31 @@
#include "fmt.h"
#include "byte.h"
#include <time.h>
static unsigned int fmt_2digits(char* dest,int i) {
dest[0]=(char)((i/10)+'0');
dest[1]=(char)((i%10)+'0');
return 2;
}
size_t fmt_iso8601(char* dest,time_t t) {
struct tm* x=gmtime(&t);
size_t i;
if (dest==0) return sizeof("2014-05-27T19:22:16Z")-1;
/* "2014-05-27T19:22:16Z" */
i=fmt_2digits(dest,(x->tm_year+1900)/100);
i+=fmt_2digits(dest+i,(x->tm_year+1900)%100);
dest[i++]='-';
i+=fmt_2digits(dest+i,x->tm_mon+1);
dest[i++]='-';
i+=fmt_2digits(dest+i,x->tm_mday);
dest[i++]='T';
i+=fmt_2digits(dest+i,x->tm_hour);
i+=fmt_str(dest+i,":");
i+=fmt_2digits(dest+i,x->tm_min);
i+=fmt_str(dest+i,":");
i+=fmt_2digits(dest+i,x->tm_sec);
i+=fmt_str(dest+i,"Z");
return i;
}

@ -0,0 +1,10 @@
#include <fmt.h>
#include <assert.h>
#include <unistd.h>
int main() {
char buf[1024];
write(1,buf,fmt_iso8601(buf,0));
write(1,"\n",1);
return 0;
}
Loading…
Cancel
Save