15

I am working with a function in C++ to help get the integer for the month. I did some searching and found one that uses localtime but I do not want to set it up to remove warnings so I need to use localtime_s. but when I use that my pointer no longer works and I need someone to help me find what I am missing with the pointer.

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <Windows.h>
#include "FolderTask.h"
#include <ctime> //used for getMonth
#include <string>
#include <fstream>

int getMonth()
{
    struct tm newtime;
    time_t now = time(0);
    tm *ltm = localtime_s(&newtime,&now);
    int Month = 1 + ltm->tm_mon;
    return Month;
}

the error I am getting is:

error C2440: 'initializing': cannot convert from 'errno_t' to 'tm *' note: Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

kevin haysmer
  • 171
  • 1
  • 1
  • 5
  • Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You should also learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). And are you using the Windows [`localtime_s`](https://msdn.microsoft.com/en-us/library/a442x3ye.aspx) function, or the [`localtime_s`](http://en.cppreference.com/w/c/chrono/localtime) from the C standard library? Those two are different. Please elaborate on the problems you have, please show us the possible error messages you get when you build. *Elaborate!* – Some programmer dude Feb 07 '16 at 19:58
  • So it's the [Windows Visual Studio extension `localtime_s`](https://msdn.microsoft.com/en-us/library/a442x3ye.aspx) you use. Read the reference, check and read about what the function returns, – Some programmer dude Feb 07 '16 at 20:08

1 Answers1

27

It looks like you're using Visual C++, so localtime_s(&newtime,&now); fills in the newtime struct with the numbers you want. Unlike the regular localtime function, localtime_s returns an error code.

So this is a fixed version of the function:

int getMonth()
{
    struct tm newtime;
    time_t now = time(0);
    localtime_s(&newtime,&now);
    int Month = 1 + newtime.tm_mon;
    return Month;
}
404
  • 8,022
  • 2
  • 27
  • 47
Christopher Oicles
  • 3,017
  • 16
  • 11
  • Thank you that was what i needed. I didn't relies that the pointer with the needed information became newtime and couldn't understand that. – kevin haysmer Feb 07 '16 at 23:26
  • If you're ignoring the error result though, you might as well not bother with the `_s` version. –  Feb 08 '16 at 01:35
  • 1
    The non-_s version returns a pointer to a C-runtime static buffer, which is sort of crappy. But, Kevin's main reason for using it was to get rid of MSVC's deprecation warning without fussing with the `_CRT_SECURE_NO_DEPRECATE` definition. And anyway, the only error condition localtime_s ever returns is EINVAL (invalid argument) -- so the user has to pass crap arguments to see this -- you won't get an error condition because the runtime or OS surprised you with something outside of your control, like a low memory condition or something. – Christopher Oicles Feb 08 '16 at 05:41
  • Agreed in principle, but I've seen too often compiler warnings about unsafe methods (regardless of whether MS is right to label `localtime` as such) suppressed in a way that keeps the code equally unsafe, but makes it pretty much impossible for static analysis to detect it as unsafe. I consider that a very dangerous practice worth warning about. –  Feb 08 '16 at 07:57
  • Very helpful--I didn't realize initially that localtime_s and gmtime_s were so diffreent from their localtime/gmtime counterparts. – KBurchfiel Aug 19 '20 at 01:37