utils.d 860 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. module vibe.http.internal.utils;
  2. import core.time : nsecs, seconds;
  3. import std.datetime : SysTime;
  4. import vibe.container.internal.appender : FixedAppender;
  5. import vibe.inet.message : writeRFC822DateTimeString;
  6. string formatRFC822DateAlloc(SysTime time)
  7. @safe {
  8. static LAST = CacheTime(SysTime.min());
  9. if (time > LAST.nextUpdate) {
  10. auto app = new FixedAppender!(string, 32);
  11. writeRFC822DateTimeString(app, time);
  12. LAST.update(time);
  13. LAST.cachedDate = () @trusted { return app.data; } ();
  14. return () @trusted { return app.data; } ();
  15. } else
  16. return LAST.cachedDate;
  17. }
  18. private struct CacheTime
  19. {
  20. string cachedDate;
  21. SysTime nextUpdate;
  22. this(SysTime nextUpdate) @safe @nogc pure nothrow
  23. {
  24. this.nextUpdate = nextUpdate;
  25. }
  26. void update(SysTime time) @safe
  27. {
  28. this.nextUpdate = time + 1.seconds;
  29. this.nextUpdate.fracSecs = nsecs(0);
  30. }
  31. }