www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Time and space, physical values?

Greetings!

I have this complicated piece of code:

import std.stdio;
import std.date;
d_time CalculateGMT(d_time t)
{
char[] d = std.date.toString(t);
// d should give something like: Thu Feb 24 16:14:30 GMT-0500 2005
char [][] e = std.string.split(d," ");
d = e[0] ~ " " ~ e[1] ~ " " ~ e[2] ~ ", " ~ e[5] ~ " " ~ e[3];
writefln(d);
bit add = false;
if (std.string.find(e[4],"-") > 0)
{
add = true;
e = std.string.split(e[4],"-");
}
else
{
e = std.string.split(e[4],"+");
}
int hh = std.string.atoi(e[1][0..2]);
int mm = std.string.atoi(e[1][2..4]);
int    h = 60;
int    m = 60;
int    s = 1000;
if (add)
{
t += hh * h * m * s; // Adding hh hours to GMT time
t += mm * m * s; // Adding mm minutes to GMT time
}
else
{
t -= hh * h * m * s; // Subtracting hh hours to GMT time
t -= mm * m * s; // Subtracting mm minutes to GMT time
}
return t;
}
void main()
{
d_time t;
t = parse("09-Feb-04 01:00:18");
writefln(t);
writefln(std.date.toString(CalculateGMT(t)));
writefln();
t = parse("09-Feb-2004 01:00:18");
writefln(t);
writefln(std.date.toString(CalculateGMT(t)));
writefln();
t = UTCtoLocalTime(getUTCtime);
writefln(t);
writefln(std.date.toString(CalculateGMT(t)));
}
if you compile it, and run it, you should get,

-2079453582000
Tue Feb 09, 1904 01:00:18
Tue Feb 09 06:00:18 GMT-0500 1904

1076306418000
Mon Feb 09, 2004 01:00:18
Mon Feb 09 06:00:18 GMT-0500 2004

1112640955056
Mon Apr 04, 2005 14:55:55
Mon Apr 04 18:55:55 GMT-0400 2005

which provokes me to ask why the parsing is turned to 1900s.  Yes, I understand
that "d_time Is a signed arithmetic type giving the time elapsed since January
1, 1970. Negative values are for dates preceding 1970. The time unit used is
Ticks. Ticks are milliseconds or smaller intervals."  Nonetheless, 05 (the
actual year) is much closer to 04 then 70.  So, a few questions come up:

1.  Shouldn't the parsing look at the actual date and make some smart decisions
as to whether a YY should have a 19YY or a 20YY?

2.  I don't want to touch the string, but instead I would like to add some
integer value to t so that it turns into 2000.  What is the calculation of this?
I have been trying to play with this, but I just don't have the time.

3.  Why don't we have a function that will not be GMT and would just return the
date in this form:

Mon Feb 09, 2004 06:00:18?  (yes, I know I can write the function. :-) )  It's
just easier to be able to have these GMT functions plus a "normal human
time/date" function also.

Any help would be greatly appreciate it.  And yes, I know the second date is
wrong, since it already has the GMT time calculation done.  ;-)  I just split it
and use the other values.

thanks,

josé
Apr 04 2005