Last update August 20, 2008

Doc Comments / Phobos /
Std Date



Difference (last change) (no other diffs, normal page display)

Added: 33a34,165
=== Date formating =
Since date/time formating appears to be missing from Date, - this code does most of that for you
[[code]


import std.date;
/**
* examples:
* (new Fdate()).format("%D, %d %M %Y %H:%i:%s %O")
* Fri, 04 Jul 2008 14:26:09 +0800
*
*
*/


class Fdate
{
d_time t;
this()
{
t = getUTCtime();
}
this(d_time t)
{
this.t = t;
}
char[] format(char[] str) {
char[] ret = "";
for(int i =0; i < str.length; i++) {
if (str[i] != '%') {
ret ~= str[i];
continue;
}
// should assert()
i++;
char[] x;
int y;
char[] pad ="0000";
switch( str[i]) {
case 'Y': // year 4 digits.
x = std.string.toString(YearFromTime?(t));
ret ~= x.length < 4 ? (pad[0..(4-x.length)] ~ x) : x;
continue;


case 'F' : // month full
throw new Exception("no yet");
case 'M' : // month[0..3]
y = MonthFromTime?(t);
ret ~= monstr[(y * 3) .. (y * 3)+3];
continue;
case 'm' : // month number (2digit)
x = std.string.toString(MonthFromTime?(t)+1);
ret ~= x.length < 2 ? "0" ~ x : x;
continue;
case 'n' : // month not padded.
ret ~= std.string.toString(MonthFromTime?(t)+1);
continue;

case 'd': // 10 2 digit day of month
x = std.string.toString(DateFromTime?(t));
ret ~= x.length < 2 ? "0" ~ x : x;
continue;
case 'j': // day of month (number)
ret ~= std.string.toString(DateFromTime?(t));
continue;


case 'D' : // day of weeek[0..3]
y = WeekDay?(t);
ret ~= daystr[(y * 3) .. (y * 3)+3];
continue;
case 'l' : // day name of week.
throw new Exception("no yet");

case 'G': //24-hour format of an hour without leading zeros;
ret ~= std.string.toString(HourFromTime?(t));
continue;
case 'h': //03 12-hour format of an hour with leading zeros
throw new Exception("no yet");
case 'H': //15 24-hour format of an hour with leading zeros
x = std.string.toString(HourFromTime?(t));
ret ~= x.length < 2 ? "0" ~ x : x;
continue;
case 'i': //05 Minutes with leading zeros
x = std.string.toString(MinFromTime?(t));
ret ~= x.length < 2 ? "0" ~ x : x;
continue;
case 's': //01 Seconds, with leading zeros
x ~= std.string.toString(SecFromTime?(t));
ret ~= x.length < 2 ? "0" ~ x : x;
continue;
case 'O': //-0600 Difference to Greenwich time (GMT) in hours
char[] buffer = new char[17 + 1];


d_time dst = DaylightSavingTA?(t);
d_time offset = LocalTZA? + dst;
ret ~= (offset < 0) ? '-' : '+';
uint mn = cast(int)(offset / msPerMinute);
uint hr = mn / 60;
mn %= 60;
x = std.string.toString(hr);
ret ~= x.length < 2 ? "0" ~ x: x;
x = std.string.toString(mn);
ret ~= x.length < 2 ? "0" ~ x: x;
continue;
case '%':
ret ~= "%";
continue;

default:
throw new Exception("no yet");


} // edn case


// next char..

}

return ret;

}
char[] formatISO() {
return format("%Y-%m-%d %H:%i:%s %O");
}


}
]

std.date

Comments

Add your comments here...

toString(long time)

The current documentation omits some vital information.

The input d_time value is assumed by the function to be a UTC date-time. It is first converted to the local date-time by this function and then converted to a string.

toTimeString

The input to toTimeString() is assumed to be UTC, and it outputs a Local time string. ( NG:digitalmars.D.learn/3229).

Convert long to Date

I didn't see a built-in function to convert long to Date struct. Did I miss anything here?

C# style TimeSpan

