seconds to day, month, week, day of the week etc..

Hello everyone!

I have server time
int time
It should show what time it is right now on that server.
I should also someone extract all those informations like
day of the week, year etc.. from that variable.

here's how i calculate: day of the week, hour and minutes
[php]
// 0- Sunday, 1 - monday ... 6 -Saturday
inline int GetDay(int time)
{
int d = (time % 604800) / 86400;
d += 4;
if (d > 6) d = d - 7;
return d;
}

inline int GetHour(int time)
{
return (time % 86400) / 3600;
}

inline int GetMinute(int time)
{
return (time % 3600) / 60;
}
[php]

Now the question is, how to get:
month of the year (1-12)
year (2015 for example)
day of the month ( 1-31 )


Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>

std::string current_time()
{
    // http://en.cppreference.com/w/cpp/chrono/c/time
    const std::time_t now = std::time(nullptr) ; // typically, number of seconds since January 01 1970 00:00:00 UTC (Unix time)
    
    // http://en.cppreference.com/w/cpp/chrono/c/gmtime 
    const std::tm curr_time_uct = *std::gmtime( std::addressof(now) ) ;

    std::ostringstream stm ;
    
    // http://en.cppreference.com/w/cpp/io/manip/put_time
    static const char format[] = "%A, %d %B %Y %H:%M:%S UCT" ; // adjust as required
    stm << std::put_time( std::addressof(curr_time_uct), format ) ;
    
    return stm.str() ;
}

int main()
{
    std::cout << current_time() << '\n' ; // Monday, 28 December 2015 03:05:10 UCT
}

http://coliru.stacked-crooked.com/a/01f88705247fd696
Topic archived. No new replies allowed.