PSE Entertainment Corp
May 21, 2012, 01:35:24 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Enjoy PSE Tunes!  They're great!
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Other Languages  (Read 508 times)
Swishdaddy
Newbie
*
Posts: 1


« on: October 30, 2010, 01:22:03 AM »

I'd like to request a new forum category be created for other inputs. For example:

CSS tip:

When you insert a Button in a div that is either floated or positioned using margin, put said button ("input" tag) within it's own (simple) div tag  (i.e. <div>input button text here</div>).

Took me a long time of false starts to find this silver bullet...

DateTime Handling:

I struggled for some time with storing Date and Time values entered by a User in a form into a database. What if the  User entered "2/31/2010"? Or "2:65 PM"? Well, we can use UI tools such as DateTime pickers to ensure proper inputs. But when we go back to report or display that data, how do we do mathematical calculations (i.e. "after Sept 6 and before Oct 2")? How do we sort by date and time?

The seemingly obvious answer is to cast your database fields as type DATETIME. However, this introduces all sorts of synaxtual issues when crafting your SQL queries. It really can be a pain in the ass, trust me.

My (I like to believe) novel solution is this... store the date, time, or datetime as a varchar (i.e. "10/26/2010 1:16 AM") and a numerical representaion of said datetime (i.e. "38790.23646788") (these values are not the same... I just pulled them out of thin air).

So what you do is calculate how many days have passed since an arbitrary date. In VB the "Cdbl" function passed against a datetime value

CDbl(CDate("10/21/2010 1:19 AM"))
CDbl(Cdate(Now))

will return the amount of days that have passed since, I believe, Jan 1, 1899. (There is some ambiguity about this date, but it really does not matter). Then, on the right side of the decimal point, is a decimal representation of the time. So the top of the hour would be "0", 15 minutes after would be ".25", 30 minutes after would be ".5", and so on. I store this numeric datetime value as a varchar as well.

In the end, I have two columns, one called "aDateTime", and one called "nDateTime" (the "a" is to break up the possibly reserved term "DateTime", and the "n" stands for "numeric"). I use aDateTime for all display needs (it's what I show on the web pages or the application forms) and the nDateTime for all my calculations, because now you can use all the mathematical functions your programming language supports to manipulate your date stuff cause it's all numbers, baby!!

I have used this to great effect in very complex datetime-dependent applications. It is the simplest damn way I ever did see, and that's a fact!



More to come, maybe...


Swish
Report to moderator   Logged
webmast
Guest
« Reply #1 on: January 20, 2011, 07:08:07 PM »

The VB integer date is days since (mm/dd/yyyy) 12/30/1899 (time epoch 0) for the CDate function.

The python equivalent starts at (mm/dd/yyyy) 01/01/0001 (time epoch 1).

To convert between the VB and Python mechanism you must do a delta between your date and 12/30/1899.

Example:

>>>d_vb6 = datetime.date(1899,12,30)
>>>d_python = datetime.date(2011,01,20)
>>>d_vb6_int = d_python - d_vb6

datetime.timedelta(40563)
Report to moderator   Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.14 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!