473,654 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

do the equivalent of unix date(1) in C

Let's say you don't have any more documentation than man pages, and
you want to figure out how to do the equivalent of
$ date
in C. I.e. just simply print out the current date and time. How to
proceed thru the forest of date and time related man pages?
Nov 13 '05 #1
2 2498
>Let's say you don't have any more documentation than man pages, and
you want to figure out how to do the equivalent of
$ date
in C. I.e. just simply print out the current date and time. How to
proceed thru the forest of date and time related man pages?


1. Call time() to get the current time as a time_t.

#include <time.h>
time_t now;
now = time((time_t *)0);

2. Call localtime() or gmtime() to convert the time_t into a struct tm which
contains the time in more human-understandable terms (month, day, hour, etc).
struct tm *tp;

tp = localtime(&now) ;
3. Call asctime() or strftime() to format the string the way you want it.
4. printf() the string.

It is possible to combine step 2 and 3 with ctime(), whose format rather
resembles the UNIX date command output except for not including a time zone.

However, getting a time zone name to put in the date command output
has to be done unportably. Some implementations of strftime() may
support %Z for this.

Gordon L. Burditt
Nov 13 '05 #2
Groovy hepcat Dan Jacobson was jivin' on Fri, 31 Oct 2003 12:53:09
+0800 in comp.lang.c.
do the equivalent of unix date(1) in C's a cool scene! Dig it!
Let's say you don't have any more documentation than man pages, and
you want to figure out how to do the equivalent of
$ date
in C. I.e. just simply print out the current date and time. How to
proceed thru the forest of date and time related man pages?


Please do your own homework. This is an extremely easy assignment.
I'm sure you can figure it out if you could be bothered to try.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3287
by: pekka niiranen | last post by:
Does anybody know Python recipe for changing the date of the directory or files in W2K to current date and time? In UNIX shell command "touch" does it. -pekka-
0
1833
by: Casey | last post by:
Hi! I found several resources describing how to convert the UNIX timestamp to a readable date, however I need to convert a date into a UNIX timestamp. For instance, I'm writing a program that will be querying an Oracle database, and I need to select data that falls between a certain date. All the dates are stored as UNIX timestamps. So, I need a way to convert a date - like April 1st, 2005 - to the unix timestamp... I thought to use the...
5
4687
by: Tom | last post by:
A field in a data set I want to import into Access is in Unix time (seconds from a certain time on a certain date). Does anyone know the precise date and the precise time on that date that Unix is based on? How do I calculate the Date when I have Unix seconds? Will the calculation take into account leap years? Thanks! Tom
1
2382
by: MLH | last post by:
In an Access 97 form, I have a textbox control with the following code that runs AfterUpdate... Option Compare Database Option Explicit Private Sub UNIXdate_AfterUpdate() Me!RealDate = DateAdd("s", , #1/1/1970#) End Sub
4
3606
by: Alan | last post by:
Is anyone know the equivalent term used for Dynamic Link Library (DLL) in unix environment?? Thanks Alan
4
2224
by: groleo | last post by:
Hi. I'm trying to port a piece of code, written for win32, that uses the finddata_t structure. Is there an equivalent , or a work-around, that implement finddata_t on *nix platforms?
6
34083
by: marc | last post by:
hi im trying to convert Date() into a unix timestamp so i can stick the result into a mysql db, please help!
9
7236
by: Aditi | last post by:
I am working around a problem called Y2038 bug. http://groups.google.co.in/group/comp.unix.programmer/browse_thread/thread/a2f678e4f2761fb0/2816aaf1f50f863e?hl=en&lnk=st&q=time.h+64+bit+linux#2816aaf1f50f863e I am developing an application which need to be built both on windows and linux and used time_t, ctime, mktime, localtime and gmtime functions from <time.h>. The application already exists and I have to replace these 32bit time...
0
13326
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed programatically either from UNIX or Oracle PLSQL. In this Section, I will be explaining about calling a Concurrent program from UNIX using the CONCSUB Command. Pre-requisite: 1. Concurrent Program should be registered in oracle Applications...
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.