It would be sweet to include a C# style TimeSpan class/struct in std library.
   /************** C# Example **************/
   DateTime date1 = DateTime.Now;
   DateTime date2 = DateTime.Now;
   TimeSpan span = date1 - date2;
   Console.WriteLine(span);
   /********** End of C# Example ***********/

Date formating

Since date/time formating appears to be missing from Date, - this code does most of that for you
 
import std.date;
/**
 * examples:
 * (new Fdate()).format("%D, %d %M %Y %H:%i:%s %O")
 * Fri, 04 Jul 2008 14:26:09 +0800
 * 
 * 
 */


class Fdate
{
	d_time t;
	this()
	{
		t = getUTCtime();
	}
	this(d_time t)
	{
		this.t = t;
	}
	char[] format(char[] str) {
		char[] ret =  "";
		for(int i =0; i < str.length; i++) {
			if (str[i] != '%') {
				ret ~= str[i];
				continue;
			}
			// should assert()
			i++;
			char[] x;
			int y;
			char[] pad ="0000";
			switch( str[i]) {
				case 'Y': // year 4 digits.
					x = std.string.toString(YearFromTime(t)); 
					ret ~= x.length < 4 ? (pad[0..(4-x.length)] ~ x) : x;
					continue;
					
					
				case 'F' : // month full
					throw new Exception("no yet");
				case 'M' : // month[0..3]
					y = MonthFromTime(t);
					ret ~= monstr[(y * 3) .. (y * 3)+3];
					continue;
				case 'm' : // month number (2digit)
					x = std.string.toString(MonthFromTime(t)+1); 
					ret ~= x.length < 2 ? "0" ~ x : x;
					continue;
				case 'n' : // month not padded.
					ret ~= std.string.toString(MonthFromTime(t)+1); 
					continue;
					
				case 'd': // 10   2 digit day of month
					x = std.string.toString(DateFromTime(t)); 
					ret ~= x.length < 2 ? "0" ~ x : x;
					continue;
				case 'j': //  day of month (number)
					ret ~= std.string.toString(DateFromTime(t)); 
					continue;
					
					
				case 'D' : // day of weeek[0..3]
					y = WeekDay(t);
					ret ~= daystr[(y * 3) .. (y * 3)+3];
					continue;
				case 'l' : // day name of week.
					throw new Exception("no yet");
				
				case 'G':      //24-hour format of an hour without leading zeros;
					ret ~= std.string.toString(HourFromTime(t));
					continue;
				case 'h':      //03         12-hour format of an hour with leading zeros
					throw new Exception("no yet");
				case 'H':      //15         24-hour format of an hour with leading zeros
					x = std.string.toString(HourFromTime(t));
					ret ~= x.length < 2 ? "0" ~ x : x;
					continue;
				case 'i':      //05         Minutes with leading zeros
					x = std.string.toString(MinFromTime(t));
					ret ~= x.length < 2 ? "0" ~ x : x;
					continue;
				case 's':      //01         Seconds, with leading zeros
					x ~= std.string.toString(SecFromTime(t));
					ret ~= x.length < 2 ? "0" ~ x : x;
					continue;
				case 'O':      //-0600      Difference to Greenwich time (GMT) in hours
					char[] buffer = new char[17 + 1];

					  
					d_time dst = DaylightSavingTA(t);
					d_time offset = LocalTZA + dst;
					ret ~=  (offset < 0) ? '-' : '+';
					uint mn = cast(int)(offset / msPerMinute);
					uint hr = mn / 60;
					mn %= 60;
					x = std.string.toString(hr);
					ret ~= x.length < 2 ? "0" ~ x: x;
					x = std.string.toString(mn);
					ret ~= x.length < 2 ? "0" ~ x: x;
					continue;
				case '%': 
					ret ~= "%";
					continue;
					
				default:
					throw new Exception("no yet");

				
			} // edn case
			
			
			// next char..
			
		}
		
		return ret;
		
	}
	char[] formatISO() {
		return  format("%Y-%m-%d %H:%i:%s %O");
	}
	
	
}

Links


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: August 20, 2008 8:42 (diff))