www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - date and time

reply Joel Christensen <joelcnz gmail.com> writes:
I'm using D2.049. I have a program in which I want to be able to get the 
current time as one number, also be able to change the hour and stuff 
then convert it back to one number. I'm saving just the one number to file.

Thanks in advance. :-)
Sep 25 2010
parent reply "Yao G." <yao.gomez spam.gmail.com> writes:
On Sat, 25 Sep 2010 18:47:39 -0500, Joel Christensen <joelcnz gmail.com>  
wrote:

 I'm using D2.049. I have a program in which I want to be able to get the  
 current time as one number, also be able to change the hour and stuff  
 then convert it back to one number. I'm saving just the one number to  
 file.

 Thanks in advance. :-)
std.datetime is your friend. http://www.digitalmars.com/d/2.0/phobos/std_date.html -- Yao G.
Sep 25 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
I've tried that module.

I was putting:
long dt = UTCtoLocalTime(getUTCtime - (msPerDay / 2));
Then when daylight savings came it was wrong, (computer was right mind).

long datetime = UTCtoLocalTime(getUTCtime);
It is 1 hour and half a day out. It was the right hour till daylight 
savings. I live in New Zealand.

Some one said that module was a mine field.

 std.datetime is your friend.

 http://www.digitalmars.com/d/2.0/phobos/std_date.html
Sep 25 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 25 September 2010 17:48:54 Joel Christensen wrote:
 I've tried that module.
 
 I was putting:
 long dt = UTCtoLocalTime(getUTCtime - (msPerDay / 2));
 Then when daylight savings came it was wrong, (computer was right mind).
 
 long datetime = UTCtoLocalTime(getUTCtime);
 It is 1 hour and half a day out. It was the right hour till daylight
 savings. I live in New Zealand.
 
 Some one said that module was a mine field.
std.date is definitely broken. Some of it works, much doesn't. It is going to be replaced by std.datetime which will have much better functionality for both times and dates, but that hasn't been finished yet. Both Yao G. and I are working on implementations which are going to be reviewed for possible inclusion in phobos as std.datetime, and we're getting close to being done, but as I said, it's not finished yet. So, your best bet is to either mess around with std.date and figure out if you can get the functionality you need to work one way or another (I think that I ended up only ever using UTC in my code that used std.date since I couldn't get it to properly print local time), or you can use the C API directly: http://www.cppreference.com/wiki/c/date/start I believe that core.sys.posix.time.d already has the appropriate prototypes for them in D (on posix systems at least). - Jonathan M Davis
Sep 25 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks for the replies Jonathan M Davis and Yao G. :-)

Good to hear it's being worked on. I've other programs that are done 
with D1.0 that are all right.

I'm using Windows, would like it to work on Linux too though.

I think I'll use year month etc. separately instead of just having a big 
number (for converting to the time measurements).

How do you access the core library? I wanted to look at the core time 
module and couldn't, (C/C++ sites of course have that information any way).
Sep 25 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 25 September 2010 21:37:30 Joel Christensen wrote:
 Thanks for the replies Jonathan M Davis and Yao G. :-)
 
 Good to hear it's being worked on. I've other programs that are done
 with D1.0 that are all right.
 
 I'm using Windows, would like it to work on Linux too though.
 
 I think I'll use year month etc. separately instead of just having a big
 number (for converting to the time measurements).
 
 How do you access the core library? I wanted to look at the core time
 module and couldn't, (C/C++ sites of course have that information any way).
If you want to use it, just import it. e.g. import core.sys.posix.time; I don't see a corresponding set of definitons for Windows though, for some reason - I have no idea why. If you want to look at the code, then look look in your dmd directory. It's in src/druntime/src/. core.sys.posix.time is src/druntime/src/core/sys/posix/time.d. From the little I've look at core though, the system stuff really isn't documented. It's pretty much just including proper declarations for C system stuff and giving a way for your to access it without directly putting the C declarations in your code yourself. It also deals with some of the differences on platforms, which can prove to be quite annoying - even among posix implementations. Of course, the stuff you're looking for is standard C stuff, so you could just do it yourself directly - all it really requires is the function declarations. Check out http://www.digitalmars.com/d/2.0/interfaceToC.html for more details. - Jonathan M Davis
Sep 25 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks Jonathan. My plan is to use the C version, and not use the big 
time_t number. I'm actually using std.c.stdio module too, for binary 
files, I probably should use std.stream or some thing.
Sep 25 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 25 September 2010 22:40:39 Joel Christensen wrote:
 Thanks Jonathan. My plan is to use the C version, and not use the big
 time_t number. I'm actually using std.c.stdio module too, for binary
 files, I probably should use std.stream or some thing.
Well, std.stdio will give you a way to read binary files, so you can use that, but you should probably avoid std.stream. As far as I know, it works just fine, but it's scheduled for deprecation as soon as a replacement has been decided on. std.stream is not range-based, and the phobos developers what streams in phobos to be range-based, so they plan to replace std.stream, but they're still trying to figure out what the best design for the the new std.stream should be, and so I have no idea when it'll actually be replaced. - Jonathan M Davis
Sep 25 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks for your reply Jonathan. Yes, I heard about stream being 
replaced, but since my code with binary files isn't very much, I can 
just redo it if I have to. I think I'll continue using std.c.file for 
the time being.

I should learn about ranges.

I tried std.stdio already.

It's been been good getting replies here. :-)
Sep 26 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday 26 September 2010 00:23:23 Joel Christensen wrote:
 Thanks for your reply Jonathan. Yes, I heard about stream being
 replaced, but since my code with binary files isn't very much, I can
 just redo it if I have to. I think I'll continue using std.c.file for
 the time being.
 
 I should learn about ranges.
You'll be able to do a lot more with stuff like std.algorithm if you have a good understanding of ranges (I should also note that std.algorithm is a prime example of why auto is such a good thing - you do _not_ want to have to use all of the specific return types in std.algorithm directly). This article by Andrei would be a good place to start: http://www.informit.com/articles/article.aspx?p=1407357 There's also this presentation that he did on ranges at BoostCon 2009: http://blip.tv/file/2432106 They require you to think differently about some things, but for a lot of stuff, it can be very powerful and much simpler to do. One really cool thing is that you can process a range pretty much like you'd process an slist in a functional language like lisp or Haskell (you can use front and popFront() very similarly to how head/car and tail/cdr are used in function languages). I really love ranges, but they can take some getting used to. A lot of folks seme to react fairly negatively to them intially, but once you've messed with them a bit, you'll probably agree that in most cases, they're at least as good if not superior to iterators. - Jonathan M Davis
Sep 26 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks again for the reply Jonathan. I'm using doublely linked list I 
made for a game where ships and there lazer bolts are in the same list. 
Without linked list I couldn't do things like create a lazer bolt or 
remove one while trans-versing the linked list. I had to use my own 
linked list, having a next and prev node in each object that goes in the 
list.
Sep 26 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Joel Christensen:

 Thanks again for the reply Jonathan. I'm using doublely linked list I 
 made for a game where ships and there lazer bolts are in the same list. 
 Without linked list I couldn't do things like create a lazer bolt or 
 remove one while trans-versing the linked list. I had to use my own 
 linked list, having a next and prev node in each object that goes in the 
 list.
I am looking for use cases of singly|doubly linked lists, I (nearly) never need them in my code. Few questions: 1) What's a typical (or average) length of your list? 2) Are items sorted (like from the bigger to the smaller one)? 3) Is the item order important (so is it OK to shuffle them in some other ways)? 4a) How often/many do you need to add items to the list? 4b) How often/many do you need to remove items to the list? Bye, bearophile
Sep 26 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
 I am looking for use cases of singly|doubly linked lists, I (nearly) never
need them in my code. Few questions:
 1) What's a typical (or average) length of your list?
Thanks for your interest bearophile. I haven't used linked list much more than just trying them out. And my game is at its earlier stages. Not very much, 2 players, mines ad lazer bolts are added and removed when used. I guess around 10.
 2) Are items sorted (like from the bigger to the smaller one)?
I've got the player objects fixed at the head of the array.
 3) Is the item order important (so is it OK to shuffle them in some other
ways)?
I'm thinking of having then sorted as objects are added. I'm also thinking of having a pointer to the start and end of each kind of object (like sub lists).
 4a) How often/many do you need to add items to the list?
My game is only small at the moment, so not a good sample. I guess every time a player fires a lazer bolt or lays a mine, but those objects don't last long.
 4b) How often/many do you need to remove items to the list?
