Hi I have trouble have parse date in this format:
1295716379
I don’t what kind of date format is it.
Human readable value of this string is:
22. 1. 2011, 18.12
Also I don’t know that this format is some cowboy coder format or it is some "standard".
And if it is possible parse string on the top to human readable format, for examle in C#, Java, C++.
Thank
It looks like a unix timestamp.
You can parse them like so:
C#: Parsing unix time in C#
C++: Converting a unix time to a human readable format
Java: Unix epoch time to Java Date object
Further links: Epoch Converter.com.
That's a UNIX Epoch timestamp.
An example in C# to convert it to a DateTime:
DateTime ToDateTime(int seconds)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.ToLocalTime().AddSeconds(seconds);
}
This will convert it into local time.
Verified, it is a unix timestamp.
The time is Sat Jan 22 17:12:59 2011 in UTC.
It looks like you have a local time value and your timezone is UTC+1.
In C/C++:
#define _USE_32BIT_TIME_T
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i = atoi("1295716379");
time_t t = (time_t)i;
puts(ctime( &t ));
tm t_tm = *gmtime(&t);
puts(asctime( &t_tm ));
return 0;
}
Output:
Sun Jan 23 02:12:59 2011
Sat Jan 22 17:12:59 2011
Note that gmtime return UTC time value, localtime return local time value.
PS: I'm living in UTC+9 timezone
Related
I have two input variables: an epoch time in UTC time zone and the name of the actual time zone. How do I get a formatted day/time using moment.js that would take in account the DST changes. I tried this code but it doesn't do the trick. What am I doing wrong, please?
var abs_time = 1611188219.277; // this is UTC coresponding to 1/21/2021 18:31:37 UTC
var timezone = "America/New_York"; // this the actual time zone
var mom = moment(abs_time * 1000).format();
var date_time = moment.tz(mom, timezone).format('ddd, MMM DD YYYY - HH:mm');
console.log(date_time);
//actual result: Thu, Jan 21 2021 - 18:31
//desired result: Thu, Jan 21 2021 - 13:31 - in the summer this should only be 4 hour difference
First, 1611188219.277 actually corresponds to 2021-01-21T00:16:59.277Z, not the time you gave in your question (assuming it is a Unix timestamp with seconds precision). This can be seen with the following code:
const d = new Date(1611188219.277 * 1000);
const s = d.toISOString();
console.log(s);
You can get the equivalent local time in a specific time zone without any libraries, as long as you're satisfied with the output produced by the toLocaleString function.
const d = new Date(1611188219.277 * 1000);
const s = d.toLocaleString(undefined, { timeZone: 'America/New_York' });
console.log(s);
Note that undefined in the above code will use the browser's current language. If you want a specific language, you could pass its language code there instead (such as en or en-US, etc.)
In general, due to its project status, you should avoid using Moment unless it's already being used in an existing project. If however, you must use Moment and Moment-TimeZone for this, you can do the following to get the same result:
const m = moment.tz(1611188219.277 * 1000, 'America/New_York');
const s = m.format('ddd, MMM DD YYYY - HH:mm');
console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data-10-year-range.min.js"></script>
I used the same format from your question, but of course you could change this as desired.
You might also consider using Luxon (the successor to Moment), or Date-fns, or several other libraries.
And yes, all of the above will correctly take daylight saving time into account.
Is there a good way to parse milliseconds since epoch (ex. 1486252500000 13 digits) formatted time into a human readable format?
DateTime does have a named constructor for millisecond since epoch
https://api.dartlang.org/stable/1.24.2/dart-core/DateTime/DateTime.fromMillisecondsSinceEpoch.html
DateTime date = new DateTime.fromMillisecondsSinceEpoch(1486252500000)
If you want to convert it to human readable string, you can use intl package with the DateFormat class
import "package:intl/intl_browser.dart";
var format = new DateFormat("yMd");
var dateString = format.format(date);
time.Date(t.Year(), t.Month(), time.Now().Day(), 10, 0, 0, 0, time.UTC)
I want to set dateTime of 10:00:00 AM in IST format in golang.
It depends on the format of the time you have at hand. Go has some standard time formats ready as consts in the time package, but you can specify your own standard if it's custom. Regarding the time zone, you can parse or output a time in a specific time zone. Here is an example of parsing a time string in IST, and outputting it as UTC. It's not clear from your question what is your precise problem but I hope this helps:
// First, we create an instance of a timezone location object
loc, _ := time.LoadLocation("Asia/Kolkata")
// this is our custom format. Note that the format must point to this exact time
format := "Jan _2 2006 3:04:05 PM"
// this is your timestamp
timestamp := "Jun 25 2015 10:00:00 AM"
// now we parse it, considering it's in IST
t, err := time.ParseInLocation(format, timestamp, loc)
// printing it prints it in IST, but you can set the timezone to UTC if you want
fmt.Println(t, err)
// example - getting the UTC timestamp
fmt.Println(t.UTC())
I am trying to parse .cds delphi database file. Simple int values and strings are easy to parse. But the only one i cannot understand is a DateTime format.
I found 6 bytes that affecting DateTime Value
I am using python and the following code:
data = '\x00\x00' + '\xBC\xCE\x6F\xEC\xE7\xCC'
data_long = struct.unpack('Q', data)[0]
But struct.unpack doesnt have 6 byte type values, so i added \x00 \x00 to make 8 byte long value ('Q' option)
Here is small sample .cds file with one row https://yadi.sk/d/PkZKy50YgCmqE
DateTimeIssl value = "16.04.2015 9:25:47"
I found 6 hex values but cant unpack it properly.
Can anyone tell me how to read it, or maybe give me a link to some documentation about .cds file structure?
Update:
OK! Thanks to Deltics for guide me how to read TDateTime. I found some test values on internet and i wrote decode function that converts it to Python datetime object.
data = '\x2E\xD8\x82\x2D\xCE\x47\xE3\x40'
data_double = struct.unpack('d', data)[0]
double_split = str(data_double).split('.')
SECONDS_IN_DAY = 60*60*24
time_from_starting_date = timedelta(days=int(double_split[0]), seconds=int(SECONDS_IN_DAY * (float(double_split[1]) * pow(0.1, len(double_split[1])))))
starting_date = datetime(1899, 12, 30)
result_date = starting_date + time_from_starting_date
print time_from_starting_date
print result_date
For 2E D8 82 2D CE 47 E3 40 it will be 08.02.2008 10:38:00.
Works fine.
But i still cannot found valid 8-bytes for field DateTimeIssl in file that i've linked above. Maybe there a different datetime format?
A Delphi date/time (TDateTime) is a Double precision floating point. This is an 8-byte value. You should not need to add any packing or null bytes. If you are having to do this then you have not identified the double value correctly in the file.
Looking at the sample CDS you linked to, each value that could sensibly be interpreted as a date/time (e.g. DateRoshd, DateTimeIssl) is followed by 8 bytes of data.
After reading the double value, the whole number part of this value indicates the date as the number of days since 30 Dec 1899. The decimal part is the time of day on that date.
e.g.
1.0 = 31 Dec 1899, 00:00 (midnight)
2.5 = 1 Jan 1900, 12:00 (midday)
More information on the Delphi TDateTime data type can be found here.
Responding to myself. Maybe for someone it will be useful.
In binary format, TClientDataSet DateTime contains INTEGER value of milliseconds since 02.01.0001, but stores as a 8-byte DOUBLE
So you have to read 8-bytes, unpack it as a double, then convert value to integer. Here is Python code that worked for me:
data = '\x00\xBC\xCE\x6F\xEC\xE7\xCC\x42' # Time: 2015-04-16 09:25:47
data_double = struct.unpack('d', data)[0]
time_from_starting_date = timedelta(days=-2, milliseconds=long(data_double))
starting_date = datetime(0001, 01, 02)
result_date = starting_date + time_from_starting_date
print "Time:", result_date
New to JodaTime library, i would like to get a DateTime's milliseconds field with the specified TimeZone's offset.
So far my attempt is:
private DateTimeZone timeZone = DateTimeZone.forID("Europe/Amsterdam");
private long now=new DateTime().withZone(timeZone).getMillis();
But i always get the UTC millis, the timezone offset isnt applied,
Is there any way to apply the timezone's offset to the DateTime object?
Thx!
First: What do you intend to do with these "local" millis? What do you really try to achieve? Normally only UTC-millis are needed.
Anyway, remember the general timezone offset definition which is:
UTC + Offset = Local Time
Then the solution is simple:
DateTimeZone tz = DateTimeZone.forID("Europe/Amsterdam");
long nowUTC = new DateTime().withZone(tz).getMillis();
long nowLocal = nowUTC + tz.getOffset(nowUTC);
But once again: What is your use-case for "local" millis? They are not even related to UNIX epoch any longer because the UTC-link is cut off.
About your last question ("Is there any way to apply the timezone's offset to the DateTime object?"):
Your DateTime-object already has got a timezone, namely "Europe/Amsterdam". It is internally used to compute the field tuple representation once you have a global UTC-timestamp expressed as millis since UNIX epoch. No need to apply an extra offset on DateTime. It is already there.
JodaTime is using machine time inside. So to find miliseconds, you can use a constant storing LocalDateTime referring to Jan 1, 1970(Because of UNIX Time).
Unix time, or POSIX time, is a system for describing points in time,
defined as the number of seconds elapsed since midnight proleptic
Coordinated Universal Time (UTC) of January 1, 1970, not counting leap
seconds.
Then calculate the difference between your DateTime.
I tried like this;
public static void main(String[] args) {
final LocalDateTime JAN_1_1970 = new LocalDateTime(1970, 1, 1, 0, 0);
DateTime local = new DateTime().withZone(DateTimeZone.forID("Europe/Amsterdam"));
DateTime utc = new DateTime(DateTimeZone.UTC);
System.out.println("Europe/Amsterdam milis :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.forID("Europe/Amsterdam")), local).getMillis());
System.out.println("UTC milis :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.UTC), utc).getMillis());
}
And the result is;
Europe/Amsterdam milis :1429695646528
UTC milis :1429692046534
And #leonbloy write here a good comment.
Your local and utc represent the same instants of time, (only with
different timezones attached). Hence, getMillis() (which gives the
"physical" time interval elapsed from the "instant" corresponding to
the unix epoch), must return the same value.
I will also look for better solution with no constant.