Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Working with date_t and time_t

Working with date_t and time_t

Scheduled Pinned Locked Moved Managed C++/CLI
questiontutorial
11 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F for study

    I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific timezone, I am doing calculations and have number of hours to add or substract. So I don't know: how can I initialize tm struct with the date value I have? how to add or substract 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 struc to get desired date value

    please provide inputs.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    I am not sure what this has to do with Managed C++. However, the time, _time32, _time64[^] function describes the content of a time_t value, which you can easily initialise with a user defined time. I have not come across a date_t type, where is it defined?

    F 1 Reply Last reply
    0
    • L Lost User

      I am not sure what this has to do with Managed C++. However, the time, _time32, _time64[^] function describes the content of a time_t value, which you can easily initialise with a user defined time. I have not come across a date_t type, where is it defined?

      F Offline
      F Offline
      for study
      wrote on last edited by
      #3

      Sorry, if I used wrong group to post. I have updated post with some of the steps I tried, please check and let me know if is there any solution available.

      1 Reply Last reply
      0
      • F for study

        I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific timezone, I am doing calculations and have number of hours to add or substract. So I don't know: how can I initialize tm struct with the date value I have? how to add or substract 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 struc to get desired date value

        please provide inputs.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Since all those fields are integers you can add, subtract or replace any of them. You should check that the values are valid in terms of the calendar, although the mktime, _mktime32, _mktime64[^] functions will attempt to normalise a tm structure into a valid time. There are many other useful functions, and you should study the documentation to see which ones will help you.

        F 1 Reply Last reply
        0
        • L Lost User

          Since all those fields are integers you can add, subtract or replace any of them. You should check that the values are valid in terms of the calendar, although the mktime, _mktime32, _mktime64[^] functions will attempt to normalise a tm structure into a valid time. There are many other useful functions, and you should study the documentation to see which ones will help you.

          F Offline
          F Offline
          for study
          wrote on last edited by
          #5

          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

          L 1 Reply Last reply
          0
          • F for study

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            If you want to add partial hours, days etc. Then use mktime (as I advised in my previous comment) to convert your tm structure into a time_t value. you can then just add or subtract the number of seconds in your time increment.

            F 1 Reply Last reply
            0
            • L Lost User

              If you want to add partial hours, days etc. Then use mktime (as I advised in my previous comment) to convert your tm structure into a time_t value. you can then just add or subtract the number of seconds in your time increment.

              F Offline
              F Offline
              for study
              wrote on last edited by
              #7

              ok, so here is what I did now-

              //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;
              //completed it...

              int hoursToAdd = 4.5;

              initStruct->tm_hour = initStruct->tm_hour + hoursToAdd;

              time_t myTime = mktime(initStruct);//this return -1

              printf( "%s", ctime( &myTime));//this prints null..

              any idea whats going wrong here?

              L 1 Reply Last reply
              0
              • F for study

                ok, so here is what I did now-

                //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;
                //completed it...

                int hoursToAdd = 4.5;

                initStruct->tm_hour = initStruct->tm_hour + hoursToAdd;

                time_t myTime = mktime(initStruct);//this return -1

                printf( "%s", ctime( &myTime));//this prints null..

                any idea whats going wrong here?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                int hoursToAdd = 4.5;

                That is not a valid statement so it will not even compile. You cannot assign a floating point number to an integer variable.

                F 1 Reply Last reply
                0
                • L Lost User

                  int hoursToAdd = 4.5;

                  That is not a valid statement so it will not even compile. You cannot assign a floating point number to an integer variable.

                  F Offline
                  F Offline
                  for study
                  wrote on last edited by
                  #9

                  Yes, my bad. well in that case my original question reopens how can I assign partial hour to tm struct to enable it to give valid date value.

                  L 1 Reply Last reply
                  0
                  • F for study

                    Yes, my bad. well in that case my original question reopens how can I assign partial hour to tm struct to enable it to give valid date value.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    You really (and I mean really) need to go and study the documentation which explains what each field represents. If you have some number of hours and some number of minutes then you need to add them separately to each field. You should also step through your code with the debugger so you can see at each step what are the values of the various fields.

                    F 1 Reply Last reply
                    0
                    • L Lost User

                      You really (and I mean really) need to go and study the documentation which explains what each field represents. If you have some number of hours and some number of minutes then you need to add them separately to each field. You should also step through your code with the debugger so you can see at each step what are the values of the various fields.

                      F Offline
                      F Offline
                      for study
                      wrote on last edited by
                      #11

                      Thank you very much Richard for your kind help. I really appreciate your time and effort.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups