DEV Community

Priyadarshini Chettiar
Priyadarshini Chettiar

Posted on

Unix Time in Flutter

I had the privilege to work for an organization and develop a flutter application which communicates with an IoT Device. While developing the application, I was asked to use Unix Time, which otherwise called as Epoch Time.

My research taught me that

"Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds; the Unix epoch is 00:00:00 UTC on 1 January 1970; leap seconds are ignored, with a leap second having the same Unix time as the second before it, and every day is treated as if it contains exactly 86400 seconds" - source Wikipedia.

So, how do we implement it in Flutter using Dart?

Well the answer is pretty straight forward, we have inbuilt functions in dart to do the required.



void main(){
DateTime now = DateTime.now();
var epochTime= now.millisecondsSinceEpoch;
print(epochTime);
}


Enter fullscreen mode Exit fullscreen mode

image

The output we get after the above would be in milliseconds i.e. there will be 13 digits and in decimal format.

If you need the output in seconds then , just divide the output by 1000, to fix it to 10 digits



DateTime now = DateTime.now();
var epochTime= now.millisecondsSinceEpoch/1000;


Enter fullscreen mode Exit fullscreen mode

image

certainly can use floor, ceil and round methods if required to refractor the above values.



void main(){
DateTime now = DateTime.now();
var epochTime= (now.millisecondsSinceEpoch)/1000;
var testFloor= epochTime.floor();
var testCeil = epochTime.ceil();
var testRound = epochTime.round();
print(epochTime);
print("Testing Floor - " +  testFloor.toString());
print("Testing Ceil - " +  testCeil.toString());
print("Testing Round - " +  testRound.toString());
}


Enter fullscreen mode Exit fullscreen mode

image

In case you need the value in microseconds, use the microsecondsSinceEpoch function.



void main(){
DateTime now = DateTime.now();
var epochTime= now.microsecondsSinceEpoch;
print(epochTime);
}


Enter fullscreen mode Exit fullscreen mode

image

Now, what if you want the value in different formats like HexCode or Binary or Decimal

Well though it sounds little complicated its simple using the toRadixString() function.

  • To convert the time value into Binary you need to pass the parameter "2" to the above function.
  • To convert the time value into Decimal code you need to pass the parameter "10" to the above function.
  • To convert the time value into Hexadecimal code you need to pass the parameter "16" to the above function.


void main(){

// assign the output in decimal format and in seconds
var epochTime= (DateTime.now().millisecondsSinceEpoch)/1000;

//Using Floor methid to get absolute value
var convertToFloor = epochTime.floor();


//convert the value to Binary
var convertToBinary= convertToFloor.toRadixString(2); 

//convert the value to Decimal
var convertToDecimal= convertToFloor.toRadixString(10);

//convert the value to Hex Code
var convertToHex= convertToFloor.toRadixString(16);

//print all
print("The Epoch Time in seconds is "+ epochTime.toString());
print("Using Floor to get absolute value " + convertToFloor.toString());
print("Converted to Binary - " + convertToBinary);
print("Converted to Decimal - " + convertToDecimal);
print("Converted to Hex - " + convertToHex);
}



Enter fullscreen mode Exit fullscreen mode

image

That's it for this article, hope you found this article helpful.

Though its simple, I would like to know your thoughts in comments.

Until we meet again with my next article, Happy Coding 😃

Top comments (0)