Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/techgaun/unix-time
Unix time in various languages - A reference
https://github.com/techgaun/unix-time
java javascript time timestamp unix
Last synced: about 1 month ago
JSON representation
Unix time in various languages - A reference
- Host: GitHub
- URL: https://github.com/techgaun/unix-time
- Owner: techgaun
- License: apache-2.0
- Created: 2015-07-31T18:48:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-02T20:25:50.000Z (over 7 years ago)
- Last Synced: 2024-08-03T18:22:21.566Z (4 months ago)
- Topics: java, javascript, time, timestamp, unix
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nepal - Unix time in various languages
README
# unix-time
Unix time in various languages - A reference### What is [Unix time](https://en.wikipedia.org/wiki/Unix_time)?
Unix time (also known as POSIX time or erroneously as Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.### Examples
#### How to get current unix time
_UTaaS (Unix Timestamp as a Service) (generic)_
```shell
curl https://icanhazepoch.com/
```_C_
```c
#include
#includeint main(int argc, char **argv) {
unsigned int unix_time = (unsigned) time(NULL);
fprintf(stdout, "%u\n", unix_time);
return 0;
}
```_C++_
```cpp
#include
#includeusing namespace std;
int main(int argc, char **argv) {
unsigned int unix_time = (unsigned) time(NULL);
cout << unix_time << endl;
return 0;
}
```_Elixir_
```elixir
# first way
DateTime.utc_now |> DateTime.to_unix# second way
System.system_time(:second)
```_Erlang_
```erlang
os:system_time(second).
```_Golang_
```go
now := time.Now()
unixTs := now.Unix()
```_Java_
```java
long timestamp = System.currentTimeMillis()/1000;
System.out.println(timestamp);
```Java 8 brings a new API to work with date and times, [Instant](http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html). A good read about why [JSR-310 is not same as joda-time](http://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html). You can get current unix time on Java 8 by doing something like below:
```java
long timestamp = Instant.now().getEpochSecond();
System.out.println(timestamp);
```_Javascript_
```javascript
Math.floor(new Date() / 1000)
```[Moment](https://github.com/moment/moment/) is very popular in javascript world for playing with dates in javascript. Below is the momentjs example.
```javascript
var moment = require('moment');
var unixtime = moment().unix();
```_PHP_
```php
echo time();//php5.3 and above
//OOP style
$date = new DateTime();
echo $date->getTimestamp();//procedural style
$date = date_create();
echo date_timestamp_get($date);
```_Python_
```python
from time import time
print(int(time()))import datetime
print(int(datetime.datetime.utcnow().timestamp()))
```_Ruby_
```ruby
Time.now.to_i
```