Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Last active April 22, 2021 12:46
Show Gist options
  • Save jedisct1/b7812ae9b4850e0053a21c922ed3e9dc to your computer and use it in GitHub Desktop.
Save jedisct1/b7812ae9b4850e0053a21c922ed3e9dc to your computer and use it in GitHub Desktop.
// CC0 license - No Rights Reserved.
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t now = time(NULL);
struct tm *tm;
int off_sign;
int off;
if ((tm = localtime(&now)) == NULL) {
return -1;
}
off_sign = '+';
off = (int) tm->tm_gmtoff;
if (tm->tm_gmtoff < 0) {
off_sign = '-';
off = -off;
}
printf("%d-%d-%dT%02d:%02d:%02d%c%02d:%02d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
off_sign, off / 3600, off % 3600);
return 0;
}
@psycho23
Copy link

psycho23 commented May 7, 2017

2017-5-7T03:36:50+00:00

$ date +"%A, %B %-d, %Y. %_I:%-M %p %z %Z"
Sunday, May 7, 2017. 3:37 AM +0000 UTC

$ export TZ='America/Los_Angeles'
$ ./a.out ; echo
2017-5-6T20:37:41-07:00
$ date +"%A, %B %-d, %Y. %_I:%-M %p %z %Z"
Saturday, May 6, 2017. 8:37 PM -0700 PDT

@Lukas0025
Copy link

Nice work. Can you add licence (MIT?) please. I want to use it, but i can't.

@jedisct1
Copy link
Author

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment