1 module hunt.http.Util; 2 3 import hunt.concurrency.Executors; 4 import hunt.concurrency.ExecutorService; 5 import hunt.concurrency.Scheduler; 6 import hunt.concurrency.ScheduledThreadPoolExecutor; 7 8 import std.concurrency : initOnce; 9 10 import std.array; 11 import std.conv; 12 import std.datetime; 13 import std.string; 14 15 /** 16 * 17 */ 18 struct CommonUtil { 19 20 static ScheduledThreadPoolExecutor scheduler() { 21 return initOnce!_scheduler(cast(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(5)); 22 } 23 private __gshared ScheduledThreadPoolExecutor _scheduler; 24 25 static void stopScheduler() { 26 if (_scheduler !is null) { 27 _scheduler.shutdown(); 28 } 29 } 30 31 32 /// convert time to RFC822 format string 33 static string toRFC822DateTimeString(SysTime systime) { 34 Appender!string ret; 35 36 DateTime dt = cast(DateTime)systime; 37 Date date = dt.date; 38 39 ret.put(to!string(date.dayOfWeek).capitalize); 40 ret.put(", "); 41 ret.put(rightJustify(to!string(date.day), 2, '0')); 42 ret.put(" "); 43 ret.put(to!string(date.month).capitalize); 44 ret.put(" "); 45 ret.put(to!string(date.year)); 46 ret.put(" "); 47 48 TimeOfDay time = cast(TimeOfDay)systime; 49 int tz_offset = cast(int)systime.utcOffset.total!"minutes"; 50 51 ret.put(rightJustify(to!string(time.hour), 2, '0')); 52 ret.put(":"); 53 ret.put(rightJustify(to!string(time.minute), 2, '0')); 54 ret.put(":"); 55 ret.put(rightJustify(to!string(time.second), 2, '0')); 56 57 if (tz_offset == 0) 58 { 59 ret.put(" GMT"); 60 } 61 else 62 { 63 ret.put(" " ~ (tz_offset >= 0 ? "+" : "-")); 64 65 if (tz_offset < 0) tz_offset = -tz_offset; 66 ret.put(rightJustify(to!string(tz_offset / 60), 2, '0')); 67 ret.put(rightJustify(to!string(tz_offset % 60), 2, '0')); 68 } 69 70 return ret.data; 71 } 72 }