473,225 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Reading local time into time_t

I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromString(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_str(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik
Mar 26 '06 #1
8 12300
Henrik Goldman wrote:
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromString(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_str(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik

Check out the date/time library from boost to do this.

It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.

Mar 26 '06 #2
> Check out the date/time library from boost to do this.

Thanks for the suggestion but unfortunatly not usable in my case. Since it's
a library that I write I cannot put boost into it. There are too many
requirements and obstacles to make this into a solution, unfortunatly.
It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.


True. I guess it should be possible to solve by looking at the environment
variables for daylight adjustment and timezone but I doubt I'm the first one
to have come into this problem. Therefore I thought someone else could
perhaps point me to a website that shows such a solution.

Thanks in advance.
-- Henrik
Mar 26 '06 #3

Henrik Goldman wrote:
Check out the date/time library from boost to do this.


Thanks for the suggestion but unfortunatly not usable in my case. Since it's
a library that I write I cannot put boost into it. There are too many
requirements and obstacles to make this into a solution, unfortunatly.
It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.


True. I guess it should be possible to solve by looking at the environment
variables for daylight adjustment and timezone but I doubt I'm the first one
to have come into this problem. Therefore I thought someone else could
perhaps point me to a website that shows such a solution.

Thanks in advance.
-- Henrik

Why can't you "put" boost into a library you write? Who or what
is putting this constraint on you?

Mar 26 '06 #4
Henrik Goldman wrote:
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromString(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_str(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik

A quick look at the various time functions yields this algorithm:

Just adjust your time_t result returned from mktime() by the time
difference between local and UTC time. This difference is fixed except
when you cross into (or out of) daylight savings time.

You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.

Mar 26 '06 #5
> You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.


Thanks. This sounds like a good idea. Which time do you suggest I should use
those first functions on? I guess I should use the un-adjusted time_t,
right?

Thanks.
-- Henrik
Mar 27 '06 #6
> Why can't you "put" boost into a library you write? Who or what
is putting this constraint on you?


It's a static library that I'm creating. In other words it's linked at the
client site together with code that I have no control over.
This means if they use boost as well then there will eventually be trouble
(unless I would rename the namespace so I get my own "version" of boost).
Besides this it adds extra code in there which is not a good idea (esp.
since it's planned to be ported to embedded OS's in future). Last but not
least it makes it harder to port the code for new compilers. I will only be
able to compile my code if boost supports the platform. I don't say it's a
realistic problem since I know they do alot of platform support, but still
it's something that I need to worry about.

Those requirements are what users expect of my library, not my own ideas.

-- Henrik
Mar 27 '06 #7
Henrik Goldman wrote:
You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.


Thanks. This sounds like a good idea. Which time do you suggest I should use
those first functions on? I guess I should use the un-adjusted time_t,
right?


I went through this mess a couple of years ago.
You have to make the calls to localtime and gmtime every time, though.

Consider if you take the time differential at the start of your program,
and during your run, daylight savings time starts or ends (possible if
you have a program designed to run for days at a time). Then your time
differential is incorrect after a while. So you need to perform get the
local and gmtime every time you wish to convert a time_t struct. I
personally believe that there should have been a mkgmtime() as the
inverse of gmtime().
Mar 27 '06 #8
red floyd <no*****@here.dude> wrote:
Consider if you take the time differential at the start of your program,
and during your run, daylight savings time starts or ends (possible if
you have a program designed to run for days at a time). Then your time
differential is incorrect after a while. So you need to perform get the
local and gmtime every time you wish to convert a time_t struct.


I'm not sure if DST is standardized, but if it is, you could cache the
value and only update if you see a date that is across the DST status
change.

Well, I just checked and a struct tm does have a field tm_isdst, so
maybe this could be queried to see if you need to update the correction
factor.

--
Marcus Kwok
Mar 28 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
4
by: Ekong, Samuel Akpan | last post by:
Hi, I am parsing a binary file and intend to read a date(not time) field encoded thus: 0xA1290B(little-endian). Now I know the date to be 12/04/2003 but just cannot get any of the known datetime...
14
by: George | last post by:
In Time.h there is a structure defined for time settings. I'm building an embedded system that has a Real Time Clock but it's not PC compatible. My question is: I don't some elements of the...
6
by: leorulez | last post by:
I was wondering if there is any way in C to read the date and time (either system time or from the keyboard) and see if it falls between certain date and time? I am not sure how to compare the 2...
1
by: Generic Usenet Account | last post by:
I am trying to find the best way to get the difference between UTC and the local time? Here's a small code snippet that I have written for this purpose. Is there a better way to do this? ...
18
by: moni | last post by:
I have 2 time values: System time and an input from the user. 1) System time is in the form of seconds from 1/1/1970 calculated by using
2
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? ...
4
by: Seth7TS | last post by:
Hi, I want to make a trial version of a software but if i read the system clock the user can modify it... how can i prevent this?? i thought to read the time from internet but i don't know if...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.