changeset 776:580d4ca9f1ff

Added new module dil.Time.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 19 Feb 2008 19:30:04 +0100
parents e76c9fdb83a3
children 9f61e8af55d5
files trunk/src/cmd/DDoc.d trunk/src/dil/Time.d trunk/src/dil/lexer/Lexer.d
diffstat 3 files changed, 56 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/cmd/DDoc.d	Mon Feb 18 22:59:56 2008 +0100
+++ b/trunk/src/cmd/DDoc.d	Tue Feb 19 19:30:04 2008 +0100
@@ -25,10 +25,9 @@
 import dil.Converter;
 import dil.SourceText;
 import dil.Enums;
+import dil.Time;
 import common;
 
-import tango.stdc.time : time_t, time, ctime;
-import tango.stdc.string : strlen;
 import tango.text.Ascii : toUpper;
 import tango.io.File;
 import tango.io.FilePath;
@@ -88,15 +87,13 @@
   // Create a macro environment for this module.
   mtable = new MacroTable(mtable);
   // Define runtime macros.
+  mtable.insert("MODPATH", mod.getFQNPath() ~ mod.fileExtension());
+
   mtable.insert("TITLE", mod.getFQN());
   mtable.insert("DOCFILENAME", mod.getFQN());
-
-  time_t time_val;
-  time(&time_val);
-  char* str = ctime(&time_val);
-  char[] time_str = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
-  mtable.insert("DATETIME", time_str.dup);
-  mtable.insert("YEAR", time_str[20..24].dup);
+  auto timeStr = Time.toString();
+  mtable.insert("DATETIME", timeStr);
+  mtable.insert("YEAR", timeStr[20..24]);
 
   auto doc = new DDocEmitter(mtable, incUndoc, mod, tokenHL);
   doc.emit();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/dil/Time.d	Tue Feb 19 19:30:04 2008 +0100
@@ -0,0 +1,36 @@
+/++
+  Author: Aziz Köksal
+  License: GPL3
++/
+module dil.Time;
+
+import tango.stdc.time : time_t, time, ctime;
+import tango.stdc.string : strlen;
+
+struct Time
+{
+static:
+  char[] toString()
+  {
+    time_t time_val;
+    .time(&time_val);
+    char* str = ctime(&time_val); // ctime returns a pointer to a static array.
+    char[] timeStr = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
+    return timeStr.dup;
+  }
+
+  char[] time(char[] timeStr)
+  {
+    return timeStr[11..19];
+  }
+
+  char[] month_day(char[] timeStr)
+  {
+    return timeStr[4..10];
+  }
+
+  char[] year(char[] timeStr)
+  {
+    return timeStr[20..24];
+  }
+}
--- a/trunk/src/dil/lexer/Lexer.d	Mon Feb 18 22:59:56 2008 +0100
+++ b/trunk/src/dil/lexer/Lexer.d	Tue Feb 19 19:30:04 2008 +0100
@@ -14,19 +14,16 @@
 import dil.CompilerInfo;
 import dil.Unicode;
 import dil.SourceText;
+import dil.Time;
 import common;
 
 import tango.stdc.stdlib : strtof, strtod, strtold;
 import tango.stdc.errno : errno, ERANGE;
-import tango.stdc.time : time_t, time, ctime;
-import tango.stdc.string : strlen;
 
 public import dil.lexer.Funcs;
 
-/++
-  The Lexer analyzes the characters of a source text and
-  produces a doubly-linked list of tokens.
-+/
+/// The Lexer analyzes the characters of a source text and
+/// produces a doubly-linked list of tokens.
 class Lexer
 {
   SourceText srcText; /// The source text.
@@ -48,12 +45,10 @@
   uint inTokenString; /// > 0 if inside q{ }
   NewlineData.FilePaths* filePaths;
 
-  /++
-    Construct a Lexer object.
-    Params:
-      srcText = the UTF-8 source code.
-      infoMan = used for collecting error messages.
-  +/
+  /// Construct a Lexer object.
+  /// Params:
+  ///   srcText = the UTF-8 source code.
+  ///   infoMan = used for collecting error messages.
   this(SourceText srcText, InfoManager infoMan = null)
   {
     this.srcText = srcText;
@@ -103,10 +98,8 @@
     return srcText.data;
   }
 
-  /++
-    The "shebang" may optionally appear once at the beginning of a file.
-    Regexp: #![^\EndOfLine]*
-  +/
+  /// The "shebang" may optionally appear once at the beginning of a file.
+  /// Regexp: #![^\EndOfLine]*
   void scanShebang()
   {
     if (*p == '#' && p[1] == '!')
@@ -138,20 +131,18 @@
     case TOK.DATE,
          TOK.TIME,
          TOK.TIMESTAMP:
-      time_t time_val;
-      time(&time_val);
-      char* str = ctime(&time_val);
-      char[] time_str = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
+      auto time_str = Time.toString();
       switch (t.kind)
       {
       case TOK.DATE:
-        time_str = time_str[4..11] ~ time_str[20..24] ~ \0; break;
+        time_str = Time.month_day(time_str) ~ ' ' ~ Time.year(time_str); break;
       case TOK.TIME:
-        time_str = time_str[11..19] ~ \0; break;
+        time_str = Time.time(time_str); break;
       case TOK.TIMESTAMP:
-        time_str = time_str[0..24] ~ \0; break;
+        break; // time_str is the timestamp.
       default: assert(0);
       }
+      time_str ~= '\0'; // Terminate with a zero.
       t.str = time_str;
       break;
     case TOK.VENDOR: