unix-timestamp

How to parse unix timestamp to time.Time

这一生的挚爱 提交于 2019-11-27 17:13:57
I'm trying to parse an Unix timestamp but I get out of range error. That doesn't really makes sense to me, because the layout is correct (as in the Go docs): package main import "fmt" import "time" func main() { tm, err := time.Parse("1136239445", "1405544146") if err != nil{ panic(err) } fmt.Println(tm) } Playground The time.Parse function does not do Unix timestamps. Instead you can use strconv.ParseInt to parse the string to int64 and create the timestamp with time.Unix : package main import ( "fmt" "time" "strconv" ) func main() { i, err := strconv.ParseInt("1405544146", 10, 64) if err !=

Convert Unix timestamp to a date string

雨燕双飞 提交于 2019-11-27 17:00:49
Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line? date might work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work properly. It seems like there might be an easier way - am I missing something? John Kugelman With GNU's date you can do: date -d "@$TIMESTAMP" # date -d @0 Wed Dec 31 19:00:00 EST 1969 (From: BASH: Convert Unix Timestamp to a Date ) On OS X, use date -r . date -r "$TIMESTAMP" Alternatively, use strftime() . It's not available directly from the shell, but

How can I work with dates before 1900 in PHP?

与世无争的帅哥 提交于 2019-11-27 16:04:35
I am using PHP and jQuery to build an interactive timeline which needs to display dates between 1500 and 2020. I usually use PHP's strtotime function when working with dates, but it does not work for dates pre-1900. The dates will come from a MySQL database, and are formatted as strings such as " January 31, 1654 " (this may not be the ideal format, but I can't change how they are stored). I am using PHP to parse the dates, basically converting them into pixel values which determine where they are displayed on the timeline. What is the easiest way to parse these historical dates? Pascal MARTIN

unix timestamp round to midnight

两盒软妹~` 提交于 2019-11-27 15:41:19
问题 If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight. For example if the timestamp is 1324189035 then how can I remove the hours, minutes, and seconds to put the timestamp at midnight for that day. 回答1: echo date('d-m-Y H:i:s', strtotime('today', 1324189035)); 回答2: Because of how you're using it, I wouldn't calculate midnight at all: it is

How to convert strings like “19-FEB-12” to epoch date in UNIX

流过昼夜 提交于 2019-11-27 15:41:11
问题 In UNIX how to convert to epoch milliseconds date strings like: 19-FEB-12 16-FEB-12 05-AUG-09 I need this to compare these dates with the current time on the server. 回答1: To convert a date to seconds since the epoch: date --date="19-FEB-12" +%s Current epoch: date +%s So, since your dates are in the past: NOW=`date +%s` THEN=`date --date="19-FEB-12" +%s` let DIFF=$NOW-$THEN echo "The difference is: $DIFF" Using BSD's date command, you would need $ date -j -f "%d-%B-%y" 19-FEB-12 +%s

How to round unix timestamp up and down to nearest half hour?

淺唱寂寞╮ 提交于 2019-11-27 14:34:49
问题 Ok so I am working on a calendar application within my CRM system and I need to find the upper and lower bounds of the half an hour surrorunding the timestamp at which somebody entered an event in the calendar in order to run some SQL on the DB to determine if they already have something booked in within that timeslot. For example I have the timestamp of 1330518155 = 29 February 2012 16:22:35 GMT+4 so I need to get 1330516800 and 1330518600 which equal 16:00 and 16:30. If anyone has any ideas

How do I deserialize timestamps that are in seconds with Jackson?

こ雲淡風輕ζ 提交于 2019-11-27 13:55:35
I've got some JSON that has timestamps in seconds (i.e. a Unix timestamp): {"foo":"bar","timestamp":1386280997} Asking Jackson to deserialize this into an object with a DateTime field for the timestamp results in 1970-01-17T01:11:25.983Z , a time shortly after the epoch because Jackson is assuming it to be in milliseconds . Aside from ripping apart the JSON and adding some zeros, how might I get Jackson to understand the seconds timestamp? I wrote a custom deserializer to handle timestamps in seconds (Groovy syntax). class UnixTimestampDeserializer extends JsonDeserializer<DateTime> { Logger

Behind The Scenes: Core Data dates stored with 31 year offset?

半腔热情 提交于 2019-11-27 13:34:33
I know, "no user-serviceable parts inside" ... but I'm curious: In a Core Data sqlite3 DB, it seems I can get at the date within a ZDATE like so: sqlite> select datetime(ZDATE,'unixepoch','31 years','localtime') from ZMYCLASS; 2003-12-11 19:00:00 2009-12-31 19:00:00 2009-01-24 19:00:00 2011-01-01 19:00:00 2009-10-03 20:00:00 ... Unix Epoch I get, but why 31 years? Core Data stores dates relative to reference date, which is Jan 1st, 2001 (31 years after EPOCH as pointed out in the comments) Here's some code to decode the dates from the table, in case it is useful to you. NSNumber *time =

MySQL: What's the best to use, Unix TimeStamp Or DATETIME [duplicate]

狂风中的少年 提交于 2019-11-27 12:49:49
问题 This question already has answers here : Should I use the datetime or timestamp data type in MySQL? (37 answers) Closed 6 years ago . Probably many coders want to ask this question. it is What's the adventages of each one of those MySQL time formats. and which one you will prefer to use it in your apps. For me i use Unix timestamp because maybe i find it easy to convert & order records with it, and also because i never tried the DATETIME thing. but anyways i'm ready to change my mind if

Go / golang time.Now().UnixNano() convert to milliseconds?

泪湿孤枕 提交于 2019-11-27 10:05:42
问题 How can I get Unix time in Go in milliseconds? I have the following function: func makeTimestamp() int64 { return time.Now().UnixNano() % 1e6 / 1e3 } I need less precision and only want milliseconds. 回答1: Just divide it: func makeTimestamp() int64 { return time.Now().UnixNano() / int64(time.Millisecond) } Here is an example that you can compile and run to see the output package main import ( "time" "fmt" ) func main() { a := makeTimestamp() fmt.Printf("%d \n", a) } func makeTimestamp() int64