Visit Our UNIX and Linux User Community


may be simple but i don't know -- Print current date from C program


 
Thread Tools Search this Thread
Top Forums Programming may be simple but i don't know -- Print current date from C program
# 1  
Old 02-15-2002
Lightbulb may be simple but i don't know -- Print current date from C program

How to print current date of the Unix system accessing thru C++ program ?

I wrote like this

#include <time.h>
.......
time_t tt;
struct tm *tod;
....
time(&tt);
tod = localtime(&tt);
cout << tod->tm_mon + 1 << "/"
<< tod->tm_mday << "/"
<< tod->tm_year << endl;

Still the year/ date /month printed is not correct


The asctime(tod) prints Fri Feb 15 05:49:29 2002 the correct date

But the cout << tod->tm_mon / tm_mday / tm_year prints wrong values only
The output is like
2/17/146

which has not correspondance with Feb 15/ 2002 date

Please help

Thanks
# 2  
Old 02-15-2002
I just tried your code and I got:
2/15/102
which is the expected result since today is Feb 15, 2002.

Just as you are adding 1 to tm_mon, so must you add 1900 to tm_year.

If you are really getting 2/17/146, you must have a bug elsewhere.
# 3  
Old 02-15-2002
may be simple but i don't know -- Print current date from C program

Yes. This seems to be really funny.
I am getting the same result ie "2/17/146" only.

The date in command prompt gives correct date
Fri Feb 15 07:22:05 CST 2002

But when accessing from C program, I get the 2/17/146 result.

Is there any other environmental set up like <locale.h > inside my C program ?
or can we achieve the same by using getdate()

Thanks
LS1429
# 4  
Old 02-15-2002
Here is my test program:
Code:
#include<iostream.h>
#include<time.h>
main()
{
        time_t tt;
        struct tm *tod;
        time(&tt);
        tod=localtime(&tt);
        cout << tod->tm_mon +1 << "/" << tod->tm_mday << "/"
             << tod->tm_year << endl ;
        exit(0);
}

Give it a try. I am getting 2/15/102 without doing anything more. Internally the library will at my TZ environment variable but that's the only other thing that can affect the output that I know of.

And by the way, I tested on a Sun. What system are you using?
# 5  
Old 02-18-2002
may be simple but i don't know -- Print current date from C program

I found one difference from my code from yours.

I am using the tm structure in a function instead of main()
My sample code structure is

#include <iostream.h>
#include <iomanip.h>
#include <time.h>
....
int main(int argc, char **argv)
{
.....
display_estHdr_formatted(msg);
.....
}

int displayHdr(istrstream& msg)
{

time_t tt; // Seconds since 1/1/1970
struct tm *tod;

time(&tt);
tod = localtime(&tt);

cout << tod->tm_mon + 1 << "/"
<< tod->tm_mday << "/"
<< tod->tm_year << endl
<< tod->tm_sec << endl;


msg.read(displayBuffer,424);

msg.read(displayBuffer, 9);

return 0;
}

I am using IBM Aix version
# 6  
Old 02-18-2002
Did you try my program? Did it work?
# 7  
Old 02-19-2002
may be simple but i don't know -- Print current date from C program

Thanks.

Your program worked and when embedded in my program as individual function it was continuing the same.

The actual problem was before call of the function, I have converted an output to octal which has set the o/p to octals.

Hence octal 146 corresponds to 102 (year 1900 +102) decimal
and hence I was receiving the date in octal.

Smilie

Thanks for your help.
LS1429

Previous Thread | Next Thread
Test Your Knowledge in Computers #35
Difficulty: Easy
The IP address 127.0.0.1 is also called 'localhost'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

2. UNIX for Dummies Questions & Answers

print all dates 100 days from current date

can anyone please suggest me on ideas to write a script which has to go back to 100 days from 'current date' and print the date of each day starting from 100th day to current day. (korn shell please) Thanks Pavan (5 Replies)
Discussion started by: pavan_test
5 Replies

3. UNIX for Dummies Questions & Answers

awk to print current date?

List, I want to print the first line of my text file (say "me you"), preceded by the current date/time. Something like (pseudo code): awk '{print date,$1}' I don't have a lot of awk knowledge (understatement), so forgive me if the answer is obvious... (3 Replies)
Discussion started by: philipz
3 Replies

4. UNIX for Dummies Questions & Answers

How a program to convert '|' delimited file into excel?

Hi All, I want to convert a '|' delimited file to excel file. I want it is an automated process (this means I need not to open excel, import and choose '|' delimited, then ... to generate an excel) I need to use a scripts or proram to convert the file. Do any one have this tools? A... (11 Replies)
Discussion started by: wilsonchan1000
11 Replies

5. UNIX for Dummies Questions & Answers

I don't know where to start

I have been in computers for 30 years and know everything about MS, but nothing about anything else. Been developping websites (inter alia) running on MS servers using ASP's and vbScripts which apparently does not work under Unix. My own website is being hosted on a Unix server and I need to... (4 Replies)
Discussion started by: paul@cascom
4 Replies

6. UNIX Desktop Questions & Answers

Genome Startmenu crashed why I don't Know ??

what I have to do ? (1 Reply)
Discussion started by: atiato
1 Replies

7. Programming

a very simple question (but i don't know)

how to write a program that receive a string from keyboard and then print it out. i write a program: main() { char str; gets(str); printf(str); } but when i compile it, the system said something like "new line is not last charicter" and sometime the system said "a3.c is up to date"... (1 Reply)
Discussion started by: dell9
1 Replies

8. UNIX for Dummies Questions & Answers

Don't hate me because I'm stupid...

Hi all, I don't know the first thing about Unix, but I would like to learn. I would like to know what the difference between Linux and Unix is, and where I can obtain a copy of either. Thanks :o (8 Replies)
Discussion started by: ThisIsNewToMe
8 Replies

9. UNIX for Dummies Questions & Answers

Don't Know to Rebuild Linux Kernel(URGENT)

Hello. I'm installing Oracle8i on Red Hat Linux Server 7.0. The Oracle documentation has some preinstallation request to be carried before installing the Oracle software. It asks to modify some SEMAPHORE & SHARED MEMORY values in to a specified file. And finally after updating this values it... (2 Replies)
Discussion started by: S.Vishwanath
2 Replies

10. UNIX for Dummies Questions & Answers

How to subtract 2 hours from 'date' in shell ( /bin/sh ) script ?

I write a sh script that zip and copy to tape all files that older then 2 hours. 1. The way I choose is - touch a file with "now - 2 hours", then use fine with '! -newer' 2. Do you have any other idea to do it ? tnx. (1 Reply)
Discussion started by: yairon
1 Replies

Featured Tech Videos