473,396 Members | 2,121 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,396 software developers and data experts.

Convert string to time_t

1
I have a customer that supplied me with a text file that has some data and dates stored, apparently, as milliseconds since 01/01/1970 12:00a. I need to display the date/time in their application. I believe I can use the localtime routine but that requires the data passed to it to be in time_t format. I currently have it as a string.

One sample number stored in a string 1192034967000. How do I convert this to time_t so I can get the date/time conversion or is there a better way?

Thanks
Oct 12 '07 #1
3 21926
The function sscanf does that, it let you assign variable values from a string.
Oct 12 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
time_t is just an unsigned int. You can convert from a string to time_t using a stringstream.
Expand|Select|Wrap|Line Numbers
  1. string str = "1192034967000";
  2. time_t t;
  3. stringstream ss;
  4. ss << str;
  5. ss >> t;
  6.  
Be aware that time_t in its 32-but form is no good after Jan 18,2038.
Oct 12 '07 #3
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. string str = "1192034967000";
  2. time_t t;
  3. stringstream ss;
  4. ss << str;
  5. ss >> t;
  6.  
This wont work. You have forgotten that time_t is the time in seconds since 01/01/1970 12:00a but the number is the time in milliseconds. Worst if time_t is 32 bit because 1192034967000 takes more than 32 bits to represent it.

You need to drop the last 3 digits first and convert 1192034967 to a time_t.
Oct 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Nimmy | last post by:
Hi, I have a file which has different dates, I want to scanf them as CHAR and convert them to DATE format, how can I do this? Thanks
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
1
by: HMS Surprise | last post by:
I am working with an ARM946ES processor running C under Nucleus. I need to take time and date (year, month, day, hour, minute, second) from an external RTC chip and convert it to time_t to reset...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
7
by: Angus Comber | last post by:
Hello I need to do this conversion but can't seem to find the function. Angus
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? ...
3
by: Jonathan | last post by:
I have taken up C++ programming again and need to write a socket client, so far I am able to create the connection to the socket. As I need a unix timestamp in char to put on a socket in a HTTP...
4
by: PeteOlcott | last post by:
I need to convert time_t to System::DateTime. All of the examples that I found are either in C# or in the obsolete managed C++ syntax. What is the current syntax for converting a (probably 64 bit)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.