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