Skip to content

Instantly share code, notes, and snippets.

@artur-kink
Last active August 29, 2015 14:05
Show Gist options
  • Save artur-kink/e90a8b7e54ca7b6cdd73 to your computer and use it in GitHub Desktop.
Save artur-kink/e90a8b7e54ca7b6cdd73 to your computer and use it in GitHub Desktop.
Get current current day time in fractional days format.
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv){
//Get current time
time_t now = time(0);
struct tm* now_tm = localtime(&now);
//Get midnight today
struct tm today;
today.tm_sec = 0;
today.tm_min = 0;
today.tm_hour = 0;
today.tm_year = now_tm->tm_year;
today.tm_mon = now_tm->tm_mon;
today.tm_mday = now_tm->tm_mday;
//Get seconds since midnight today.
double seconds = difftime(now, mktime(&today));
//Get fractional time.
double fraction = seconds/(60*60*24);
printf("%f\n", fraction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment