0

I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific date value, I am doing calculations and have number of hours to add or subtract.

So I don't know:

  • how can I initialize tm struct with the date value I have?
  • how to add or subtract hours in tc struct variable to get required date?

my intention is
Date received "24-9-2016 13:30" and 5 Hours to add
so final Date: "24-9-2016 18:30"

//Temporarily init time to local

time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
.
.
.
 //Not sure how can I subtract or add hours in this struct to get desired date value

This is about formatting user defined not a duplicate.

user987316
  • 894
  • 4
  • 13
  • 35
  • 1
    Possible duplicate of [How to calculate UTC offset from IANA timezone name in C](http://stackoverflow.com/questions/26845638/how-to-calculate-utc-offset-from-iana-timezone-name-in-c) – David C. Rankin Sep 24 '16 at 08:59
  • If you want an industrial-strength solution, it is here: http://userguide.icu-project.org/datetime/timezone/examples - libicu is the standard way in C to convert times between arbitrary time zones. – John Zwinck Sep 24 '16 at 09:00
  • Not really, date is coming from user and need to perform additional steps, main thing is how can I add or subtract from into a struct tm. – user987316 Sep 24 '16 at 09:02
  • Is the time zone you need the system time zone, or some arbitrary one the user chooses? – John Zwinck Sep 24 '16 at 09:04
  • that detailed calculation is in place, which gives how many hours should be added/subtracted in a user given date. I am not sure how to apply those values in struct tm. – user987316 Sep 24 '16 at 09:05
  • Possible duplicate of [Addition some interval to tm structs](http://stackoverflow.com/questions/4214429/addition-some-interval-to-tm-structs) – melpomene Sep 24 '16 at 09:12
  • Yes, all these are integers, so I can perform addition or subtraction, but it is not handled well in below cases- if hours are added Like 4.5 then 13 hours should become 18:30 Or more than 24 then date should modify In above cases I will need to add additional code depending on hours value, which is bad, isn't there any system method which will modify entire structure depending on the value assigned – user987316 Sep 24 '16 at 09:33

1 Answers1

4
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    struct tm tm = {0};
    if (!strptime("24-9-2016 13:30", "%d-%m-%Y %H:%M", &tm)) {
        return EXIT_FAILURE;
    }

    tm.tm_hour += 5;
    tm.tm_isdst = -1;
    mktime(&tm);

    char buf[40];
    if (!strftime(buf, sizeof buf, "%d-%m-%Y %H:%M", &tm)) {
        return EXIT_FAILURE;
    }

    printf("result: %s\n", buf);
    return EXIT_SUCCESS;
}

Points of note:

  • We initialize tm to all zeroes, then use strptime to parse the input string.
  • Adding 5 hours is as simple as tm_hour += 5.
  • We set tm_isdst to -1 to tell mktime to figure out automatically whether Daylight Saving Time should be in effect.
  • We call mktime(&tm) afterwards to normalize the time struct (e.g. adding 5 hours to 23:30 should result in 04:30 (and incrementing the day), not 28:30).
  • We convert the result back into human readable form with strftime.

A possible issue is that this will output 24-09-2016 18:30, i.e. it will pad the month/day numbers to 2 places using zeroes. If you don't want that, you'll have to print/format the tm fields manually.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Yes, that is something I want to do. Thanks @melpomene. One thing is, my code is going to run in solaris and i don't think strptime() is available there. Is there any alternative there? or I will have to write my own code for what is done inside strptime? – user987316 Sep 24 '16 at 09:49
  • @user987316 According to http://docs.oracle.com/cd/E23824_01/html/821-1465/strptime-3c.html#scrolltoc, `strptime` is available. – melpomene Sep 24 '16 at 09:52
  • If I have to add hour value such as 4.5, how can I do that? tm_hour is int. – user987316 Sep 24 '16 at 10:38
  • @user987316 Add 4 to `tm_hour` and 30 to `tm_min`. – melpomene Sep 24 '16 at 10:46
  • 1
    @user987316 Alternative to adding 4 to `tm_hour` and 30 to `tm_min` 1) add 4*60+30 to `tm_min` or 2) add (4*60+30)*60 to `tm_sec`. `mktime()` will adjust the fields as needed. – chux - Reinstate Monica Sep 24 '16 at 19:50
  • Thanks @melpomene, unfortunately mktime() isn't adjusts seconds values to update minute and hours, not sure if I am doing anything wrong. anyways really Thanks for the extended help. – user987316 Sep 26 '16 at 05:36