About as often as I add them, eg. the mines and lazers are added and taken away often. How many, two at a about the same time with two players.
Sep 26 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Joel Christensen:

 Not very much, 2 players, mines ad lazer bolts are added and removed 
 when used. I guess around 10.
I see, then use a dynamic array, plus append, and few functions like remove. It's faster and simpler. In most situations today (in a language that allows mutability) linked lists are the wrong data structure to use. Bye, bearophile
Sep 26 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
I normally use D's built in dynamic arrays. but it doesn't work with 
adding and removing with the foreach loop (like removing an object from 
the list while still going through the list).
Sep 26 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday 26 September 2010 11:38:35 Joel Christensen wrote:
 I normally use D's built in dynamic arrays. but it doesn't work with
 adding and removing with the foreach loop (like removing an object from
 the list while still going through the list).
I would point out to you that Bearophile seems strongly opposed to linked lists in general and seems to point out to people that arrays and vector/ArrayList types (Array in std.container) are faster on modern hardware whenever they come up. I can only assume that it's one of his pet peeves (we all have them - for instance, I hate it when people use post-increment or pos-decrement for instance when pre-increment or pre-decrement will do), but I don't think that he's necessarily right. Classicly in computer science, if you have a data structure which you're going to be doing a lot of appending to, prepending to, or inserting into, you use a linked list. std.container currently has SList, which is a singly-linked list but no doubly-linked list. However, they don't have random access and they take up more memory than an array or an Array (thanks to all of those prev and next pointers). Bearophile objects to them, I believe, because they aren't a single block of memory and so they harm cache locality, resulting in poorer cache performance from the CPU. He may very well be right (in fact on some level, I'm sure he is), but that's the sort of thing that most people worry about when they find that their data structure has poor performance rather than reacting negatively to any suggestion of using a linked list. Whether using a linked list is the best choice for your application, I don't know, but it is often the case that you can do the job just as well or better with an Array, tree, or hash table, depending on what you're trying to do and how you're trying to do it. However, if you constantly adding and removing from a container (especially if you're doing it anywhere other than the end), and you rely on insertion order (so you can't use a hash table) rather than sorted order (like you would with a tree), then a linked list is likely the best choice. It has cheap insertions and removals. Certainly, in D, I would avoid having lots of appending to and removal from the end of dynamic arrays in your code because the GC isn't efficient enough and that's going to result in a lot of allocations and deallocations, but Array should solve that problem for the most part, since it uses an internal array which is larger than the Array's length so that it can reduce how often reallocation occurs (and if you've removed from the end, then it has more room for adding more to the end, so it shouldn't have the same problem as a built-in array). In any case, from your description of having to remove from the middle of a list would indicate that a linked list is probably the best for what you're doing, but not knowing in detail, I'm obviously not in the best place to judge. - Jonathan M Davis
Sep 26 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 I can only assume that it's one of his pet peeves (we all have them -
This is a long story, and I don't have time now, I am sorry. Something related: http://tinyurl.com/3yrawox Bye, bearophile
Sep 26 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks for the long winded reply Jonathan.

I don't know how to avoid using my own linked list, I have next/prev in 
each class (Ball, Lazer and Mine ) in the list.

Thanks bearophile, I had a bit of a look at that site.

My game is simple so just maybe the easiest way is the way to go, though 
if I use the most tightest way and so my game would be an prototype for 
bigger games to reference.
Sep 26 2010
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 27 Sep 2010 10:27:36 +0400, Joel Christensen <joelcnz gmail.com>  
wrote:

 Thanks for the long winded reply Jonathan.

 I don't know how to avoid using my own linked list, I have next/prev in  
 each class (Ball, Lazer and Mine ) in the list.
That's called intrusive linked list, and I find using it quite viable: zero-allocation O(1) add/removal is a very strong characteristics. They are very useful especially for lock-free algorithms.
Sep 27 2010
parent Joel Christensen <joelcnz gmail.com> writes:
 That's called intrusive linked list, and I find using it quite viable:
 zero-allocation O(1) add/removal is a very strong characteristics.

 They are very useful especially for lock-free algorithms.
That's for the info Denis. I got the idea from a friend who is interested in how to make games.
Sep 27 2010
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 26 Sep 2010 04:38:36 -0400, Joel Christensen <joelcnz gmail.com>  
wrote:

 Thanks again for the reply Jonathan. I'm using doublely linked list I  
 made for a game where ships and there lazer bolts are in the same list.  
 Without linked list I couldn't do things like create a lazer bolt or  
 remove one while trans-versing the linked list. I had to use my own  
 linked list, having a next and prev node in each object that goes in the  
 list.
Not sure if dcollections could be of use: http://www.dsource.org/projects/dcollections And the linked list class: http://www.dsource.org/projects/dcollections/browser/branches/d2/dcollections/LinkList.d One thing my lib supports is removal while traversing via foreach. See the purge function. Sorry about lack of online docs, I need to figure out how to automatically generate them (the D1 docs are auto-generated, but I haven't put in any time to figure out how to generate the D2 version). -Steve
Sep 27 2010
parent reply Johannes Pfau <spam example.com> writes:
On 27.09.2010 14:01, Steven Schveighoffer wrote:
 
 
 Sorry about lack of online docs, I need to figure out how to
 automatically generate them (the D1 docs are auto-generated, but I
 haven't put in any time to figure out how to generate the D2 version).
 
 -Steve
Yao has a nice ddoc template for D2. See for example http://d.yao.com.mx/datetime/core.html The ddoc file is available here: https://bitbucket.org/gomez/yao-library/src/da11956a6a6e/docs/ but I don't know about the license for that file, you might have to ask him about that. This doesn't generate an index though, if you meant that. -- Johannes Pfau
Sep 27 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 27 Sep 2010 08:34:57 -0400, Johannes Pfau <spam example.com> wrote:

 On 27.09.2010 14:01, Steven Schveighoffer wrote:
 Sorry about lack of online docs, I need to figure out how to
 automatically generate them (the D1 docs are auto-generated, but I
 haven't put in any time to figure out how to generate the D2 version).

 -Steve
Yao has a nice ddoc template for D2. See for example http://d.yao.com.mx/datetime/core.html The ddoc file is available here: https://bitbucket.org/gomez/yao-library/src/da11956a6a6e/docs/ but I don't know about the license for that file, you might have to ask him about that. This doesn't generate an index though, if you meant that.
Thanks, Yao's seems similar to Phobos, which I'm not a big fan of. Particularly the munged-together list of constructs/functions at the top. I used Tango's (old) doc generation templates to generate the D1 docs, and Brad of dsource set up my project to rebuild the D1 docs on every checkin. However, I don't think he's set up a D2 compiler, and even if he did, it's changing so frequently that he'd have to update it too often. I dumped the Tango doc templates for the D2 version, and I want to rewrite them, but I just haven't had the time. In particular, I want a categorical index at the top or on the side (i.e. functions, members, etc.) Having everything munged together is super-confusing. I really like the dil-generated docs that Tango uses, but I'm pretty sure dil isn't D2... -Steve
Sep 27 2010
parent "Yao G." <yao.gomez spam.gmail.com> writes:
On Mon, 27 Sep 2010 07:46:49 -0500, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 Thanks,

 Yao's seems similar to Phobos, which I'm not a big fan of.  Particularly  
 the munged-together list of constructs/functions at the top.
To be honest, I don't like it either :) I plan to improve it, and by improve it I mean to remove that cloud-like unordered list of symbols, and use a tree or something similar. CandyDoc sometimes hangs Opera, so I wanted to make a quick replacement, that also worked well without JavaScript. Maybe I'll switch to Kandil or just use the doc generator of Dil. -- Yao G.
Sep 27 2010
prev sibling parent "Yao G." <yao.gomez spam.gmail.com> writes:
On Mon, 27 Sep 2010 07:34:57 -0500, Johannes Pfau <spam example.com> wrote:

 Yao has a nice ddoc template for D2. See for example
 http://d.yao.com.mx/datetime/core.html
 The ddoc file is available here:
 https://bitbucket.org/gomez/yao-library/src/da11956a6a6e/docs/ but I
 don't know about the license for that file, you might have to ask him
 about that. This doesn't generate an index though, if you meant that.
That ddoc file is in, let's say, experimental stage, and I had to make some "hacks" to make the generated docs pass the HTML4 validator (see for example the UL or the D_CODE macros). They don't have licence because I didn't find an equivalent to the Boost Licence but for documentation, but they can be used freely for whatever need you have. I still have to make some improvements, for example create a tree of symbols, like the one in Kandil or CandyDoc. -- Yao G.
Sep 27 2010