add fmt_human, fmt_humank and fmt_httpdate
parent
a3b08eb5e5
commit
40bba8865b
@ -0,0 +1,17 @@
|
||||
.TH fmt_httpdate 3
|
||||
.SH NAME
|
||||
fmt_httpdate \- write a date in ASCII as in the HTTP protocol
|
||||
.SH SYNTAX
|
||||
.B #include <fmt.h>
|
||||
|
||||
unsigned int \fBfmt_httpdate\fP(char *\fIdest\fR,time_t \fIsource\fR);
|
||||
.SH DESCRIPTION
|
||||
fmt_httpdate writes a date in ASCII representation as the HTTP protocol defines it:
|
||||
"Sun, 06 Nov 1994 08:49:37 GMT".
|
||||
|
||||
fmt_httpdate does not append \\0.
|
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_httpdate returns the number
|
||||
of bytes it would have written.
|
||||
|
||||
The output of fmt_httpdate is 29 until Jan 1st 10000.
|
@ -0,0 +1,35 @@
|
||||
#include "fmt.h"
|
||||
#include "byte.h"
|
||||
#include <time.h>
|
||||
|
||||
static unsigned int fmt_2digits(char* dest,int i) {
|
||||
dest[0]=(i/10)+'0';
|
||||
dest[1]=(i%10)+'0';
|
||||
return 2;
|
||||
}
|
||||
|
||||
unsigned int fmt_httpdate(char* dest,time_t t) {
|
||||
static const char days[] = "SunMonTueWedThuFriSat";
|
||||
static const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
||||
struct tm* x=gmtime(&t);
|
||||
int i;
|
||||
|
||||
if (dest==0) return 29;
|
||||
/* "Sun, 06 Nov 1994 08:49:37 GMT" */
|
||||
byte_copy(dest,3,days+3*x->tm_wday); i=3;
|
||||
i+=fmt_str(dest+i,", ");
|
||||
i+=fmt_2digits(dest+i,x->tm_mday);
|
||||
i+=fmt_str(dest+i," ");
|
||||
byte_copy(dest+i,3,months+3*x->tm_mon); i+=3;
|
||||
i+=fmt_str(dest+i," ");
|
||||
i+=fmt_2digits(dest+i,(x->tm_year+1900)/100);
|
||||
i+=fmt_2digits(dest+i,(x->tm_year+1900)%100);
|
||||
i+=fmt_str(dest+i," ");
|
||||
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," GMT");
|
||||
return i;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
.TH fmt_human 3
|
||||
.SH NAME
|
||||
fmt_human \- write a human readable ASCII representation of a long integer
|
||||
.SH SYNTAX
|
||||
.B #include <fmt.h>
|
||||
|
||||
unsigned int \fBfmt_human\fP(char *\fIdest\fR,unsigned long long \fIsource\fR);
|
||||
.SH DESCRIPTION
|
||||
fmt_human writes a human readable ASCII representation of \fIsource\fR
|
||||
to \fIdest\fR and returns the number of bytes written. The result
|
||||
resembles the file size output of "ls -H"; 1000 becomes "1.0k", 1000000
|
||||
becomes "1.0M" and so on for "G" and "T".
|
||||
|
||||
fmt_human does not append \\0.
|
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_human returns the number
|
||||
of bytes it would have written.
|
||||
|
||||
The output of fmt_human can not exceed 11 (assuming unsigned long long
|
||||
has 64 bits).
|
||||
.SH "SEE ALSO"
|
||||
fmt_humank(3), scan_human(3)
|
@ -0,0 +1,22 @@
|
||||
.TH fmt_humank 3
|
||||
.SH NAME
|
||||
fmt_humank \- write a human readable ASCII representation of a long integer
|
||||
.SH SYNTAX
|
||||
.B #include <fmt.h>
|
||||
|
||||
unsigned int \fBfmt_humank\fP(char *\fIdest\fR,unsigned long long \fIsource\fR);
|
||||
.SH DESCRIPTION
|
||||
fmt_humank writes a human readable ASCII representation of \fIsource\fR
|
||||
to \fIdest\fR and returns the number of bytes written. The result
|
||||
resembles the file size output of "ls -h"; 1024 becomes "1.0k", 1048576
|
||||
becomes "1.0M" and so on for "G" and "T".
|
||||
|
||||
fmt_humank does not append \\0.
|
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_humank returns the number
|
||||
of bytes it would have written.
|
||||
|
||||
The output of fmt_humank can not exceed 11 (assuming unsigned long long
|
||||
has 64 bits).
|
||||
.SH "SEE ALSO"
|
||||
fmt_human(3), scan_humank(3)
|
@ -0,0 +1,8 @@
|
||||
#include <time.h>
|
||||
#include <buffer.h>
|
||||
|
||||
main() {
|
||||
char buf[100];
|
||||
buffer_put(buffer_1,buf,fmt_httpdate(buf,time(0)));
|
||||
buffer_putnlflush(buffer_1);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#include <fmt.h>
|
||||
#include <buffer.h>
|
||||
|
||||
main() {
|
||||
char buf[1024];
|
||||
buffer_put(buffer_1,buf,fmt_human(buf,1)); buffer_putnlflush(buffer_1);
|
||||
buffer_put(buffer_1,buf,fmt_human(buf,1400)); buffer_putnlflush(buffer_1);
|
||||
buffer_put(buffer_1,buf,fmt_human(buf,2300000)); buffer_putnlflush(buffer_1);
|
||||
buffer_put(buffer_1,buf,fmt_human(buf,(unsigned long long)(-1ll))); buffer_putnlflush(buffer_1);
|
||||
}
|
Loading…
Reference in New Issue