Question about SYSTEMTIME

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ralph

    #1

    Question about SYSTEMTIME

    Hi,
    I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
    it?
    I start like that:

    SYSTEMTIME time_old, time_now;

    GetLocalTime(&t ime_old);
    GetLocalTime(&t ime_now);

    And now I must have in time_old the SYSTEMTIME - 15 minutes, is there
    any implemented function in C to do in (it must work in windows not in
    unix).

    I'll be very gratefull for any sugestions.

    Thank you very much for help

    Ralph

  • Keith Thompson

    #2
    Re: Question about SYSTEMTIME

    "ralph" <rklimski@o2.pl writes:
    I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
    it?
    I start like that:
    >
    SYSTEMTIME time_old, time_now;
    >
    GetLocalTime(&t ime_old);
    GetLocalTime(&t ime_now);
    >
    And now I must have in time_old the SYSTEMTIME - 15 minutes, is there
    any implemented function in C to do in (it must work in windows not in
    unix).
    There is no SYSTEMTIME type or GetLocalTime() function in standard C.
    Since you say the problem is Windows-specific, you need to ask in a
    Windows-specific newsgroup.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • MQ

      #3
      Re: Question about SYSTEMTIME


      ralph wrote:
      Hi,
      I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
      it?
      I start like that:
      >
      SYSTEMTIME time_old, time_now;
      >
      GetLocalTime(&t ime_old);
      GetLocalTime(&t ime_now);
      Did you look at the format of the SYSTEMTIME structure? It has fields
      like wHour, wMinute, wSecond, etc. I imagine the code would be
      something like

      if(time_old.wMi nute < 15)
      {
      time_old.wHour--;
      time_old.wMinut e = 45 + time_old.wMinut e;
      /* not complete, more must be added */
      }
      else
      {
      time_old.wMinut e -= 15;
      }

      I'll leave it as an exercise for you to add the rest of the code for
      handling day, week, month, year, etc boundaries, which is actually not
      that trivial... Consult MSDN for the details of the SYSTEMTIME
      structure. And before you get bombarded by cranky clc regulars about
      the inappropriatene ss of the post in this purely C newsgroup, I would
      suggest posting platform-specific (eg Windows) questions on an MS
      forum. I don't have a problem with it, but you will find some do... :)

      cheers
      MQ

      Comment

      • Ian Malone

        #4
        Re: Question about SYSTEMTIME

        MQ wrote:
        ralph wrote:
        >Hi,
        >I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
        >it?
        >I start like that:
        >>
        >SYSTEMTIME time_old, time_now;
        >>
        >GetLocalTime(& time_old);
        >GetLocalTime(& time_now);
        >
        Did you look at the format of the SYSTEMTIME structure? It has fields
        like wHour, wMinute, wSecond, etc. I imagine the code would be
        something like
        >
        if(time_old.wMi nute < 15)
        {
        time_old.wHour--;
        time_old.wMinut e = 45 + time_old.wMinut e;
        /* not complete, more must be added */
        }
        else
        {
        time_old.wMinut e -= 15;
        }
        >
        I'll leave it as an exercise for you to add the rest of the code for
        handling day, week, month, year, etc boundaries, which is actually not
        that trivial... Consult MSDN for the details of the SYSTEMTIME
        structure. And before you get bombarded by cranky clc regulars about
        the inappropriatene ss of the post in this purely C newsgroup, I would
        suggest posting platform-specific (eg Windows) questions on an MS
        forum. I don't have a problem with it, but you will find some do... :)
        >
        As an alternative either:
        1. Use whatever routine this platform has for converting SYSTEMTIME
        to a single number (in seconds, milliseconds or whatever) and
        subtract the right amount, then convert back.
        2. Use the C function time to get the time in seconds, subtract
        15*60, use localtime to convert the result to struct_tm.

        --
        imalone

        Comment

        • MQ

          #5
          Re: Question about SYSTEMTIME


          Ian Malone wrote:
          As an alternative either:
          1. Use whatever routine this platform has for converting SYSTEMTIME
          to a single number (in seconds, milliseconds or whatever) and
          subtract the right amount, then convert back.
          2. Use the C function time to get the time in seconds, subtract
          15*60, use localtime to convert the result to struct_tm.
          Yes, this is probably easier. Let the OS/libraries do the work

          MQ

          Comment

          • Keith Thompson

            #6
            Re: Question about SYSTEMTIME

            Ian Malone <ibm21@cam.ac.u kwrites:
            [snip]
            As an alternative either:
            1. Use whatever routine this platform has for converting SYSTEMTIME
            to a single number (in seconds, milliseconds or whatever) and
            subtract the right amount, then convert back.
            2. Use the C function time to get the time in seconds, subtract
            15*60, use localtime to convert the result to struct_tm.
            The latter assumes that time_t represents the time in seconds. It
            commonly does, but there is no such guarantee (or implication) in the
            C standard.

            If you need portability, you can convert a time_t value to a struct tm
            using localtime(), manipulate the members of the struct tm, then
            convert it back using mktime().

            If you don't care about absolute portability (e.g., if your platform
            happens to make guarantees about the representation of time_t, and
            your code is already platform-specific anyway), you can probably get
            away with performing arithmetic directly on a time_t. Or you can
            consult your system's documentation for solutions.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            Working...