The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 01:28:52 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: time/date manipulation  (Read 3309 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: April 21, 2007, 04:13:14 PM »

Just wrote this. It's trivial but it may help someone and I was interested in the use of iomanip which, surprisingly, I haven't realy delved into before.

This prints yesterday's (now - 86400 seconds) date, formatted as I wanted it, could easily be modified to perform a lot of date related tasks........

Code:
#include <time.h>
#include <iostream>
#include <iomanip> // For formatting output

using namespace std;

int main( void )
{
  time_t now;
  struct tm * timeinfo;
  time( &now );
  now -= 86400; // Seconds in a day
  timeinfo = localtime( &now );

  cout << setfill('0' ) << setw( 2 ) << timeinfo->tm_mday << " "
       <<  setw( 2 ) << timeinfo->tm_mon + 1 << " " << setw( 2 )
       << timeinfo->tm_year - 100 << endl;
}

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 21, 2007, 04:29:20 PM »

And thanks Mr. Darkness for kicking off the deeplingo repository...

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #2 on: May 10, 2007, 06:01:55 AM »

iomanip rocks
Its worth remembering though that each time you pass a command into the stream it affects the next object only
so if you do

stream << setw(2) << myInt << myInt2;

myInt will have a width of 2 but myInt2 will have a normal width
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: May 10, 2007, 11:30:05 PM »

Exactly MonkeyM, traps for young players huh?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #4 on: May 11, 2007, 01:20:12 AM »

Traps for young players?
I am young - 24 lol
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #5 on: May 11, 2007, 05:42:28 AM »

And I ain't (41) but I fell for it too. New to iomanip I mean't  Wink

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #6 on: May 11, 2007, 06:15:21 AM »

lol ahh
 Praise

everyones a new guy to something.
Logged

I am Tyler Durden
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!