c - Converting String from strftime to int/long -


the goal function, delivers int (or long) actual time. therefor write localtime string (as far understood this) , try convert atoi/atol int/long. somehow don't expected result.

things i've tried:

  • i switched initial int long.
  • i changed size of string. if resize 7 instead of needed 15 bytes, converts @ least date part of string. need all.

i've got following code:

long generate_time() {         time_t time1;     time(&time1);      char time_str[15];     long act_time;      strftime(time_str, 15, "%y%m%d%h%m%s", localtime(&time1));     printf("actual time: %s\n", time_str);      act_time = atol(time_str);     printf("transformed time: %d\n", act_time);      return act_time; } 

resulting in response:

actual time: 20170407091221 transformed time: 1240669205

i hope easy sort out , thank in advance!

you cannot use long store such data: max value signed long is:

-2^31+1 +2^31-1 

or

-2,147,483,648 2,147,483,647 

you can use unit64_t need

#include <stdint.h>  [...]  uint64_t act_time = strtoll(time_str, null, 10); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -