Get Unix Timestamp with C++

How do I get the unix timestamp in C as an int?

For 32-bit systems:

fprintf(stdout, "%u\n", (unsigned)time(NULL)); 

For 64-bit systems:

fprintf(stdout, "%lu\n", (unsigned long)time(NULL)); 

how to convert datetime to unix timestamp in c?

You could try a combination of strptime and mktime

struct tm tm;
time_t epoch;
if ( strptime(timestamp, "%Y-%m-%d %H:%M:%S", &tm) != NULL )
epoch = mktime(&tm);
else
// badness

How to get the unix timestamp in C#

You get a unix timestamp in C# by using DateTime.UtcNow and subtracting the epoch time of 1970-01-01.

e.g.

Int32 unixTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

DateTime.UtcNow can be replaced with any DateTime object that you would like to get the unix timestamp for.

There is also a field, DateTime.UnixEpoch, which is very poorly documented by MSFT, but may be a substitute for new DateTime(1970, 1, 1)

Get Unix timestamp with C++

C++20 introduced a guarantee that time_since_epoch is relative to the UNIX epoch, and cppreference.com gives an example that I've distilled to the relevant code, and changed to units of seconds rather than hours:

#include <iostream>
#include <chrono>

int main()
{
const auto p1 = std::chrono::system_clock::now();

std::cout << "seconds since epoch: "
<< std::chrono::duration_cast<std::chrono::seconds>(
p1.time_since_epoch()).count() << '\n';
}

Using C++17 or earlier, time() is the simplest function - seconds since Epoch, which for Linux and UNIX at least would be the UNIX epoch. Linux manpage here.

The cppreference page linked above gives this example:

#include <ctime>
#include <iostream>

int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result))
<< result << " seconds since the Epoch\n";
}

How to get a Unix timestamp for C (in double or float)

The time() function returns an integer only. Both
gettimeofday() and
clock_gettime() return structures (different structures) with seconds and subseconds (microseconds for gettimeofday() and nanoseconds for clock_gettime()). You'd have to do an appropriate (but simple) computation to return that as a double.

There's no point in returning a current Unix timestamp as a float; you'd only get values accurate to a couple of minutes or so.

POSIX has officially deprecated gettimeofday(). However, Mac OS X for one does not have clock_gettime(), so you're likely to find gettimeofday() is more widely available.

#include <time.h>
#include <stdio.h>

int main(void)
{
long t0 = time(0);
float f0 = t0;
float f1;
float f2;

long d_pos = 0;
long d_neg = 0;

while ((f1 = t0 + d_pos) == f0)
d_pos++;
while ((f2 = t0 + d_neg) == f0)
d_neg--;

printf("t0 = %ld; f0 = %12.0f; d_pos = %ld (f1 = %12.0f); d_neg = %ld (f2 = %12.0f)\n",
t0, f0, d_pos, f1, d_neg, f2);
return 0;
}

Sample output:

t0 = 1385638386; f0 =   1385638400; d_pos = 79 (f1 =   1385638528); d_neg = -51 (f2 =   1385638272)

Convert current time from windows to unix timestamp in C or C++

Maybe my question was phrased badly: All I wanted was to get the current time on a windows machine as a unix timestamp.
I now figured it out myself (C language, Code::Blocks 12.11, Windows 7 64 bit):

#include <stdio.h>
#include <time.h>
int main(int argc, char** argv) {
time_t ltime;
time(<ime);
printf("Current local time as unix timestamp: %li\n", ltime);

struct tm* timeinfo = gmtime(<ime); /* Convert to UTC */
ltime = mktime(timeinfo); /* Store as unix timestamp */
printf("Current UTC time as unix timestamp: %li\n", ltime);

return 0;
}

Example output:

Current local time as unix timestamp: 1386334692
Current UTC time as unix timestamp: 1386331092


Related Topics



Leave a reply



Submit