UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdlib.h> 30 #include "pool.h" 31 32 #include "date.h" 33 34 char *date_wkday[] = { 35 "Sun", 36 "Mon", 37 "Tue", 38 "Wed", 39 "Thu", 40 "Fri", 41 "Sat" 42 }; 43 44 char *date_month[] = { 45 "Jan", 46 "Feb", 47 "Mar", 48 "Apr", 49 "May", 50 "Jun", 51 "Jul", 52 "Aug", 53 "Sep", 54 "Oct", 55 "Nov", 56 "Dec" 57 }; 58 59 sstr_t date_format_http(time_t t, pool_handle_t *pool) { 60 sstr_t d; 61 d.ptr = NULL; 62 d.length = 0; 63 64 struct tm date; 65 gmtime_r(&t, &date); 66 67 char *str = pool_malloc(pool, 32); 68 snprintf( 69 str, 70 32, 71 "%s, %02d %s %d %02d:%02d:%02d GMT", 72 date_wkday[date.tm_wday], 73 date.tm_mday, 74 date_month[date.tm_mon], 75 1900 + date.tm_year, 76 date.tm_hour, 77 date.tm_min, 78 date.tm_sec); 79 80 d.length = 29; 81 d.ptr = str; 82 83 return d; 84 } 85 86 sstr_t date_format_iso8601(time_t t, pool_handle_t *pool) { 87 sstr_t d; 88 d.ptr = NULL; 89 d.length = 0; 90 91 struct tm date; 92 gmtime_r(&t, &date); 93 94 char *str = pool_malloc(pool, 24); 95 snprintf( 96 str, 97 24, 98 "%d-%02d-%02dT%02d:%02d:%02dZ", 99 1900 + date.tm_year, 100 date.tm_mon + 1, 101 date.tm_mday, 102 date.tm_hour, 103 date.tm_min, 104 date.tm_sec); 105 106 d.length = 20; 107 d.ptr = str; 108 109 return d; 110 } 111