Login

Welcome, Guest. Please login or register.

September 06, 2025, 11:32:45 pm

Author Topic: Data Type Question  (Read 1552 times)  Share 

0 Members and 1 Guest are viewing this topic.

dida

  • Victorian
  • Trailblazer
  • *
  • Posts: 31
  • Respect: 0
Data Type Question
« on: November 13, 2013, 07:52:25 pm »
0
hi guys,

This may be a silly question, but if we are given a variable.. say.. TimeOfPurchase.. is this variable considered a Date/Time variable? Or a floating point/integer?

thanks

slothpomba

  • Honorary Moderator
  • ATAR Notes Legend
  • *******
  • Posts: 4458
  • Chief Executive Sloth
  • Respect: +327
Re: Data Type Question
« Reply #1 on: November 13, 2013, 08:35:08 pm »
+2
(Note: All this is conditional on the exact language you're using but i'm pretty sure every language out there certainly must have a time type)

Lets break it down.

What is an....

Integer: It's a whole number, no decimal points. You could represent the date as an integer, it might a bit of an odd choice but you could do it. Say for example todays date 13112013. It could work, at least in C the maximum integer value (for a long one anyway) is 2^31. I suppose you could store the time as a Int as well, especially if its in 24 hour time, so, it's 8:30 PM right now. In 24 hour time that would be 20:30 (just add 12 to the hour).

Code: [Select]
int Time = 20:30 (just realised i had a colon (:), obviously not a valid integer, just ignore that character)
Floating Point: It's similar to an integer in that its an number but it can be a decimal number here. I don't really see how it would be any more advantageous or "smart" to store your time in a float.

Time/Date Type: This is the most advantageous way, it's a special type for the time and more likely than not, the function that *gets* the time will store it in this kind of thing by default. Here is some C++ code i've been tinkering with lately, you dont necessarily need to understand it but just note how the time is never stored in an integer but in a special time type. Later i do convert it to an integer but thats just because i was messing around and only wanted a certain part of the time (the hour) so i could use it as an If condition ( if hour = 5 then print "5 O'clock).

Code: [Select]
#include <ctime>
#include <iostream>
using namespace std;

int main( )
{
   
   time_t now = time(0); // current date/time based on current system
   time_t timer;

   cout << "Number of sec since January 1,1970:" << now << endl;

   tm *ltm = localtime(&now);

   // print various components of tm structure.
   cout << "Year: "<< 1900 + ltm->tm_year << endl;
   cout << "Month: "<< 1 + ltm->tm_mon<< endl;
   cout << "Day: "<< ltm->tm_mday << endl;
   int meh = ltm->tm_hour;
   cout << meh;
   cout << "Time: "<< ltm->tm_hour << ":";
   cout << ltm->tm_min << ":";
   cout << ltm->tm_sec << endl;

   if (meh == 5)
   {
       cout << "5 O'clock!";
   }
}
« Last Edit: November 14, 2013, 12:15:05 am by slothpomba »

ATAR Notes Chat
Philosophy thread
-----
2011-15: Bachelor of Science/Arts (Religious studies) @ Monash Clayton - Majors: Pharmacology, Physiology, Developmental Biology
2016: Bachelor of Science (Honours) - Psychiatry research

dida

  • Victorian
  • Trailblazer
  • *
  • Posts: 31
  • Respect: 0
Re: Data Type Question
« Reply #2 on: November 13, 2013, 09:01:48 pm »
0
Thankyou so much, i completely understand now!

Really appreciate it :)

XpK

  • Victorian
  • Adventurer
  • *
  • Posts: 19
  • Programming Master Race, reporting in
  • Respect: 0
  • School: BMG
  • School Grad Year: 2013
Re: Data Type Question
« Reply #3 on: November 13, 2013, 09:34:48 pm »
0
Say for example todays date 13112013.

Have a look at the Unix Epoch Time, it's quite interesting! Dat 70's Unix tech
Lover of cars, player of guitars, king of the friendzone.

[Physics]
[English]
[Methods]
[Legal]
[IT SD]

^^ I've been waiting a whole damn year to fill that in with Study Scores...