question

Jeremy R avatar image
1 Like"
Jeremy R asked Jordan Johnson commented

Convert date before 1970 to DateTime

Normally when I construct a DateTime, I use something like the following.

string dateString = "1/01/1970";
double dateNum = convert(dateString, DATE_STR, FS_DATETIME);
DateTime dateObject = DateTime(dateNum);

However, when I do this with a date prior to 1/01/1970, I get the following exception.

exception: Invalid convert command: _mkgmtime64 failure

Then if my subsequent code tries to use the DateTime object, FlexSim immediately crashes.

According to the command help description of convert, FS_DATETIME is "the number of seconds since Jan 1, 1601", so I should be able to use any date from 1601 onward. Yet the command seems to be bounded by the (very inconvenient) Unix time limit of 1/01/1970. The DateTime object itself supports dates before 1970 (I've been able to produce them by subtracting from a date), but convert doesn't appear to.

How can I easily create a DateTime object for dates prior to 1970?

datetimedatedate conversion
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Jordan Johnson avatar image
1 Like"
Jordan Johnson answered Jordan Johnson commented

While the DateTime object can handle dates before 1970, the convert() command cannot. This is because the convert() command first converts all values into UNIX time, which (as you have noted) starts in 1970.

The DateTime object itself can handle dates before 1970:

DateTime(0).year // returns 1601

However, FlexSim currently doesn't have a way to parse text specifying dates older than 1970. It is on the development list, as part of improvements to the DateTime class API.

Is there a reason you need dates from before 1970?

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeremy R avatar image Jeremy R commented ·

@jordan.johnson, Yes, objects in our model have a specific creation date, which is used to determine future actions. For example, people have their date of birth stored (which is often before 1970), which we use to determine (for example) their age, which is then uses in the model to trigger certain events (e.g. retirement). There's similar uses in our model for buildings, some of which were constructed before 1970.
While a workaround for this specific example might be possible, what we really want is to just put these dates in a DateTime object and treat all dates in the model the same way. The fact that the DateTime object does support pre-1970 dates, but that we just can't convert our strings into DateTime objects, is the frustrating part.

1 Like 1 ·
Jordan Johnson avatar image Jordan Johnson ♦♦ commented ·

There are some plans to change this for version 2020.

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.