I'm pulling a timestamp from a datebase, it is using RFC3339 format but the timezone is missing. So when I try to compare the timestamps it will be off. How do I change dbtime to be Eastern time?
// time format
const
(
RFC3339 = "2006-01-02T15:04:05Z07:00"
)
//now time
now := time.Now()
nowtime := now.Unix()
fmt.Println("Nowtime:", nowtime)
fmt.Println("Now:", now)
//time in db
fmt.Println("Dbtime string:", dbtime)
udbtime, err := time.Parse.EST(RFC3339,dbtime)
fmt.Println("RFC3339: " + RFC3339)
fmt.Println("dbtime parsed", udbtime)
fmt.Println("dbtime parsed unixtime", udbtime.Unix())
My output is
Nowtime: 1466443640
Now: 2016-06-20 13:27:20.963232824 -0400 EDT
Dbtime string: 2016-06-20T12:41:45.14Z
RFC3339: 2006-01-02T15:04:05Z07:00
dbtime parsed 2016-06-20 12:41:45.14 +0000 UTC
dbtime parsed unixtime 1466426505
You could parse the timestamps using the defined time.RFC3339 like you've shown, and adjust them after the fact by adding the UTC offset so that they are correct in UTC.
udbtime = udbtime.Add(4*time.Hour)
This however requires that you check whether each time falls in EST or EDT to add the correct offset, unless you can assume they are all EDT.
A better way would be to use your own time format without a specific TZ offset (the Z isn't part of the time specification for parsing), and parse it with time.ParseInLocation.
This way you can account for the correct offset depending on whether the time would have been in EST or EDT.
https://play.golang.org/p/IrUnTwvlkk
RFC3339local := "2006-01-02T15:04:05Z"
loc, err := time.LoadLocation("America/New_York")
if err != nil {
log.Fatal(err)
}
ts1 := "2016-06-20T12:41:45.14Z"
t1, _ := time.ParseInLocation(RFC3339local, ts1, loc)
fmt.Println(t1)
// prints: 2016-06-20 12:41:45.14 -0400 EDT
ts2 := "2016-01-20T12:41:45.14Z"
t2, _ = time.ParseInLocation(RFC3339local, ts2, loc)
fmt.Println(t2)
// prints: 2016-01-20 12:41:45.14 -0500 EST
you can try go-carbon,a simple,semantic and developer-fridendly golang package for datetime
carbon.Parse("2020-12-31").ToRfc3339String()
// output
2020-12-31T00:00:00+08:00
https://github.com/golang-module/carbon
Related
I know you can get the current date and time using the Now() function, but how would you get the date of yesterday?
You can use the Yesterday or IncDay(Now,-1) function from System.DateUtils as follow:
uses System.DateUtils;
begin
// Example 1:
ShowMessage('Yesterday = ' + DateToStr(Yesterday)); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Date)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(tomorrow)); // Date of tomorrow
// Example 2:
ShowMessage('Yesterday = ' + DateToStr(IncDay(Now,-1))); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Now)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(IncDay(Now,1))); // Date of tomorrow
end;
These functions return a TDateTime data type. The time component is set to zero.
Why not just use Date - 1 ?
Since one day is 1.0 in TDateTime encoding, substracting 1 is enough.
I'm using time.h with tm_hour to get the hour of the day, but the time is in 12 Hr format by default.
I need 24 Hour format (00-23 Hrs) for simple event time code, like:
time_t now;
struct tm * timeinfo;
time(&now);
timeinfo = localtime(&now);
Serial.println("24 Hr Time is: %H:%M:%S\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); //test
Serial.println(timeinfo->tm_hour);
if ((timeinfo->tm_hour)>= 22) // 10PM Event
//Do something here
Any syntax assistance appreciated.
It is working on android and browser and WAS working on iOS (ionic view and device). I am saving a date inside my local Storage (import { Storage } from '#ionic/storage';) then compare it to a another date
Here's what I do And is working everywhere but suddenly, not ios under ionic view. Last test was maximum 2 weeks ago and was working as intended !
this.storage.get(filename).then((metadata_stored) => {
if (metadata_stored && metadata_stored.date && metadata_stored.date.getTime() === filedate.getTime())
//do something
and I get TypeError: metadata_stored.date.getTime() is not a function; metadata_stored.date.getTime is undefined.
filedate is create through something like this filedate: Date = new date("2017-10-14T15:44:48+02:00") (supposedly an ISO). Then it is saved in my local storage : this.storage.set(filename, { /* ... */ , date: filedate })
If I display metadata_stored.data, I get something like 2017-08-13T13:44:10.000Z
I don't get why it suddenly and silently stopped working. And how to correct it since metadate.date is an old value of filedate and filedate is a new Date(...) ! No code were changed in this section last 2 weeks.
Edit: I solved it by doing (new Date(metadata_stored.date)).getTime() instead. But I still have no idea why it stopped working for no reason...
In local storage, everything is stored as a string.
That means you should use (new Date(metadata_stored.date)).getTime() because metadata_stored.date is a string, not a Date.
let a = new Date();
let b = new Date();
// 2017-11-30T18:01:01457Z object
console.log(a, typeof a);
// 2017-11-30T18:01:01457Z object
console.log(b, typeof b);
localStorage.a = a;
localStorage.b = b;
a = localStorage.a;
b = localStorage.b;
// Thu Nov 30 18:01:01 GMT+0000 (GMT Standard Time) string
console.log(a, typeof a);
// Thu Nov 30 18:01:01 GMT+0000 (GMT Standard Time) string
console.log(b, typeof b);
a = new Date(localStorage.b);
b = new Date(localStorage.a);
// 2017-11-30T18:01:01457Z object
console.log(a, typeof a);
// 2017-11-30T18:01:01457Z object
console.log(b, typeof b);
I have the following code:
timeLocal := "01/July/2015:18:12:25 +0900"
inFormat := "02/January/2006:15:04:05 -0700"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
Now, parsing works fine. But when I run:
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed)
The output is:
01/July/2015:18:12:25 +0900
02/January/2006:15:04:05 -0700
2015-07-01 18:12:25 +0900 +0900
Should the second +0900 be there? What is the stupid thing that I'm doing? Sorry, it was really a long day, I don't see what I'm missing.
Oh, and the whole file is here:
package main
import (
"fmt"
"time"
"log"
)
func main() {
timeLocal := "01/July/2015:18:12:25 +0900"
inFormat := "02/January/2006:15:04:05 -0700"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed)
}
If you look at documentation of time.Time you will see what the default output format is:
String returns the time formatted using the format string:
"2006-01-02 15:04:05.999999999 -0700 MST"
Now you should see what the second +0900 is doing there – this is a location (time zone) name. Since your input format doesn't have a name it will simply repeat an offset.
You can provide name by altering your input format to parse location name. Alternatively you can provide an output format which doesn't print the name, if you don't need it.
Your modified example:
package main
import (
"fmt"
"time"
"log"
)
func main() {
timeLocal := "01/July/2015:18:12:25 +0900 XYZ"
inFormat := "02/January/2006:15:04:05 -0700 MST"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed) // 2015-07-01 18:12:25 +0900 XYZ
fmt.Println(parsed.Format("02/January/2006:15:04:05 -0700"))
}
http://play.golang.org/p/xVGvlt-M5B
The default format used by Time.String is this:
2006-01-02 15:04:05.999999999 -0700 MST
Notice the "MST" part. Since you're not providing the name of the zone, the format just "names" it the same as offset, that is "+0900". If you change that to "+0000", you'll see that this is indeed a time zone name:
2015-07-01 18:12:25 +0000 UTC
If you don't want that, just use a separate format for printing:
myFmt := "2006-01-02 15:04:05.999999999 -0700"
fmt.Println(parsed.Format(myFmt))
I use below codes to convert a 12hour UTC to a new timezone
MySourceCodes: (part of my udp socket server codes)
// $gps_time = "9:43:52";
$gps_time = $time_hour.":".$time_min.":".$time_sec;
// $time_received = "01:45:04 2012-07-28";
$time_received = date('H:i:s Y-m-d');
$utc = new DateTimeZone("UTC");
$moscow = new DateTimeZone("Europe/Moscow");
//Instantiate both AM and PM versions of your time
$gps_time_am = new DateTime("$gps_time AM", $utc);
$gps_time_pm = new DateTime("$gps_time PM", $utc);
//Received time
$time_received = new DateTime($time_received, $moscow);
//Change timezone to Moscow
$gps_time_am->setTimezone($moscow); /* ### ### Line 105 Error ### ### */
$gps_time_pm->setTimezone($moscow);
//Check the difference in hours. If it's less than 1 hour difference, it's the correct one.
if ($time_received->diff($gps_time_pm)->h < 1) {
$correct_time = $gps_time_pm->format("H:i:s Y-m-d");
}
else {
$correct_time = $gps_time_am->format("H:i:s Y-m-d");
}
echo $correct_time;
PHP Error :
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (0:11:38 AM) at position 8 (A): The timezone could not be found in the database' in C:\xampp\php\udp.php:105
Stack trace:
#0 C:\xampp\php\udp.php(105): DateTime->__construct('0:11:38 AM', Object(DateTimeZone))
#1 {main} thrown in C:\xampp\php\udp.php on line 105
Error give when input time is 0:11:38 AM
Question:
how can I solve the problem ?
Edit: my input time Source from GPS Tracker (change seconds to 12hour format time)
$time_hour = floor($get_time / 3600);
$time_min = floor(($get_time - ($time_hour*3600)) / 60);
$time_sec = dotwodigits(($get_time - (($time_hour*3600) + ($time_min * 60))));
GPS Time = $time_hour.":".$time_min.":".$time_sec
As I've pointed out in the comments, 12-hour time doesn't have a 0:xx:yy time. What you can do, then, is check the value of $time_hour beforehand:
//assigning values to $time_hour, $time_min, and $time_sec
if ($time_hour == 0) $time_hour = 12;
//rest of code
This will prevent the invalid time from being passed to the DateTime constructor.
EDIT: This check can actually be performed during the assignment of $time_hour:
$time_hour = floor($get_time / 3600) ?: 12;