473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

question about using ctime

How do I use the library ctime to set the current time in: seconds, hours (24 hour), minutes into separate integers.

And how do i do the same thing with dates setting the current year, day , month into separate integers.

This is so i can use them to use the integers instead of user input and the user typing in the date themselves.
In c++.
Jan 13 '10 #1
5 2663
Banfa
9,065 Expert Mod 8TB
Read the ctime reference. There are only 9 functions so it should take you long to find the one you need.
Jan 14 '10 #2
I think i found it. struct tm right?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main ()
  5. {
  6.   time_t rawtime;
  7.   struct tm * timeinfo;
  8.  
  9.   time ( &rawtime );
  10.   timeinfo = localtime ( &rawtime );
  11.   printf ( "Current local time and date: %s", asctime (timeinfo) );
  12.  
  13.   return 0;
  14. }
this is a dumb question...
i dont see how to implement a "tm structure" as the return value
Jan 14 '10 #3
whodgson
542 512MB
Your code works fine and is probably as you intended
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>//added
  2. #include <stdio.h>
  3. #include <time.h>
  4. using namespace std; //added
  5.  
  6. int main ()
  7. {
  8.   time_t rawtime;
  9.   struct tm * timeinfo;
  10.  
  11.   time ( &rawtime );
  12.   timeinfo = localtime ( &rawtime );
  13.   printf ( "Current local time and date: %s", asctime (timeinfo) );
  14.   cout<<"\n";
  15.  
  16.  char q;
  17.  cout<<"Press any key to continue...";
  18.  cin>>q;
  19.  return 0;//these last 4 added
  20. }
printed: Current local time and date: Thu Jan 14 13:14:01 2010
Press any key to continue.
Jan 14 '10 #4
sorry that was example code from cplusplus reference... i dont think you read my first post...
im trying to say, and i quote myself

How do I use the library ctime to set the current time in: seconds, hours (24 hour), minutes into separate integers.

And how do i do the same thing with dates setting the current year, day , month into separate integers.
The example was the function i was going to use but i dont quite know how to use it...
i need to refine that code to fit my needs hence the quote.

i know no matter what function it is i need to use struct tm
p.s. read the quote
Jan 14 '10 #5
Banfa
9,065 Expert Mod 8TB
My answer would probably be only convert to struct tm as required (i.e. locally to any given function), pass the time around as a time_t.

Is this C or C++.

If it is C++ you can always just return a struct tm. Note don't return the struct tm* pointer because that points to static data it would be overwritten on the next call to localtime (or gmtime). Return *timeinfo.

In C returning structures is generally considered bad practice. Good C practice is to pass in a pointer to an already allocated block of data and let the called function fill it in. You could do that with struct tm.
Jan 14 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mike Zupan | last post by:
I wrote this simple threading app using pygame. I'm looking to load a movie via mplayer in full screen mode and have all my events be sent to my python app and not mplayer. When my mouse is over...
1
by: | last post by:
Hi I am using SQL server 7.0 and here is my code: Dim conn set conn = Server.CreateObject("ADODB.Connection") conn.open "websql" If conn.state=adStateOpen then Response.Write("ConnMade :...
1
by: Tivo Escobar | last post by:
Hi all, anybody here can tell me what exactly is returned by the standard function time() in <ctime> (or time.h). Please dont tell me it is the number of seconds since 1 Jan of 1970 GMT, cos it...
2
by: Hao Xu | last post by:
Hi everyone! I think that everyone knows ctime() in glibc: The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When ...
16
by: John Hanley | last post by:
I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except...
2
by: Chris | last post by:
Hello, How can i convert a C++ CTime (4 bytes) into a C# DateTime ? (my CTime is read in a file). Thanks, -- Chris
6
by: Schüle Daniel | last post by:
hello NG, consider this code >>> def timelogger(f): .... def wrapper(*a,**kw): .... print "started at %s" % time.ctime() .... t0 = time.time() .... ...
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
4
by: Pietro Cerutti | last post by:
Hi group, #include <stdio.h> #include <unistd.h> #include <time.h> int main(void) { time_t t1, t2; char *st1, *st2;
1
by: parag_paul | last post by:
I am try ing to use ctime with the type time_t , I used .... time_t t = time(0); char* sc = ctime( &t); printf ("%s\n", sc); ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.