1 module hunt.http.codec.http.model.DateGenerator; 2 3 import hunt.Exceptions; 4 import hunt.text.Common; 5 import hunt.util.StringBuilder; 6 7 import std.datetime; 8 import std.format; 9 import std.array; 10 11 /** 12 * ThreadLocal Date formatters for HTTP style dates. 13 */ 14 class DateGenerator { 15 // private static TimeZone __GMT = TimeZone.getTimeZone("GMT"); 16 17 // shared static this() { 18 // __GMT.setID("GMT"); 19 // } 20 21 __gshared string[] DAYS = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]; 22 __gshared string[] MONTHS = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 23 24 private static DateGenerator __dateGenerator; 25 static this() 26 { 27 __dateGenerator = new DateGenerator(); 28 } 29 30 31 // static string __01Jan1970 = DateGenerator.formatDate(0); 32 33 /** 34 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" 35 * 36 * @param date 37 * the date 38 * @return the formatted date 39 */ 40 static string formatDate(SysTime date) { 41 return __dateGenerator.doFormatDate(date); 42 } 43 44 45 // static string formatDate(long date) { 46 // return __dateGenerator.doFormatDate(date); 47 // } 48 49 /** 50 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies 51 * 52 * @param buf 53 * the buffer to put the formatted date into 54 * @param date 55 * the date in milliseconds 56 */ 57 static void formatCookieDate(StringBuilder buf, long date) { 58 __dateGenerator.doFormatCookieDate(buf, date); 59 } 60 61 /** 62 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies 63 * 64 * @param date 65 * the date in milliseconds 66 * @return the formatted date 67 */ 68 static string formatCookieDate(long date) { 69 StringBuilder buf = new StringBuilder(28); 70 formatCookieDate(buf, date); 71 return buf.toString(); 72 } 73 74 75 string doFormatDate(SysTime date) 76 { 77 Appender!string buf; 78 79 DateTime dt = cast(DateTime)date; 80 81 DayOfWeek day_of_week = dt.dayOfWeek; 82 int day_of_month = dt.day; 83 Month month = dt.month; 84 int year = dt.year; 85 int century = year / 100; 86 year = year % 100; 87 88 int hours = dt.hour; 89 int minutes = dt.minute; 90 int seconds = dt.second; 91 92 buf.put(DAYS[day_of_week]); 93 buf.put(','); 94 buf.put(' '); 95 buf.put(format("%02d", day_of_month)); 96 97 buf.put(' '); 98 buf.put(MONTHS[month - Month.jan]); 99 buf.put(' '); 100 buf.put(format("%02d", century)); 101 buf.put(format("%02d", year)); 102 103 buf.put(' '); 104 buf.put(format("%02d", hours)); 105 buf.put(':'); 106 buf.put(format("%02d", minutes)); 107 buf.put(':'); 108 buf.put(format("%02d", seconds)); 109 buf.put(" GMT"); 110 return buf.data; 111 } 112 113 // private StringBuilder buf = new StringBuilder(32); 114 // private GregorianCalendar gc = new GregorianCalendar(__GMT); 115 116 /** 117 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" 118 * 119 * @param date 120 * the date in milliseconds 121 * @return the formatted date 122 */ 123 string doFormatDate(long date) { 124 implementationMissing(); 125 return ""; 126 } 127 128 /** 129 * Format "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies 130 * 131 * @param buf 132 * the buffer to format the date into 133 * @param date 134 * the date in milliseconds 135 */ 136 void doFormatCookieDate(StringBuilder buf, long date) { 137 implementationMissing(); 138 // gc.setTimeInMillis(date); 139 140 // int day_of_week = gc.get(Calendar.DAY_OF_WEEK); 141 // int day_of_month = gc.get(Calendar.DAY_OF_MONTH); 142 // int month = gc.get(Calendar.MONTH); 143 // int year = gc.get(Calendar.YEAR); 144 // year = year % 10000; 145 146 // int epoch = cast(int) ((date / 1000) % (60 * 60 * 24)); 147 // int seconds = epoch % 60; 148 // epoch = epoch / 60; 149 // int minutes = epoch % 60; 150 // int hours = epoch / 60; 151 152 // buf.append(DAYS[day_of_week]); 153 // buf.append(','); 154 // buf.append(' '); 155 // StringUtils.append2digits(buf, day_of_month); 156 157 // buf.append('-'); 158 // buf.append(MONTHS[month]); 159 // buf.append('-'); 160 // StringUtils.append2digits(buf, year / 100); 161 // StringUtils.append2digits(buf, year % 100); 162 163 // buf.append(' '); 164 // StringUtils.append2digits(buf, hours); 165 // buf.append(':'); 166 // StringUtils.append2digits(buf, minutes); 167 // buf.append(':'); 168 // StringUtils.append2digits(buf, seconds); 169 // buf.append(" GMT"); 170 } 171 }