Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

DateTime.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file DateTime.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version, April 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.format.DateTime;
00040 
00041 private import  std.date;
00042 
00043 private import  mango.format.Int,
00044                 mango.format.Formatter;
00045 
00046 /******************************************************************************
00047 
00048 ******************************************************************************/
00049 
00050 class DateTime : Formatter
00051 {
00052         /**********************************************************************
00053 
00054         **********************************************************************/
00055 
00056         final static tChar[] format (long i)
00057         {
00058                 return format (new tChar[40], i);
00059         }
00060 
00061         /**********************************************************************
00062 
00063                 Shamelessly plundered from std.date (with permission), this 
00064                 version avoids allocating memory.
00065 
00066                 Returns a populated slice of the provided buffer; with zero
00067                 length if the date was invalid.
00068 
00069                 Note that this is just a stopgap implementation for now.
00070 
00071         **********************************************************************/
00072 
00073         final static tChar[] format (tChar[] buffer, long time)
00074         {
00075                 // "Tue Apr 02 02:04:57 GMT-0800 1996"
00076                 assert(buffer.length >= 29 + 7 + 1);
00077 
00078                 d_time  t,
00079                         dst,
00080                         offset;
00081                 tChar   sign;
00082                 tChar*  p = buffer;
00083 
00084                 void str (tChar* s, tChar c)
00085                 {
00086                         p[0..3] = s[0..3];
00087                         p += 3;
00088                         *p++ = c;
00089                 }
00090 
00091                 void num (int i, tChar c=0)
00092                 {
00093                         p += Int.format (p[0..2], i, Int.Radix.Decimal, '0').length;
00094                         if (c)
00095                             *p++ = c;
00096                 }
00097                 
00098 
00099                 if (time == d_time_nan)
00100                     return buffer[0..0];
00101 
00102                 dst = DaylightSavingTA (time);
00103                 offset = LocalTZA + dst;
00104                 t = time + offset;
00105 
00106                 str (&daystr[WeekDay(t) * 3], ' ');
00107                 str (&monstr[MonthFromTime(t) * 3], ' ');
00108                 num (DateFromTime(t), ' ');
00109                 num (HourFromTime(t), ':');
00110                 num (MinFromTime(t), ':');
00111                 num (SecFromTime(t), ' ');
00112 
00113                 sign = '+';
00114                 if (offset < 0)
00115                    {
00116                    sign = '-';
00117                    offset = -(LocalTZA + dst);
00118                    }
00119                 str ("GMT", sign);
00120 
00121                 int mn = cast(int)(offset / msPerMinute);
00122                 num (mn / 60);
00123                 num (mn % 60, ' ');
00124 
00125                 Int.format (p[0..4], YearFromTime(t));
00126                 p += 4;
00127         
00128                 return buffer[0..p-buffer.ptr];
00129         }
00130 
00131         /**********************************************************************
00132 
00133                 Convert a string to a date. This is just a symmetrical 
00134                 wrapper around the Phobos functionality.
00135 
00136                 Note that this is just a stopgap implementation for now.
00137               
00138         **********************************************************************/
00139 
00140         final static long parse (tChar[] date, uint* ate=null)
00141         in {
00142            assert (ate == null);  // for now ...
00143            }
00144         body
00145         {
00146                 return std.date.parse (date);
00147         }
00148 }
00149 
00150 
00151 
00152 
00153 
00154 /+
00155                 len = sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d",
00156                                &daystr[WeekDay(t) * 3],
00157                                &monstr[MonthFromTime(t) * 3],
00158                                DateFromTime(t),
00159                                cast(int)HourFromTime(t), cast(int)MinFromTime(t), 
00160                                cast(int)SecFromTime(t), sign, hr, mn,
00161                                cast(long)YearFromTime(t));
00162 +/

Generated on Sun Mar 6 00:30:56 2005 for Mango by doxygen 1.3.6