Dictionary attribute as date

I have a string variable

dateinistr = “2021-06-01T09:00:00”

I converted to date

dateini = Date.parse dateinistr

Then I tried to attach it to a component (called e) in this way:

e.set_attribute 'Sketchup4D’,’Inicio', dateini

The problem is that Sketchup does not recognize it as a Date.

¿What I’m doing wrong?

A few things are wrong…
Your example code includes some ‘smart-quotes’ - these are not allowed in Ruby.
Use " or ’ instead around the string…
The set_attribute arguments must not include then either - so again, use " or ’
It works fine as a ‘date’ attribute when I correct your code…

Adjust your MAC’s setting to not use smart-quotes in Ruby scripts…

According to: AttributeDictionary #[]=-instance_method

The value can be :
Boolean, Integer, Float, Length, nil, String, Time, Array, Geom::Point3d, Geom::Vector3d

The Date class is not one of them.

Thanks @TIG for your reply.
Smart Cuotes was an error because I typed that in Apple notes not here.
The only way I receive a value is if I put

Date.parse(dateinistr).to_s

But I receive as a String

Thanks @dezmo it should be the problem why I only recive a String.

Go through the core Time class and convert to and from integer seconds since the Unix Epoch.

Ie …

dict.set_attribute( 'Sketchup4D', 'Inicio', dateini.to_time.to_i )

Then to read it back from the dictionary …

secs = dict.get_attribute( 'Sketchup4D', 'Inicio' )
dateini = Time.at(secs).to_date
1 Like

Thanks a lot @DanRathbun . It’s a good option but seconds may be a very big numer to deal with.

For serializing the date? That’s very common thing to do - storing as Unic Epoch. What is the concern?

I’m trying to import a schedule from Microsoft Project. Then assign a task from the schedule to every element in the model. That lets me attach the start and finish dates too.
With that data I can see the elements between two dates, making an animation of the building process or checking the elements that are built in a certain moment.
I only need days and generally my projects are built in less than 2 years.

Even so, what’s the concern of encoding Time as unix epoch?

… computers love integers. They can work with integers really fast. But Ruby is slow with strings.

On today’s 64bit machines, a signed integer can have a max value of 9,223,372,036,854,775,807.

In seconds that is: 292,471,208,677 (292 billion, 471 million, etc.) years !

You convinced me, I will save start and finish as unix epoch… It will be faster while animating