Hello!
A problem is that I want to express a date before 1970-1-1 00:00:00.eg:)1960-11-11 10:10:10.
But I can just get the second from
At begining,I plane to do like this:
def sec = (
{DateTime zone = DateTimeZone.local,"1970-01-01 00:00:00.000000 +0000"}
-
{DateTime zone = DateTimeZone.local,"1900-01-01 00:00:00.000000 +0000"}
)
def time = {DateTime zone = DateTimeZone.local,"1900-01-01 00:00:00.000000 +0000"} + sec -288366590000 * 0.001s
But the IDE throws exception.the bizarrerie thing is I can get sec and the IDE does not throw exception.When I create DateTime of 1900-01-01 00:00:00.000000 +0000
The IDE throws exception.
Maybe the way I walk is not proper.So How to do ?
If you look up the documentation index entry for "date" , you find:
====
When using a UTC based time zone, you can represent virtually any date and time. The UTC calendar does not have daylight savings time and is simply a projection of the Gregorian calendar indefinitely forward and backward in time.
Local time zones are more restricted in the range of dates they can represent. For example, the default local timezone can represent dates in the range 1970-01-01 00:00:00.000000 +0000 to 2038-01-19 03:14:07.000000 +0000, and are only precise to around the nearest nanosecond.
Trying to specify a date outside of the limits of a time zone results in an error.
====
To use UTC, you can either specify the zone or use the +0000 in the string.
{DateTime
zone = DateTimeZone.utc,
"1900-01-01 00:00:00.000000"
}
Also note that you can also define a UTC-based timezone like this:
{DateTimeZone mode = "utc", utc-offset-minutes = 120}
You can compute the utc-offset-minutes for a given time zone by constructing a time using the local timezone and querying its info for the offset:
{DateTime}.info.utc-offset-minutes
If you are in a timezone that need to account for seasonal differences (e.g. "daylight savings time" in the US), then the choice of UTC-based timezone will depend on the time of year. Since the exact dates on which daylight savings time goes into affect have changed over the years, it is best to use local time for dates between 1970-now since the OS provides the necessary information. For dates before that, you have to provide your own heuristics.
If you only need to express the date, and not the time, for dates earlier than 1970, then any UTC timezone will do, and when constructing from individual year, month, day values you should use the DateTime.date constructor.
In my computer,when the timezone is set to Beijing TimeZone,eg:11:00:00 will be the value 03:00:00 of (GMT) Casablanca, Monrovia Zone
Now I can use {DateTime.date ...} to express the date before 1970-1-1 00:00:00,but when I change the computer Timezone,the value of object
of being created by {DateTime.date ...} will not changed.
So,how to create a DateTime which can express before 1970 and changes its value according to the TimeZone of the computer?
Changing the time zone on your computer does change the meaning of DateTimeZone.local. It does not change the underlying DateTimeData value used to represent the actual date and time, since that is always stored in UTC time; it only determines how to convert the DateTimeData to the DateTimeInfo, which breaks the time down into minutes and seconds and so on.
The native OS's Curl supports only provide timezone information back to 1970 (Windows might do better than that but we are reluctant to introduce many OS-specific behavior in our runtime libraries) so before that we only provide UTC offset timezones.
So if you really need to represent a time before 1970, you should initialize it using a UTC timezone. Then you should create a function to produce the UTC version of the local timezone as I outlined earlier, and use that function to set the timezone of the DateTime whenever you think the computer's timezone might have changed (unfortunately, Curl does not provide an event to to tell you when this happens).
Here is how I think I might write the timezone conversion function:
{define-proc public {local-timezone-for-date date:DateTime}:DateTimeZone
|| Find year within 1970-2038 that is closest to 'date' since that is mostly likely
|| to have similar timezone characteristics.
let reference-year:int
{if date.info.year <1970 then
set reference-year = 1972 || first leap-year after 1970
elseif date.info.year >= 2028 then
set reference-year = 2036 || last leap year before 2038
else
{return DateTimeZone.local}
}
def reference-date =
{DateTime
year = reference-year,
month = date.info.month,
day = date.info.day
}
{return
{DateTimeZone
mode = "utc",
utc-offset-minutes = reference-date.info.utc-offset-minutes
}
}
}
