PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/timezone.d

http://github.com/wilkie/djehuty
D | 93 lines | 62 code | 15 blank | 16 comment | 4 complexity | 7c7ffdd1fcda8cbf1ff6634a23bc6f03 MD5 | raw file
  1. /*
  2. * timezone.d
  3. *
  4. * This module implements the TimeZone class. This class can depict difference
  5. * timezones and can be used to translate Time into a localized value.
  6. *
  7. * Author: Dave Wilkinson
  8. * Originated: January 1st, 2010
  9. *
  10. */
  11. module core.timezone;
  12. import Scaffold = scaffold.time;
  13. import core.definitions;
  14. import core.string;
  15. import io.console;
  16. class TimeZone {
  17. // Description: This will construct a TimeZone for the current location.
  18. this() {
  19. this(Scaffold.TimeZoneGet());
  20. }
  21. // Description: This will construct a specific TimeZone for a specific
  22. // timezone name or a string containing a custom timezone.
  23. // name: A full name, or "GMT+h:mm" or "GMT-h:mm" where h and mm
  24. // represent the hour and minutes from GMT respectively.
  25. this(string name) {
  26. _name = name;
  27. if (name[0..3] == "UTC") {
  28. _utc = name;
  29. string foo = name[3..$];
  30. int pos = foo.find(":");
  31. if (pos < 0) { pos = foo.length; }
  32. foo = foo[0..pos];
  33. int diff_hr;
  34. int diff_min;
  35. foo.nextInt(diff_hr);
  36. foo = name[3+pos+1..$];
  37. foo.nextInt(diff_min);
  38. _offset_in_minutes = (diff_hr * 60) + diff_min;
  39. }
  40. else {
  41. switch(name) {
  42. case "Eastern Standard Time":
  43. _offset_in_minutes = -(5 * 60);
  44. break;
  45. case "Pacific Standard Time":
  46. _offset_in_minutes = -(8 * 60);
  47. break;
  48. case "Coordinated Universal Time":
  49. case "Temps Universel Coordonné":
  50. case "Greenwich Mean Time":
  51. _offset_in_minutes = 0;
  52. break;
  53. default:
  54. break;
  55. }
  56. int diff_hr = _offset_in_minutes / 60;
  57. int diff_min = _offset_in_minutes % 60;
  58. _utc = "UTC" ~ toStr(diff_hr) ~ ":" ~ (diff_min < 10 ? "0" : "") ~ toStr(diff_min);
  59. }
  60. }
  61. string name() {
  62. return _name;
  63. }
  64. string utcName() {
  65. return _utc;
  66. }
  67. string toString() {
  68. return utcName;
  69. }
  70. // Description: This will return the offset from UTC in microseconds.
  71. long utcOffset() {
  72. return cast(long)_offset_in_minutes * 60L * 1000000L;
  73. }
  74. protected:
  75. string _name = "";
  76. string _utc = "";
  77. int _offset_in_minutes;
  78. }