Thread: Formatting SYSTEMTIME

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Formatting SYSTEMTIME

    Hello,

    I am having a hard time displaying the current time (with AM/PM and putting the correct '0' padding.

    12:01 PM
    7:08 AM
    2:26 PM

    Code:
            SYSTEMTIME st;
            GetLocalTime(&st);
    
            WORD hour = st.wHour;
            WORD minute = st.wMinute;
            WORD seconds = st.wSecond;
    
            TCHAR _time[MAX_PATH] = TEXT("\0");
    
            wsprintf(_time, TEXT("%d:%1d %1d"), hour, minute, seconds);
    The code above does not place the '0' for a single digit minute and also does not display AM PM

    How can i do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Normally, you would use a format like "%02d" to print values from 00 to 59 say.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    What about the AM/PM format?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could use standard functions instead of windows functions:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
        struct tm *timeinfo;
        char buffer[80];
        time_t tim = time(NULL);
    
        timeinfo = localtime(&tim);
        strftime(buffer, 80, "%I:%M%p", timeinfo);
        puts(buffer);
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by EverydayDiesel View Post
    The code above does not place the '0' for a single digit minute and also does not display AM PM
    Why would you want that in the first place? The API returns the time in 24h format.
    Also, in C++, you would do it this way to avoid system-dependent calls (code being entirely portable):

    Code:
    #include <iostream>
    #include <ctime>
    #include <chrono>
    
    int main()
    {
    	auto today = std::chrono::system_clock::now();
    	auto tt = std::chrono::system_clock::to_time_t(today);
    	std::cout << "Today is: " << ctime(&tt);
    	return 0;
    }
    Also, in C++, do not use wsprintf and do not use char/wchar_t/TCHAR. You are strongly recommended to use std::string/std::wstring and std::stringstream/std::wstringstream.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    Quote Originally Posted by Elysia View Post
    Why would you want that in the first place? The API returns the time in 24h format.
    Also, in C++, you would do it this way to avoid system-dependent calls (code being entirely portable):

    Code:
    #include <iostream>
    #include <ctime>
    #include <chrono>
    
    int main()
    {
        auto today = std::chrono::system_clock::now();
        auto tt = std::chrono::system_clock::to_time_t(today);
        std::cout << "Today is: " << ctime(&tt);
        return 0;
    }
    Also, in C++, do not use wsprintf and do not use char/wchar_t/TCHAR. You are strongly recommended to use std::string/std::wstring and std::stringstream/std::wstringstream.


    I dont want the format to be in 24 hour format. I want to display as 1 2 3 4 5 6 7 8 9 10 11 12 1PM 2PM 3PM etc

    the code above tells me it is unsafe?

    Code:
    error C4996: 'ctime': This function or variable may be unsafe. Consider using ctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS
    Changing to ctime_s creates more errors because it is looking for a buffer, buff length and time
    Last edited by EverydayDiesel; 01-27-2014 at 04:03 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    #include <ctime>
    #include <chrono>
    #include <iomanip>
    
    int main()
    {
    	auto today = std::chrono::system_clock::now();
    	auto tt = std::chrono::system_clock::to_time_t(today);
    	auto * localtime = std::localtime(&tt);
    	std::cout << "Today is: " << std::put_time(localtime, "%Y-%m-%d %I:%M:%S %p") << "\n"; // 12h clock
    	std::cout << "Today is: " << std::put_time(localtime, "%Y-%m-%d %H:%M:%S") << "\n"; // 24h clock
    	return 0;
    }
    Seeing as localtime, ctime, etc are C functions, they are unsafe by nature. Hence Microsoft made safer versions of them because they required extra parameters (or input information) to be made safer.
    Ideally, you should use them, but beware that they aren't portable (they won't compile with anything but Microsoft's compiler), so it's really up to you. You must learn to use them correctly, though, or they're no safer than the standard C functions.
    It is possible to easily map these "newer" functions to the older, unsafer versions by creating wrapper functions if you're going to port it, too, just so you know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Actually since ctime() is considered by some to obsolete even in C, you should be using the strftime() function instead. And just because a function has C origins does not in it's self make a function unsafe.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between 2 SYSTEMTIME's
    By X PaYnE X in forum Windows Programming
    Replies: 4
    Last Post: 11-19-2005, 05:25 AM
  2. Utter confusion, resulting from SYSTEMTIME
    By Tronic in forum Windows Programming
    Replies: 8
    Last Post: 01-04-2005, 08:21 PM
  3. Writing FILETIME or SYSTEMTIME to File?
    By Tojam in forum Windows Programming
    Replies: 2
    Last Post: 09-23-2003, 01:55 AM
  4. Converting SYSTEMTIME to struct tm
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 06:17 AM
  5. Formatting
    By MethodMan in forum Tech Board
    Replies: 6
    Last Post: 10-19-2002, 09:38 PM