Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » EGL Development Tools » Timestamps and nulls(Some notes on references, values, nulls and how they relate to timestamps)
Timestamps and nulls [message #760871] Mon, 05 December 2011 13:48 Go to next message
Joe Pluta is currently offline Joe PlutaFriend
Messages: 62
Registered: November 2011
Member
This started out as a discussion of timestamps and why they have to have a question mark in a record. Here's what I learned about defining a timestamp: if it has a format, it's a value, if it doesn't it's a reference. Okya, fine, but I'm still wrapping my head around the fundamental difference between reference and value. This is the definition form the language reference:

Quote:
A reference type defines an object, which is a value in a memory area that was separately allocated to hold the value. The object is referenced from some logic and is an instance of the type. In this case, the words "value," "object," and "instance" are interchangeable.

A value type defines a value that is embedded in an object.


From timestamp you get this:

Quote:
A Timestamp with a format is a value. A Timestamp without a format is a reference, and it may point to any other Timestamp, with or without a format. It's not possible to create a Timestamp unless it has a format, i.e. new timestamp("yyyyMM") is valid but new timestamp is not.


If I work my way through this, what I get is this: a value is a distinct instance of a type, while a reference is a variable that refers to a type (and may or may not be null). That's not exactly how it reads; the idea of a value type being embedded in a reference type seems to overload the word "type" too much. But let me continue.

In Java, you can define a variable of type MyClass. That variable is a reference, and may contain either a null or an object of type MyClass (or its subclass, but that's irrelevant to this discussion). Object references (variables) are intrinsically null-capable. In EGL, nullability is an attribute; a variable can either be nullable or not. If it's not nullable, then when you declare the variable you are implicitly creating an object of that type.

And that's where the problem lies for timestamps. For reasons that aren't entirely clear to me, there is no default format for a timestamp and so you can't create a timestamp value without a format. You CAN create a variable of type timestamp without a format, but it cannot be implicitly initialized, because you can't create a value without a format. So, in order to declare a variable of type timestamp, you have to either declare it with a format or as nullable:

t1 timestamp ("hhmmss");
t2 timestamp?;

The former is initialized with a value of the current time in HHMMSS format, while the second is initialized with null.

See my next post for some interesting results of a test program.
Re: Timestamps and nulls [message #760887 is a reply to message #760871] Mon, 05 December 2011 14:15 Go to previous messageGo to next message
Joe Pluta is currently offline Joe PlutaFriend
Messages: 62
Registered: November 2011
Member
Run this program:
program TestTimestamps
	
	t_hms timestamp ("hhmmss");
	t_ymdhms timestamp ("yyyyMMddhhmmss");
	t_mdhms timestamp ("MMddhhmmss");
	tn_dhms timestamp ("ddhhmmss")?;
	
	function main()
		Syslib.writestdout("t_hms(i): " :: t_hms);
		Syslib.writestdout("t_ymdhms(i): " :: t_ymdhms);
		Syslib.writestdout("t_mdhms(i): " :: t_mdhms);
		Syslib.writestdout("tn_dhms(i): " :: tn_dhms);
		tn_dhms = t_hms;
		Syslib.writestdout("tn_dhms(t_hms): " :: tn_dhms);
		t_ymdhms = tn_dhms;
		Syslib.writestdout("t_ymdhms(tn_dhms): " :: t_ymdhms);
		tn_dhms = t_mdhms;
		Syslib.writestdout("tn_dhms(t_mdhms): " :: tn_dhms);
		t_ymdhms = tn_dhms;
		Syslib.writestdout("t_ymdhms(tn_dhms): " :: t_ymdhms);
	end
	
end

and you get this:
t_hms(i): 08:12:47
t_ymdhms(i): 2011-12-05 08:12:47
t_mdhms(i): 12-05 08:12:47
tn_dhms(i): 
tn_dhms(t_hms): 01 08:12:47
t_ymdhms(tn_dhms): 1970-01-01 08:12:47
tn_dhms(t_mdhms): 05 08:12:47
t_ymdhms(tn_dhms): 1970-01-05 08:12:47

I did a more complete program and got some anomalous results. I'll try to post it when I get the chance. But even this should make you think a little. My particular favorite is the last line, which ends up with 1970-01-05 as the date. Interesting!

Joe
Re: Timestamps and nulls [message #760899 is a reply to message #760871] Mon, 05 December 2011 14:36 Go to previous messageGo to next message
Joe Pluta is currently offline Joe PlutaFriend
Messages: 62
Registered: November 2011
Member
Here is a test program I wrote:
program TestTimestamps
	
	t_hms timestamp ("hhmmss");
	t_mdhms timestamp ("MMddhhmmss");
	t_ymdhms timestamp ("yyyyMMddhhmmss");
	tn timestamp?;
	tn_dhms timestamp ("ddhhmmss")?;
	
	function main()
		Syslib.writestdout("t_hms(i): " :: t_hms);
		Syslib.writestdout("t_mdhms(i): " :: t_mdhms);
		Syslib.writestdout("t_ymdhms(i): " :: t_ymdhms);
		Syslib.writestdout("tn(i): " :: tn);
		Syslib.writestdout("tn_dhms(i): " :: tn_dhms);
		
		tn = t_hms;
		Syslib.writestdout("tn(t_hms): " :: tn);
		t_ymdhms = tn;
		Syslib.writestdout("t_ymdhms(tn): " :: t_ymdhms);
		
		tn = t_mdhms;
		Syslib.writestdout("tn(t_mdhms): " :: tn);
		t_ymdhms = tn;
		Syslib.writestdout("t_ymdhms(tn): " :: t_ymdhms);
		
		tn_dhms = t_hms;
		Syslib.writestdout("tn_dhms(t_hms): " :: tn_dhms);
		t_ymdhms = tn_dhms;
		Syslib.writestdout("t_ymdhms(tn_dhms): " :: t_ymdhms);
		
		tn_dhms = t_mdhms;
		Syslib.writestdout("tn_dhms(t_mdhms): " :: tn_dhms);
		t_ymdhms = tn_dhms;
		Syslib.writestdout("t_ymdhms(tn_dhms): " :: t_ymdhms);
		
		tn_dhms = t_mdhms;
		Syslib.writestdout("tn_dhms(t_mdhms): " :: tn_dhms);
		t_ymdhms = tn_dhms;
		Syslib.writestdout("t_ymdhms(tn_dhms): " :: t_ymdhms);
	end
	
end

And here are the results:
t_hms(i): 08:31:23
t_mdhms(i): 12-05 08:31:23
t_ymdhms(i): 2011-12-05 08:31:23
tn(i): 
tn_dhms(i): 
tn(t_hms): 1970-01-01 08:31:23
t_ymdhms(tn): 1970-01-01 08:31:23
tn(t_mdhms): 1970-12-05 08:31:23
t_ymdhms(tn): 1970-12-05 08:31:23
tn_dhms(t_hms): 01 08:31:23
t_ymdhms(tn_dhms): 1970-01-01 08:31:23
tn_dhms(t_mdhms): 05 08:31:23
t_ymdhms(tn_dhms): 1970-01-05 08:31:23
tn_dhms(t_mdhms): 05 08:31:23
t_ymdhms(tn_dhms): 1970-01-05 08:31:23

They seem consistent, with a couple of observations. If you specify a format, the values you don't specify do not print. As you move from one variable to another, only the defined pieces move with you; if the source doesn't have as many components as the target, the target values are defaulted. It looks like the date defaults to 1970-01-01. And finally, a timestamp with no format (in this case, tn) seems to act as though it has a format of yyyyMMddhhmmss.

Joe
Re: Timestamps and nulls [message #761695 is a reply to message #760899] Tue, 06 December 2011 22:34 Go to previous messageGo to next message
Matt Heitz is currently offline Matt HeitzFriend
Messages: 36
Registered: July 2009
Member
Hi Joe,

That's an awful lot to chew on. Can you point out the places where you have specific questions or concerns in your test programs?

Thanks,
Matt


P.S. Regarding your original post, it might help to think of a timestamp with a format as a record with two fields: its format and its value. Timestamp variables declared with the same format will have identical format fields, but their values may differ.

A timestamp without a format could be thought of as a pointer to that kind of record. The record it points to at one moment might have a different format and value from another record it points to later.

If a timestamp without a format is nullable, and you don't put an initializer on its declaration, its initial value is null because it has no timestamp record to point at.
ts timestamp?; // it's null

If you have a non-nullable timestamp without a format, you must initialize it, as in this example.
t1 timestamp("hhmmss");
t2 timestamp = t1;
It's an error to omit t2's initializer because t2 isn't nullable. That means it has to be pointing at some record, all the time.
Re: Timestamps and nulls [message #762148 is a reply to message #761695] Wed, 07 December 2011 16:05 Go to previous messageGo to next message
Joe Pluta is currently offline Joe PlutaFriend
Messages: 62
Registered: November 2011
Member
Okay, the only thing I'm really having difficulty with is the idea of a timestamp without a format. In reality, the timestamp DOES have an implicit format; it's yyyyMMddhhmmss. If you do this:

t1 timestamp?;

You have assigned the format "yyyyMMddhhmmss" to t1. Don't believe me? Try this:

t2 timestamp ("hhmmss");
t1 = t2;
Syslib.writestdout(t1);

You'll get this:

1970-01-01 09:57:13

That's because t1 thinks it has yyyyMMdd fields. Since they aren't assigned, they assume the default values (not TODAY'S values, the arbitrary default values). This just seems a little weird to me. Note that I'm not sure how else I would do it. Would it be any more intuitive if an unformatted timestamp took on the format of its first assignment? I don't necessarily think so. I'm just saying the current behavior might take someone by surprise and should be documented.

Joe
Re: Timestamps and nulls [message #762249 is a reply to message #762148] Wed, 07 December 2011 18:59 Go to previous messageGo to next message
Brian Svihovec is currently offline Brian SvihovecFriend
Messages: 55
Registered: July 2009
Member
Joe,

Take a look at the function "asString(value ETimestamp in)" in the source code for EString.egl.

The comment for this function states:
/**
 * {@Operation widen} Converts a timestamp to a string. The 26-character result
 * will include all possible fields of a timestamp, from years down to fractions
 * of seconds, in the format "yyyy-MM-dd HH:mm:ss.SSSSSS". Leading zeros are
 * included in each field of the string when necessary, e.g. January is
 * represented as "01" not "1".
 */ 


In the absence of a value for a field, it looks like the displayed value is using the 'initial values' for these fields.

If you want to display the value using a specific format, you can try:

Syslib.writestdout(StringLib.format(t1, "hhmmss"));


The functions provided by StringLib.egl can be found here.

I agree that this information needs to be made easily accessible, but hopefully we will be able to produce documentation from these comments in the future, without having to duplicate the information in the standard documentation.

-Brian
Re: Timestamps and nulls [message #762264 is a reply to message #762249] Wed, 07 December 2011 19:24 Go to previous message
Joe Pluta is currently offline Joe PlutaFriend
Messages: 62
Registered: November 2011
Member
Thanks for the information, Brian. I hope I'm not coming across as complaining about the dates; I'm just trying to understand. I especially like to look at these things from the viewpoint of an RPG programmer. Timestamps are a bit different than in the RPG world, where they always have all the pieces. RPG date, time and timestamp types each have an implicit, immutable format. Because of this, you can't assign one timestamp to another and only overlay part of the target. For example, this could never happen in RPG:

t1 = t2;
if (t1 != t2)
  // This can happen in EGL, not in RPG
end


Joe
Previous Topic:CheckBox behavior
Next Topic:Error when adding record parts to rui handler
Goto Forum:
  


Current Time: Fri Apr 19 08:30:36 GMT 2024

Powered by FUDForum. Page generated in 0.02696 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top