Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Created December 21, 2018 17:00
Show Gist options
  • Save santiago-salas-v/700494d4f76c495aab363e29913e6f0f to your computer and use it in GitHub Desktop.
Save santiago-salas-v/700494d4f76c495aab363e29913e6f0f to your computer and use it in GitHub Desktop.
schedule log wake (win scheduler - system - power-troubleshooter - event id 1)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
int file_exists(const char *filename){
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
int main(){
time_t current_time;
const struct tm* struct_current_time;
FILE *fp;
char *c_time_string;
const char err_msg[] = "Failure to obtain current time.\n";
const char log_file_name[] = "log_wake.log";
const char sep[] = "sep=|\n";
const char time_output_fields[] = "time_str|year|month|day|hour|minute|second|weekday|daylight";
const char time_output[] = "%i|%i|%i|%i|%i|%i|%i|%i\n";
char s_out[256];
// obtain current time
current_time = time(NULL);
if(current_time == ((time_t) - 1)){
fprintf(stderr, err_msg);
}
// convert time to local time format
struct_current_time = localtime(&current_time);
c_time_string = asctime(struct_current_time);
if(c_time_string == NULL){
fprintf(stderr, err_msg);
}
// print to stdout
printf("Current time: %s", c_time_string);
sprintf(
s_out, time_output,
struct_current_time->tm_year + 1900, // year since 1900
struct_current_time->tm_mon + 1, // months since Jan [0,11]
struct_current_time->tm_mday + 0, // day of the month [1,31]
struct_current_time->tm_hour + 0, // hours since midnight [0,23]
struct_current_time->tm_min + 0, // min after the hour [0,59]
struct_current_time->tm_sec + 0, // sec after the minute [0, 60]
struct_current_time->tm_wday + 0, // days since Sunday [0, 6],
struct_current_time->tm_isdst // daylight savings bool
);
// append date struct to log_wake.log
if(file_exists(log_file_name)){
// append mode
fp = fopen(log_file_name, "a");
}else{
//create / write mode
fp = fopen(log_file_name, "w");
fprintf(fp, sep);
fprintf(fp, time_output_fields);
fputc('\n', fp);
}
for(int i=0; c_time_string[i]!='\n' && c_time_string[i]!='\0'; i++){
fputc(c_time_string[i], fp);
}
fputc('|', fp);
fprintf(fp, s_out);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment